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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
| import numpy as np from typing import Dict, List, Optional from dataclasses import dataclass from enum import Enum
class DriverState(Enum): NORMAL = "normal" FATIGUE = "fatigue" DISTRACTED = "distracted" UNRESPONSIVE = "unresponsive" MEDICAL_EMERGENCY = "medical_emergency"
@dataclass class DriverStatus: state: DriverState confidence: float unresponsive_duration: float indicators: List[str]
class UnresponsiveDriverDetector: """ 无响应驾驶员检测器 检测指标: 1. 眼睛闭合持续时长 2. 头部姿态异常 3. 无方向盘操作 4. 无踏板操作 5. 无语音响应 Euro NCAP要求: - 检测延时 <10秒 - 误报率 <1次/小时 """ def __init__(self): self.thresholds = { 'eye_closure_duration': 5.0, 'no_steering_duration': 8.0, 'no_pedal_duration': 10.0, 'head_drop_angle': 30, 'no_response_to_warning': 5.0 } self.eye_closure_start = None self.last_steering_time = None self.last_pedal_time = None self.warning_issued = False def update(self, eye_state: Dict, head_pose: np.ndarray, steering_angle: float, pedal_state: Dict, timestamp: float) -> DriverStatus: """ 更新驾驶员状态 Args: eye_state: {'left_open': bool, 'right_open': bool, 'perclos': float} head_pose: (pitch, yaw, roll) 头部姿态 steering_angle: 方向盘转角 pedal_state: {'brake': float, 'accelerator': float} timestamp: 当前时间戳 Returns: DriverStatus: 驾驶员状态 """ indicators = [] eyes_open = eye_state.get('left_open', True) and eye_state.get('right_open', True) if not eyes_open: if self.eye_closure_start is None: self.eye_closure_start = timestamp closure_duration = timestamp - self.eye_closure_start if closure_duration > self.thresholds['eye_closure_duration']: indicators.append(f'eye_closed_{closure_duration:.1f}s') else: self.eye_closure_start = None pitch, yaw, roll = head_pose if pitch > self.thresholds['head_drop_angle']: indicators.append(f'head_drop_{pitch:.0f}deg') if steering_angle == 0: if self.last_steering_time is None: self.last_steering_time = timestamp elif timestamp - self.last_steering_time > self.thresholds['no_steering_duration']: indicators.append('no_steering_input') else: self.last_steering_time = timestamp if pedal_state['brake'] == 0 and pedal_state['accelerator'] == 0: if self.last_pedal_time is None: self.last_pedal_time = timestamp elif timestamp - self.last_pedal_time > self.thresholds['no_pedal_duration']: indicators.append('no_pedal_input') else: self.last_pedal_time = timestamp if len(indicators) >= 2: state = DriverState.UNRESPONSIVE confidence = 0.9 elif len(indicators) == 1: state = DriverState.FATIGUE confidence = 0.7 else: state = DriverState.NORMAL confidence = 0.95 return DriverStatus( state=state, confidence=confidence, unresponsive_duration=self._calculate_unresponsive_duration(timestamp), indicators=indicators ) def _calculate_unresponsive_duration(self, timestamp: float) -> float: """计算无响应时长""" if self.eye_closure_start is not None: return timestamp - self.eye_closure_start return 0.0
class InterventionDecision: """ 干预决策器 决策流程: 1. 检测到无响应 → 一级警告 2. 5秒内无反应 → 二级警告 3. 10秒内无反应 → 启动干预 """ def __init__(self): self.warning_levels = { 1: { 'name': '轻微警告', 'action': ['声音提示', '仪表盘闪烁'], 'duration': 3.0 }, 2: { 'name': '紧急警告', 'action': ['高频声音', '座椅震动', '语音"请立即接管"'], 'duration': 5.0 }, 3: { 'name': '自动干预', 'action': ['车道保持', '减速', '靠边停车', '紧急呼叫'], 'duration': float('inf') } } self.current_level = 0 self.level_start_time = None def decide(self, driver_status: DriverStatus, timestamp: float) -> Dict: """ 决策干预动作 Args: driver_status: 驾驶员状态 timestamp: 时间戳 Returns: action: 干预动作 """ if driver_status.state == DriverState.NORMAL: self.current_level = 0 self.level_start_time = None return {'level': 0, 'action': 'none'} if driver_status.state == DriverState.UNRESPONSIVE: if self.current_level == 0: self.current_level = 1 self.level_start_time = timestamp return self._get_action(1) elapsed = timestamp - self.level_start_time if self.current_level == 1 and elapsed > 3.0: self.current_level = 2 self.level_start_time = timestamp return self._get_action(2) if self.current_level == 2 and elapsed > 5.0: self.current_level = 3 self.level_start_time = timestamp return self._get_action(3) return self._get_action(self.current_level) def _get_action(self, level: int) -> Dict: """获取干预动作""" warning = self.warning_levels.get(level, self.warning_levels[1]) return { 'level': level, 'name': warning['name'], 'actions': warning['action'] }
class VehicleInterventionController: """ 车辆干预控制器 执行动作: 1. 车道保持(防止偏离) 2. 减速控制(安全停车) 3. 靠边停车(紧急停车带) 4. 紧急呼叫(eCall) """ def __init__(self): self.intervention_active = False self.target_speed = 0 self.target_lane_position = 0 def execute(self, action: Dict, vehicle_state: Dict) -> Dict: """ 执行干预动作 Args: action: 干预决策 vehicle_state: 车辆状态 Returns: control: 控制指令 """ if action['level'] == 0: self.intervention_active = False return {'type': 'none'} if action['level'] == 3: self.intervention_active = True return self._emergency_stop_sequence(vehicle_state) return { 'type': 'warning', 'level': action['level'], 'hmi_actions': action['actions'] } def _emergency_stop_sequence(self, vehicle_state: Dict) -> Dict: """ 紧急停车序列 步骤: 1. 激活车道保持 2. 缓慢减速 3. 靠右行驶 4. 完全停车 5. 开启危险警报灯 6. 拨打紧急电话 """ current_speed = vehicle_state.get('speed', 0) control = { 'type': 'emergency_intervention', 'steps': [] } control['steps'].append({ 'action': 'lane_keeping', 'enable': True }) if current_speed > 0: decel_rate = min(2.0, current_speed / 10) control['steps'].append({ 'action': 'decelerate', 'rate': decel_rate, 'target_speed': 0 }) if vehicle_state.get('road_type') == 'highway': control['steps'].append({ 'action': 'change_lane', 'direction': 'right', 'target_position': 'shoulder' }) control['steps'].append({ 'action': 'hazard_lights', 'enable': True }) control['steps'].append({ 'action': 'emergency_call', 'type': 'eCall', 'message': 'Driver unresponsive. Vehicle stopped at location.' }) return control
class UnresponsiveDriverInterventionSystem: """ 完整无响应驾驶员干预系统 Euro NCAP 2026合规方案 """ def __init__(self): self.detector = UnresponsiveDriverDetector() self.decision = InterventionDecision() self.controller = VehicleInterventionController() def process(self, dms_data: Dict, vehicle_data: Dict, timestamp: float) -> Dict: """ 处理周期 Args: dms_data: DMS传感器数据 vehicle_data: 车辆状态数据 timestamp: 时间戳 Returns: result: 处理结果 """ driver_status = self.detector.update( eye_state=dms_data.get('eye_state', {}), head_pose=dms_data.get('head_pose', np.array([0, 0, 0])), steering_angle=vehicle_data.get('steering_angle', 0), pedal_state=vehicle_data.get('pedal_state', {'brake': 0, 'accelerator': 0}), timestamp=timestamp ) action = self.decision.decide(driver_status, timestamp) control = self.controller.execute(action, vehicle_data) return { 'driver_status': driver_status, 'intervention_action': action, 'vehicle_control': control }
def euro_ncap_unresponsive_scenarios(): """ Euro NCAP无响应驾驶员测试场景 来源:Euro NCAP Assessment Protocol SA v10.4 """ scenarios = [ { "id": "UR-01", "description": "驾驶员突发疾病(眼睛闭合)", "trigger": "eye_closure > 5s", "expected_response": { "warning": "3s内发出警告", "intervention": "10s内启动干预", "stop": "安全停车" } }, { "id": "UR-02", "description": "驾驶员严重疲劳(头部下垂)", "trigger": "head_pitch > 30deg for 8s", "expected_response": { "warning": "声音+震动", "intervention": "车道保持激活" } }, { "id": "UR-03", "description": "驾驶员无方向盘输入", "trigger": "no_steering > 8s", "expected_response": { "warning": "一级警告", "escalation": "无响应升级到干预" } }, { "id": "UR-04", "description": "驾驶员对警告无响应", "trigger": "no_response_to_warning > 5s", "expected_response": { "intervention": "自动停车", "ecall": "拨打紧急电话" } } ] return scenarios
if __name__ == "__main__": system = UnresponsiveDriverInterventionSystem() print("=" * 60) print("无响应驾驶员干预系统测试") print("=" * 60) timestamps = np.arange(0, 20, 0.5) for t in timestamps: if t < 5: eye_state = {'left_open': True, 'right_open': True, 'perclos': 0.1} head_pose = np.array([0, 0, 0]) elif t < 15: eye_state = {'left_open': False, 'right_open': False, 'perclos': 0.8} head_pose = np.array([35, 0, 0]) else: eye_state = {'left_open': True, 'right_open': True, 'perclos': 0.1} head_pose = np.array([0, 0, 0]) dms_data = { 'eye_state': eye_state, 'head_pose': head_pose } vehicle_data = { 'steering_angle': 0 if t > 5 and t < 15 else np.random.randn() * 5, 'pedal_state': {'brake': 0, 'accelerator': 0.2 if t < 5 else 0}, 'speed': 80, 'road_type': 'highway' } result = system.process(dms_data, vehicle_data, t) if result['driver_status'].state != DriverState.NORMAL: print(f"\n[t={t:.1f}s] {result['driver_status'].state.value}") print(f" 指标: {result['driver_status'].indicators}") print(f" 干预等级: {result['intervention_action']['level']}") if result['vehicle_control']['type'] == 'emergency_intervention': print(" ⚠️ 紧急干预已启动!") for step in result['vehicle_control']['steps']: print(f" - {step}")
|