1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| """ Euro NCAP 2026 场景检测引擎
根据官方协议v1.2实现场景匹配和警告等级判定 """
from dataclasses import dataclass from enum import IntEnum from typing import Dict, Optional
class WarningLevel(IntEnum): NONE = 0 LEVEL_1 = 1 LEVEL_2 = 2 LEVEL_3 = 3
@dataclass class DriverState: """驾驶员状态输入""" perclos: float = 0.0 eye_closure_duration: float = 0.0 gaze_off_road_duration: float = 0.0 phone_detected: bool = False phone_position: str = "" head_rotation: float = 0.0 yawn_detected: bool = False impairment_score: float = 0.0 responsive: bool = True
@dataclass class OccupantState: """乘员状态输入""" seat_occupied: bool = False weight_kg: float = 0.0 seatbelt_buckled: bool = False seatbelt_position: str = "correct" child_seat_detected: bool = False child_seat_type: str = "" posture: str = "normal" distance_to_dashboard: float = 999.0
@dataclass class CabinState: """座舱环境""" speed_kmh: float = 0.0 door_locked: bool = False engine_on: bool = False cabin_temp: float = 25.0 time_since_lock: float = 0.0
class EuroNCAP2026ScenarioEngine: """ Euro NCAP 2026 协议场景检测引擎 根据协议v1.2实现: - DMS: 疲劳/分心/损伤/无响应 - CPD: 儿童存在检测 - 安全带监测 - 气囊自适应 """ def evaluate_dms(self, driver: DriverState, cabin: CabinState) -> Dict: """评估DMS场景""" results = [] if cabin.speed_kmh >= 50: if driver.perclos > 0.40 and driver.eye_closure_duration > 5: results.append(('F-03', 'severe_fatigue', WarningLevel.LEVEL_2, 3.0)) elif driver.perclos > 0.25: results.append(('F-02', 'moderate_fatigue', WarningLevel.LEVEL_1, 5.0)) elif driver.perclos > 0.15: results.append(('F-01', 'mild_fatigue', WarningLevel.LEVEL_1, 10.0)) if 1.3 <= driver.eye_closure_duration <= 1.7: results.append(('F-04', 'microsleep', WarningLevel.LEVEL_1, 3.0)) if driver.gaze_off_road_duration > 3.0: results.append(('D-01', 'gaze_off_road', WarningLevel.LEVEL_1, 4.0)) if driver.phone_detected: if driver.phone_position == 'ear': results.append(('D-02', 'phone_ear', WarningLevel.LEVEL_1, 3.0)) elif driver.phone_position == 'texting': results.append(('D-03', 'phone_texting', WarningLevel.LEVEL_1, 3.0)) if abs(driver.head_rotation) > 45: results.append(('D-05', 'turning_back', WarningLevel.LEVEL_1, 4.0)) if driver.impairment_score > 0.6: results.append(('I-01', 'impairment_detected', WarningLevel.LEVEL_1, 5.0)) if not driver.responsive: results.append(('U-01', 'unresponsive_l1', WarningLevel.LEVEL_1, 5.0)) return { 'scenarios': results, 'max_level': max(r[2] for r in results) if results else 0, } def evaluate_cpd(self, occupant: OccupantState, cabin: CabinState) -> Dict: """评估CPD场景""" results = [] if not cabin.engine_on and not occupant.seat_occupied: return {'scenarios': [], 'max_level': 0} is_child = (occupant.weight_kg < 30 or occupant.child_seat_detected) if is_child and cabin.door_locked: if cabin.time_since_lock <= 15: results.append(('C-01', 'child_detected_locked', WarningLevel.LEVEL_1, 15.0)) if cabin.cabin_temp > 40: results.append(('C-04', 'hot_car_emergency', WarningLevel.LEVEL_3, 0.0)) return { 'scenarios': results, 'max_level': max(r[2] for r in results) if results else 0, } def evaluate_seatbelt(self, occupant: OccupantState, cabin: CabinState) -> Dict: """评估安全带场景""" results = [] if occupant.seat_occupied and not occupant.seatbelt_buckled: if cabin.speed_kmh > 22: results.append(('B-01', 'no_seatbelt_front', WarningLevel.LEVEL_1, 0.0)) elif occupant.seatbelt_buckled: if occupant.seatbelt_position == 'behind_back': results.append(('B-03', 'belt_behind_back', WarningLevel.LEVEL_1, 10.0)) elif occupant.seatbelt_position == 'lap_only': results.append(('B-04', 'belt_lap_only', WarningLevel.LEVEL_1, 10.0)) return { 'scenarios': results, 'max_level': max(r[2] for r in results) if results else 0, } def evaluate_airbag(self, occupant: OccupantState) -> Dict: """评估气囊自适应场景""" results = [] if occupant.child_seat_detected and occupant.child_seat_type == 'rear_facing': results.append(('A-01', 'rear_facing_child_seat', WarningLevel.NONE, 0.0)) if occupant.distance_to_dashboard < 20: results.append(('A-04', 'too_close_dashboard', WarningLevel.LEVEL_1, 3.0)) if occupant.posture == 'feet_up': results.append(('A-03', 'feet_on_dashboard', WarningLevel.LEVEL_1, 5.0)) return { 'scenarios': results, 'max_level': max(r[2] for r in results) if results else 0, }
if __name__ == "__main__": engine = EuroNCAP2026ScenarioEngine() driver = DriverState( perclos=0.28, eye_closure_duration=0.5, gaze_off_road_duration=4.5, phone_detected=True, phone_position='texting', ) cabin = CabinState(speed_kmh=80, engine_on=True) dms_result = engine.evaluate_dms(driver, cabin) print("=== DMS评估 ===") for scenario_id, name, level, max_delay in dms_result['scenarios']: print(f" {scenario_id}: {name} → 警告等级{level}, 时限{max_delay}秒") print(f" 最高警告等级: {dms_result['max_level']}") child = OccupantState( seat_occupied=True, weight_kg=15, child_seat_detected=True, child_seat_type='rear_facing' ) locked_cabin = CabinState(door_locked=True, time_since_lock=10, cabin_temp=42, engine_on=False) cpd_result = engine.evaluate_cpd(child, locked_cabin) print("\n=== CPD评估 ===") for scenario_id, name, level, max_delay in cpd_result['scenarios']: print(f" {scenario_id}: {name} → 警告等级{level}, 时限{max_delay}秒") print(f" 最高警告等级: {cpd_result['max_level']}") passenger = OccupantState( seat_occupied=True, seatbelt_buckled=True, seatbelt_position='behind_back' ) belt_result = engine.evaluate_seatbelt(passenger, cabin) print("\n=== 安全带评估 ===") for scenario_id, name, level, max_delay in belt_result['scenarios']: print(f" {scenario_id}: {name} → 警告等级{level}, 时限{max_delay}秒")
|