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
| """ 无响应驾驶员检测与干预系统 """
import numpy as np from typing import Dict, List from enum import Enum
class DriverState(Enum): """驾驶员状态""" ALERT = 'alert' DROWSY = 'drowsy' DISTRACTED = 'distracted' UNRESPONSIVE = 'unresponsive'
class UnresponsiveDriverIntervention: """ 无响应驾驶员干预系统 流程: 1. 检测驾驶员无响应 2. 发出警告 3. 尝试唤醒 4. 自动停车 """ def __init__(self): self.state = DriverState.ALERT self.unresponsive_duration = 0.0 self.warning_count = 0 self.unresponsive_threshold = 5.0 self.warning_interval = 2.0 self.intervention_level = 0 def update(self, dms_data: Dict, adas_data: Dict, dt: float) -> Dict: """ 更新状态 Args: dms_data: DMS数据 adas_data: ADAS数据 dt: 时间步长(秒) Returns: result: 干预决策 """ driver_state = self.analyze_driver_state(dms_data) self.update_state_machine(driver_state, dt) intervention = self.decide_intervention(dms_data, adas_data) return { 'state': self.state.value, 'intervention_level': intervention['level'], 'actions': intervention['actions'], 'unresponsive_duration': self.unresponsive_duration } def analyze_driver_state(self, dms_data: Dict) -> DriverState: """ 分析驾驶员状态 无响应判定条件: 1. 眼睛闭合 > 3秒 2. 头部低垂 3. 无手在方向盘 4. 无响应警告 """ ear = dms_data.get('ear', 0.5) eyes_closed = ear < 0.2 head_pose = dms_data.get('head_pose', (0, 0, 0)) head_down = head_pose[0] < -30 hands_on_wheel = dms_data.get('hands_on_wheel', True) if eyes_closed and head_down and not hands_on_wheel: return DriverState.UNRESPONSIVE elif eyes_closed or head_down: return DriverState.DROWSY elif not hands_on_wheel: return DriverState.DISTRACTED else: return DriverState.ALERT def update_state_machine(self, driver_state: DriverState, dt: float): """更新状态机""" if driver_state == DriverState.UNRESPONSIVE: self.unresponsive_duration += dt self.state = DriverState.UNRESPONSIVE elif driver_state == DriverState.DROWSY: self.unresponsive_duration = 0 self.state = DriverState.DROWSY elif driver_state == DriverState.DISTRACTED: self.unresponsive_duration = 0 self.state = DriverState.DISTRACTED else: self.unresponsive_duration = 0 self.state = DriverState.ALERT self.warning_count = 0 def decide_intervention(self, dms_data: Dict, adas_data: Dict) -> Dict: """ 决定干预级别 Level 0: 无干预 Level 1: 声音警告 Level 2: 振动警告 Level 3: 减速 + 警告 Level 4: 紧急停车 """ if self.state != DriverState.UNRESPONSIVE: return {'level': 0, 'actions': []} actions = [] if self.unresponsive_duration > 1.0: actions.append('audio_warning') self.intervention_level = 1 if self.unresponsive_duration > 3.0: actions.append('vibration_warning') self.intervention_level = 2 if self.unresponsive_duration > 5.0: actions.append('decelerate') actions.append('hazard_lights') self.intervention_level = 3 if self.unresponsive_duration > 8.0: actions.append('emergency_stop') actions.append('emergency_call') self.intervention_level = 4 return { 'level': self.intervention_level, 'actions': actions }
class ADASInterface: """ ADAS接口 与ADAS系统通信 """ def __init__(self): self.current_speed = 0.0 self.target_speed = 0.0 self.lane_keep_active = False self.emergency_brake_active = False def decelerate(self, deceleration: float): """减速""" self.target_speed = max(0, self.current_speed - deceleration) def emergency_stop(self): """紧急停车""" self.target_speed = 0 self.emergency_brake_active = True def enable_lane_keep(self): """启用车道保持""" self.lane_keep_active = True
class EmergencyCallSystem: """紧急呼叫系统""" def __init__(self): self.emergency_contacts = ['112', 'emergency_contact'] def call(self, location: Dict): """发起紧急呼叫""" print(f"紧急呼叫: {self.emergency_contacts}") print(f"位置: {location}")
class InterventionSystem: """完整干预系统""" def __init__(self): self.detector = UnresponsiveDriverIntervention() self.adas = ADASInterface() self.emergency = EmergencyCallSystem() def process(self, dms_data: Dict, adas_data: Dict, dt: float) -> Dict: """ 处理干预 Args: dms_data: DMS数据 adas_data: ADAS数据 dt: 时间步长 Returns: result: 处理结果 """ result = self.detector.update(dms_data, adas_data, dt) self.execute_intervention(result['actions'], adas_data) return result def execute_intervention(self, actions: List[str], adas_data: Dict): """执行干预动作""" for action in actions: if action == 'audio_warning': self.play_audio_warning() elif action == 'vibration_warning': self.trigger_vibration() elif action == 'decelerate': self.adas.decelerate(5.0) elif action == 'hazard_lights': self.activate_hazard_lights() elif action == 'emergency_stop': self.adas.emergency_stop() elif action == 'emergency_call': self.emergency.call(adas_data.get('location', {})) def play_audio_warning(self): """播放声音警告""" print("🔊 声音警告:请保持注意力!") def trigger_vibration(self): """触发振动""" print("📳 方向盘振动") def activate_hazard_lights(self): """激活危险警示灯""" print("⚠️ 危险警示灯开启")
if __name__ == "__main__": system = InterventionSystem() dms_data = { 'ear': 0.1, 'head_pose': (-45, 0, 0), 'hands_on_wheel': False } adas_data = { 'speed': 30, 'location': {'lat': 39.9, 'lon': 116.4} } for t in range(100): dt = 0.1 result = system.process(dms_data, adas_data, dt) if result['intervention_level'] > 0: print(f"[{t*0.1:.1f}s] 状态: {result['state']}, " f"干预级别: {result['intervention_level']}, " f"动作: {result['actions']}")
|