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
| import numpy as np from typing import Dict, List, Tuple from enum import Enum
class DriverState(Enum): """驾驶员状态枚举""" ALERT = 'ALERT' LIGHT_DROWSY = 'LIGHT_DROWSY' HEAVY_DROWSY = 'HEAVY_DROWSY' UNRESPONSIVE = 'UNRESPONSIVE' EMERGENCY = 'EMERGENCY'
class UnresponsiveDriverIntervention: """ 无响应驾驶员干预系统 Euro NCAP 2026要求的DMS-ADAS协同方案 """ def __init__(self, config: dict): self.dms = DriverMonitoringSystem( eye_tracker=config.get('eye_tracker'), head_pose_detector=config.get('head_pose_detector') ) self.adas = ADASController( vehicle_interface=config.get('vehicle_interface') ) self.alert_manager = AlertManager() self.intervention_planner = InterventionPlanner() self.state = DriverState.ALERT self.unresponsive_start_time = None self.alert_count = 0 self.light_drowsy_threshold = config.get('light_drowsy_threshold', 5) self.heavy_drowsy_threshold = config.get('heavy_drowsy_threshold', 10) self.unresponsive_threshold = config.get('unresponsive_threshold', 15) self.intervention_threshold = config.get('intervention_threshold', 20) def process_frame(self, dms_data: Dict, adas_data: Dict, timestamp: float) -> Dict: """ 处理单帧数据 Args: dms_data: DMS检测数据 adas_data: ADAS感知数据 timestamp: 时间戳 Returns: result: 处理结果 """ new_state = self._detect_driver_state(dms_data, timestamp) if new_state != self.state: self._handle_state_transition(new_state, timestamp) action = self._execute_action(adas_data, timestamp) return { 'driver_state': self.state, 'action': action, 'alert_level': self.alert_count, 'unresponsive_duration': self._get_unresponsive_duration(timestamp) } def _detect_driver_state(self, dms_data: Dict, timestamp: float) -> DriverState: """ 检测驾驶员状态 综合多个指标判断: 1. PERCLOS(眼睑闭合百分比) 2. 头部姿态 3. 视线方向 4. 面部表情 """ perclos = dms_data.get('perclos', 0) head_pose = dms_data.get('head_pose', {}) gaze_direction = dms_data.get('gaze_direction', (0, 0)) eyes_closed = dms_data.get('eyes_closed', False) blink_rate = dms_data.get('blink_rate', 15) if eyes_closed and perclos > 0.8: return DriverState.UNRESPONSIVE elif perclos > 0.5 or blink_rate < 5: return DriverState.HEAVY_DROWSY elif perclos > 0.3 or blink_rate > 25: return DriverState.LIGHT_DROWSY if head_pose.get('pitch', 0) > 20: return DriverState.HEAVY_DROWSY return DriverState.ALERT def _handle_state_transition(self, new_state: DriverState, timestamp: float): """处理状态转换""" old_state = self.state if new_state in [DriverState.HEAVY_DROWSY, DriverState.UNRESPONSIVE]: if self.unresponsive_start_time is None: self.unresponsive_start_time = timestamp if new_state == DriverState.ALERT: self.unresponsive_start_time = None self.alert_count = 0 self.state = new_state def _execute_action(self, adas_data: Dict, timestamp: float) -> str: """执行对应动作""" if self.state == DriverState.ALERT: return 'MONITORING' elif self.state == DriverState.LIGHT_DROWSY: self.alert_manager.issue_alert('LEVEL_1_DROWSY') return 'LEVEL_1_ALERT' elif self.state == DriverState.HEAVY_DROWSY: self.alert_manager.issue_alert('LEVEL_2_DROWSY') self.alert_count += 1 duration = self._get_unresponsive_duration(timestamp) if duration > self.intervention_threshold: return self._initiate_intervention(adas_data) return 'LEVEL_2_ALERT' elif self.state == DriverState.UNRESPONSIVE: return self._initiate_intervention(adas_data) return 'UNKNOWN' def _get_unresponsive_duration(self, timestamp: float) -> float: """获取无响应持续时间""" if self.unresponsive_start_time is None: return 0 return timestamp - self.unresponsive_start_time def _initiate_intervention(self, adas_data: Dict) -> str: """ 启动干预程序 Euro NCAP要求的干预流程 """ safe_stop_plan = self.intervention_planner.plan_safe_stop( adas_data['lane_position'], adas_data['objects'], adas_data['road_type'] ) self.adas.execute_safe_stop(safe_stop_plan) self.adas.activate_hazard_lights() self.adas.unlock_doors() self.adas.call_emergency_services() return 'INTERVENTION_INITIATED'
class InterventionPlanner: """ 干预规划器 规划安全停车位置和路径 """ def plan_safe_stop(self, lane_position: float, objects: List[Dict], road_type: str) -> Dict: """ 规划安全停车 考虑因素: 1. 当前车道位置 2. 周围车辆 3. 道路类型(高速/城市道路) 4. 应急车道是否存在 """ if road_type == 'HIGHWAY': target_lane = 'EMERGENCY_LANE' target_speed = 0 deceleration_rate = 2.0 elif road_type == 'URBAN': target_lane = 'ROADSIDE' target_speed = 0 deceleration_rate = 3.0 else: target_lane = 'CURRENT_LANE' target_speed = 0 deceleration_rate = 2.5 safe_path = self._check_path_safety( lane_position, objects, target_lane ) return { 'target_lane': target_lane, 'target_speed': target_speed, 'deceleration_rate': deceleration_rate, 'safe_path': safe_path, 'estimated_stop_distance': self._estimate_stop_distance( deceleration_rate ) } def _check_path_safety(self, lane_position: float, objects: List[Dict], target_lane: str) -> bool: """检查路径安全性""" for obj in objects: if obj['lane'] == target_lane: return False return True def _estimate_stop_distance(self, deceleration: float) -> float: """估算停车距离""" current_speed = 27.8 stop_distance = current_speed ** 2 / (2 * deceleration) return stop_distance
class AlertManager: """ 警报管理器 """ def issue_alert(self, alert_type: str): """发出警报""" if alert_type == 'LEVEL_1_DROWSY': self._level_1_alert() elif alert_type == 'LEVEL_2_DROWSY': self._level_2_alert() def _level_1_alert(self): """一级警报""" print("[DMS] 一级疲劳警报:声音提醒") def _level_2_alert(self): """二级警报""" print("[DMS] 二级疲劳警报:声音+视觉+振动")
class ADASController: """ ADAS控制器 """ def execute_safe_stop(self, plan: Dict): """执行安全停车""" print(f"[ADAS] 执行安全停车:目标车道={plan['target_lane']}") def activate_hazard_lights(self): """开启双闪灯""" print("[ADAS] 开启双闪灯") def unlock_doors(self): """解锁车门""" print("[ADAS] 解锁车门") def call_emergency_services(self): """呼叫紧急服务""" print("[ADAS] 呼叫紧急救援")
if __name__ == "__main__": config = { 'light_drowsy_threshold': 5, 'heavy_drowsy_threshold': 10, 'unresponsive_threshold': 15, 'intervention_threshold': 20 } system = UnresponsiveDriverIntervention(config) dms_data = { 'perclos': 0.6, 'eyes_closed': False, 'blink_rate': 8, 'head_pose': {'pitch': 15} } adas_data = { 'lane_position': 0.0, 'objects': [], 'road_type': 'HIGHWAY' } result = system.process_frame(dms_data, adas_data, time.time()) print(f"驾驶员状态: {result['driver_state']}") print(f"执行动作: {result['action']}")
|