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
| import numpy as np from typing import Dict, List, Tuple from dataclasses import dataclass from enum import Enum
class DriverState(Enum): """驾驶员状态枚举""" NORMAL = 0 DISTRACTED = 1 DROWSY = 2 UNRESPONSIVE = 3 MEDICAL_EMERGENCY = 4
@dataclass class DMSMetrics: """DMS监测指标""" eye_closure_ratio: float gaze_away_ratio: float blink_rate: float fixation_duration: float head_pose_change: float steering_interaction: float
class UnresponsiveDriverDetector: """无响应驾驶员检测器""" def __init__(self, config: Dict = None): self.config = config or {} self.thresholds = { 'eye_closure_duration': 5.0, 'gaze_static_duration': 8.0, 'head_pose_static': 10.0, 'no_steering': 30.0, 'lane_departure_no_reaction': 5.0 } self.history_buffer = [] self.buffer_size = 30 def detect(self, dms_metrics: DMSMetrics, adas_data: Dict) -> Dict: """ 检测无响应状态 Args: dms_metrics: DMS监测指标 adas_data: ADAS数据(车道偏离、方向盘扭矩等) Returns: result: 检测结果 """ eye_closure_detected = self._detect_eye_closure(dms_metrics) gaze_static_detected = self._detect_gaze_static(dms_metrics) head_static_detected = self._detect_head_static(dms_metrics) steering_detected = self._detect_no_steering(dms_metrics, adas_data) lane_departure_detected = self._detect_lane_departure_no_reaction(adas_data) unresponsive_score = self._calculate_unresponsive_score( eye_closure_detected, gaze_static_detected, head_static_detected, steering_detected, lane_departure_detected ) if unresponsive_score >= 0.8: state = DriverState.MEDICAL_EMERGENCY elif unresponsive_score >= 0.6: state = DriverState.UNRESPONSIVE elif unresponsive_score >= 0.4: state = DriverState.DROWSY elif unresponsive_score >= 0.2: state = DriverState.DISTRACTED else: state = DriverState.NORMAL return { 'state': state, 'score': unresponsive_score, 'indicators': { 'eye_closure': eye_closure_detected, 'gaze_static': gaze_static_detected, 'head_static': head_static_detected, 'no_steering': steering_detected, 'lane_departure_no_reaction': lane_departure_detected } } def _detect_eye_closure(self, metrics: DMSMetrics) -> bool: """检测眼睑闭合""" return metrics.eye_closure_ratio > 0.8 def _detect_gaze_static(self, metrics: DMSMetrics) -> bool: """检测视线静止""" return metrics.gaze_away_ratio < 0.05 and metrics.fixation_duration > 8.0 def _detect_head_static(self, metrics: DMSMetrics) -> bool: """检测头部静止""" return metrics.head_pose_change < 1.0 def _detect_no_steering(self, metrics: DMSMetrics, adas_data: Dict) -> bool: """检测方向盘无操作""" return metrics.steering_interaction < 0.1 def _detect_lane_departure_no_reaction(self, adas_data: Dict) -> bool: """检测车道偏离无反应""" return adas_data.get('lane_departure', False) and \ adas_data.get('time_since_departure', 0) > 5.0 def _calculate_unresponsive_score(self, *indicators) -> float: """计算无响应综合得分""" weights = [0.3, 0.25, 0.15, 0.15, 0.15] score = sum(w * float(ind) for w, ind in zip(weights, indicators)) return score
class ADASInterventionController: """ADAS干预控制器""" def __init__(self): self.current_state = DriverState.NORMAL self.warning_level = 0 self.intervention_active = False self.unresponsive_start_time = None self.warning_start_time = None def process_driver_state(self, detection_result: Dict, current_time: float) -> Dict: """ 处理驾驶员状态并触发干预 Args: detection_result: 检测结果 current_time: 当前时间戳 Returns: intervention: 干预动作 """ state = detection_result['state'] if state != self.current_state: self.current_state = state if state in [DriverState.UNRESPONSIVE, DriverState.MEDICAL_EMERGENCY]: self.unresponsive_start_time = current_time intervention = self._decide_intervention(current_time) return intervention def _decide_intervention(self, current_time: float) -> Dict: """决定干预动作""" if self.current_state not in [DriverState.UNRESPONSIVE, DriverState.MEDICAL_EMERGENCY]: return {'action': 'none', 'level': 0} elapsed = current_time - self.unresponsive_start_time if self.unresponsive_start_time else 0 if elapsed < 5: self.warning_level = 1 return { 'action': 'warning_level_1', 'level': 1, 'details': { 'audio_alert': 'loud_beep', 'visual_alert': 'red_flash', 'haptic': 'steering_vibration' } } elif elapsed < 15: self.warning_level = 2 return { 'action': 'warning_level_2', 'level': 2, 'details': { 'audio_alert': 'continuous_alarm', 'visual_alert': 'emergency_icon', 'haptic': 'seat_vibration', 'adas_activation': { 'lane_keeping': 'enhanced', 'speed_reduction': True, 'safe_stop_preparation': True } } } elif elapsed < 75: self.warning_level = 3 self.intervention_active = True return { 'action': 'emergency_stop', 'level': 3, 'details': { 'deceleration': 'gentle', 'lane_change': 'right', 'hazard_lights': True, 'emergency_call': True, 'door_unlock': True } } else: return { 'action': 'maintain_stop', 'level': 3, 'details': { 'hazard_lights': True, 'emergency_call': True, 'door_unlock': True } }
if __name__ == "__main__": detector = UnresponsiveDriverDetector() controller = ADASInterventionController() dms_metrics = DMSMetrics( eye_closure_ratio=0.9, gaze_away_ratio=0.02, blink_rate=2, fixation_duration=10.0, head_pose_change=0.5, steering_interaction=0.05 ) adas_data = { 'lane_departure': False, 'time_since_departure': 0 } result = detector.detect(dms_metrics, adas_data) print(f"驾驶员状态: {result['state'].name}") print(f"无响应得分: {result['score']:.2f}") import time current_time = time.time() intervention = controller.process_driver_state(result, current_time) print(f"\n干预动作: {intervention['action']}") print(f"干预级别: {intervention['level']}")
|