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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
| import numpy as np import time from typing import Dict, List, Optional from dataclasses import dataclass from enum import Enum
class DriverState(Enum): """驾驶员状态""" NORMAL = 0 DROWSY = 1 DISTRACTED = 2 UNRESPONSIVE = 3 EMERGENCY = 4
class InterventionLevel(Enum): """干预等级""" NONE = 0 WARNING_SOUND = 1 WARNING_VIBRATION = 2 DECELERATE = 3 EMERGENCY_STOP = 4 AUTO_PULLOVER = 5
@dataclass class DMSData: """DMS数据""" timestamp: float eye_closure: float blink_rate: float gaze_direction: Tuple[float, float] head_pose: Tuple[float, float, float] attention_score: float @dataclass class VehicleData: """车辆数据""" timestamp: float speed: float steering_angle: float accelerator_position: float brake_position: float lane_position: float front_vehicle_distance: float
@dataclass class ADASData: """ADAS数据""" timestamp: float lane_keep_enabled: bool acc_enabled: bool aeb_enabled: bool emergency_stop_available: bool
class DMSADASIntegration: """ DMS-ADAS集成系统 实现无响应驾驶员检测与干预 """ def __init__(self, config: dict): self.config = config self.driver_state = DriverState.NORMAL self.intervention_level = InterventionLevel.NONE self.dms_history: List[DMSData] = [] self.vehicle_history: List[VehicleData] = [] self.eye_closure_threshold = config.get('eye_closure_threshold', 0.8) self.unresponsive_time_threshold = config.get('unresponsive_time_threshold', 5.0) self.warning_time_threshold = config.get('warning_time_threshold', 3.0) self.intervention_controller = InterventionController(config) self.emergency_call = EmergencyCallModule(config) def process(self, dms_data: DMSData, vehicle_data: VehicleData, adas_data: ADASData) -> Dict: """ 处理输入数据,决定干预措施 Args: dms_data: DMS传感器数据 vehicle_data: 车辆状态数据 adas_data: ADAS系统数据 Returns: action: 干预动作 """ self._update_history(dms_data, vehicle_data) driver_state = self._assess_driver_state(dms_data, vehicle_data) intervention_level = self._determine_intervention( driver_state, vehicle_data, adas_data ) action = self._execute_intervention( intervention_level, vehicle_data, adas_data ) return { 'driver_state': driver_state, 'intervention_level': intervention_level, 'action': action } def _update_history(self, dms_data: DMSData, vehicle_data: VehicleData): """更新历史数据""" self.dms_history.append(dms_data) self.vehicle_history.append(vehicle_data) max_history = 30 * 30 if len(self.dms_history) > max_history: self.dms_history = self.dms_history[-max_history:] self.vehicle_history = self.vehicle_history[-max_history:] def _assess_driver_state(self, dms_data: DMSData, vehicle_data: VehicleData) -> DriverState: """ 评估驾驶员状态 使用多模态融合判断 """ eye_score = self._analyze_eye_state(dms_data) head_score = self._analyze_head_state(dms_data) behavior_score = self._analyze_behavior(dms_data, vehicle_data) weights = [0.4, 0.2, 0.4] total_score = ( weights[0] * eye_score + weights[1] * head_score + weights[2] * behavior_score ) if total_score < 0.2: return DriverState.NORMAL elif total_score < 0.5: return DriverState.DROWSY elif total_score < 0.8: return DriverState.DISTRACTED else: return DriverState.UNRESPONSIVE def _analyze_eye_state(self, dms_data: DMSData) -> float: """ 分析眼睛状态 Returns: score: 异常分数 (0-1),越高越异常 """ recent_data = self.dms_history[-90:] closed_frames = sum( 1 for d in recent_data if d.eye_closure > self.eye_closure_threshold ) closure_ratio = closed_frames / len(recent_data) if recent_data else 0 blink_rate = dms_data.blink_rate blink_abnormality = 0 if blink_rate < 10: blink_abnormality = (10 - blink_rate) / 10 elif blink_rate > 30: blink_abnormality = (blink_rate - 30) / 30 eye_score = 0.7 * closure_ratio + 0.3 * blink_abnormality return min(1.0, eye_score) def _analyze_head_state(self, dms_data: DMSData) -> float: """ 分析头部姿态 Returns: score: 异常分数 """ yaw, pitch, roll = dms_data.head_pose pitch_abnormality = max(0, (pitch - 15) / 30) if pitch > 15 else 0 recent_data = self.dms_history[-90:] if len(recent_data) > 10: yaw_variance = np.var([d.head_pose[0] for d in recent_data]) head_still = yaw_variance < 10 if head_still: pitch_abnormality = max(pitch_abnormality, 0.5) return min(1.0, pitch_abnormality) def _analyze_behavior(self, dms_data: DMSData, vehicle_data: VehicleData) -> float: """ 分析驾驶行为 Returns: score: 异常分数 """ recent_vehicle = self.vehicle_history[-450:] if len(recent_vehicle) > 10: steering_variance = np.var([v.steering_angle for v in recent_vehicle]) no_steering = steering_variance < 0.01 else: no_steering = False recent_accel = [v.accelerator_position for v in recent_vehicle[-450:]] recent_brake = [v.brake_position for v in recent_vehicle[-450:]] accel_variance = np.var(recent_accel) if recent_accel else 0 brake_variance = np.var(recent_brake) if recent_brake else 0 no_pedal = accel_variance < 0.01 and brake_variance < 0.01 attention = dms_data.attention_score behavior_score = ( 0.3 * (1 if no_steering else 0) + 0.3 * (1 if no_pedal else 0) + 0.4 * (1 - attention) ) return behavior_score def _determine_intervention(self, driver_state: DriverState, vehicle_data: VehicleData, adas_data: ADASData) -> InterventionLevel: """ 决定干预等级 Args: driver_state: 驾驶员状态 vehicle_data: 车辆数据 adas_data: ADAS数据 Returns: intervention_level: 干预等级 """ if driver_state == DriverState.NORMAL: return InterventionLevel.NONE elif driver_state == DriverState.DROWSY: return InterventionLevel.WARNING_SOUND elif driver_state == DriverState.DISTRACTED: return InterventionLevel.WARNING_VIBRATION elif driver_state == DriverState.UNRESPONSIVE: if vehicle_data.speed > 60: if adas_data.lane_keep_enabled: return InterventionLevel.DECELERATE else: return InterventionLevel.EMERGENCY_STOP else: return InterventionLevel.AUTO_PULLOVER return InterventionLevel.NONE def _execute_intervention(self, level: InterventionLevel, vehicle_data: VehicleData, adas_data: ADASData) -> Dict: """ 执行干预措施 """ action = { 'level': level, 'commands': [] } if level == InterventionLevel.NONE: pass elif level == InterventionLevel.WARNING_SOUND: action['commands'].append({ 'type': 'AUDIO_WARNING', 'sound': 'DROWSINESS_ALERT', 'volume': 0.7 }) elif level == InterventionLevel.WARNING_VIBRATION: action['commands'].append({ 'type': 'AUDIO_WARNING', 'sound': 'DROWSINESS_ALERT', 'volume': 1.0 }) action['commands'].append({ 'type': 'HAPTIC_WARNING', 'location': 'STEERING_WHEEL', 'intensity': 0.8 }) elif level == InterventionLevel.DECELERATE: action['commands'].append({ 'type': 'AUDIO_WARNING', 'sound': 'EMERGENCY_ALERT', 'volume': 1.0 }) action['commands'].append({ 'type': 'VEHICLE_CONTROL', 'action': 'DECELERATE', 'target_speed': 30, 'deceleration_rate': 2.0 }) action['commands'].append({ 'type': 'ADAS_ENABLE', 'system': 'LANE_KEEP' }) elif level == InterventionLevel.EMERGENCY_STOP: action['commands'].append({ 'type': 'AUDIO_WARNING', 'sound': 'EMERGENCY_STOP', 'volume': 1.0 }) action['commands'].append({ 'type': 'VEHICLE_CONTROL', 'action': 'EMERGENCY_STOP', 'deceleration_rate': 5.0 }) action['commands'].append({ 'type': 'HAZARD_LIGHTS', 'state': 'ON' }) elif level == InterventionLevel.AUTO_PULLOVER: action['commands'].append({ 'type': 'AUDIO_WARNING', 'sound': 'AUTO_PULLOVER', 'volume': 1.0 }) action['commands'].append({ 'type': 'VEHICLE_CONTROL', 'action': 'AUTO_PULLOVER', 'target_lane': 'RIGHTMOST', 'target_speed': 0 }) action['commands'].append({ 'type': 'HAZARD_LIGHTS', 'state': 'ON' }) action['commands'].append({ 'type': 'EMERGENCY_CALL', 'reason': 'UNRESPONSIVE_DRIVER' }) return action
class InterventionController: """ 干预控制器 与车辆ECU通信执行控制命令 """ def __init__(self, config: dict): self.config = config def execute(self, action: Dict) -> bool: """ 执行控制动作 Returns: success: 是否成功 """ for command in action.get('commands', []): if command['type'] == 'AUDIO_WARNING': self._play_audio(command['sound'], command['volume']) elif command['type'] == 'HAPTIC_WARNING': self._vibrate(command['location'], command['intensity']) elif command['type'] == 'VEHICLE_CONTROL': self._vehicle_control(command) elif command['type'] == 'EMERGENCY_CALL': self._emergency_call(command['reason']) return True def _play_audio(self, sound: str, volume: float): """播放警告音""" print(f"[AUDIO] 播放: {sound}, 音量: {volume}") def _vibrate(self, location: str, intensity: float): """触觉警告""" print(f"[HAPTIC] 振动: {location}, 强度: {intensity}") def _vehicle_control(self, command: Dict): """车辆控制""" print(f"[CONTROL] {command}") def _emergency_call(self, reason: str): """紧急呼叫""" print(f"[SOS] 紧急呼叫: {reason}")
class EmergencyCallModule: """ 紧急呼叫模块 """ def __init__(self, config: dict): self.config = config def call(self, reason: str, location: Tuple[float, float]): """ 发起紧急呼叫 Args: reason: 原因 location: GPS坐标 (lat, lon) """ print(f"[eCall] 紧急呼叫 - 原因: {reason}, 位置: {location}")
if __name__ == "__main__": config = { 'eye_closure_threshold': 0.8, 'unresponsive_time_threshold': 5.0, 'warning_time_threshold': 3.0 } system = DMSADASIntegration(config) dms_data = DMSData( timestamp=time.time(), eye_closure=0.9, blink_rate=5, gaze_direction=(0, 0), head_pose=(0, 30, 0), attention_score=0.1 ) vehicle_data = VehicleData( timestamp=time.time(), speed=80, steering_angle=0, accelerator_position=0.3, brake_position=0, lane_position=0, front_vehicle_distance=100 ) adas_data = ADASData( timestamp=time.time(), lane_keep_enabled=True, acc_enabled=True, aeb_enabled=True, emergency_stop_available=True ) result = system.process(dms_data, vehicle_data, adas_data) print("检测结果:") print(f" 驾驶员状态: {result['driver_state']}") print(f" 干预等级: {result['intervention_level']}") print(f" 干预动作: {result['action']}")
|