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
| """ 无响应驾驶员检测系统 """
import numpy as np from enum import Enum from dataclasses import dataclass from typing import Optional, List from collections import deque import time
class DriverState(Enum): """驾驶员状态""" NORMAL = "normal" ATTENTION_LOST = "attention_lost" UNRESPONSIVE = "unresponsive" EMERGENCY = "emergency"
class InterventionLevel(Enum): """干预等级""" NONE = 0 WARNING_1 = 1 WARNING_2 = 2 ADAS_TAKEOVER = 3 EMERGENCY_STOP = 4
@dataclass class DriverMetrics: """驾驶员指标""" eye_openness: float head_pitch: float head_yaw: float steering_activity: float pedal_activity: float response_to_warning: bool
class UnresponsiveDriverDetector: """ 无响应驾驶员检测器 Euro NCAP 2026要求: - 15秒内检测并触发干预 - 分级警告机制 - 紧急停车能力 """ def __init__(self): self.thresholds = { 'eye_closure': 0.2, 'head_pitch_down': 30, 'no_activity': 0.1, } self.time_thresholds = { 'eye_closure_duration': 5, 'head_down_duration': 3, 'no_activity_duration': 10, 'no_response_duration': 5, } self.current_state = DriverState.NORMAL self.state_start_time: Optional[float] = None self.warning_start_time: Optional[float] = None self.metrics_history = deque(maxlen=300) self.eye_closure_detector = EyeClosureDetector() self.head_pose_monitor = HeadPoseMonitor() self.activity_monitor = ActivityMonitor() def update(self, metrics: DriverMetrics) -> tuple: """ 更新检测状态 Args: metrics: 当前驾驶员指标 Returns: (state, intervention_level, info) """ current_time = time.time() self.metrics_history.append(metrics) eye_closed = self._detect_eye_closure(metrics) head_down = self._detect_head_down(metrics) no_activity = self._detect_no_activity(metrics) if eye_closed or head_down or no_activity: if self.state_start_time is None: self.state_start_time = current_time self.current_state = DriverState.ATTENTION_LOST duration = current_time - self.state_start_time if duration > self.time_thresholds['eye_closure_duration']: self.current_state = DriverState.UNRESPONSIVE else: self.state_start_time = None self.current_state = DriverState.NORMAL intervention = self._determine_intervention(metrics) info = { 'state': self.current_state.value, 'duration': current_time - self.state_start_time if self.state_start_time else 0, 'eye_closed': eye_closed, 'head_down': head_down, 'no_activity': no_activity, } return self.current_state, intervention, info def _detect_eye_closure(self, metrics: DriverMetrics) -> bool: """检测眼睛闭合""" return metrics.eye_openness < self.thresholds['eye_closure'] def _detect_head_down(self, metrics: DriverMetrics) -> bool: """检测头部下垂""" return metrics.head_pitch > self.thresholds['head_pitch_down'] def _detect_no_activity(self, metrics: DriverMetrics) -> bool: """检测无活动""" return (metrics.steering_activity < self.thresholds['no_activity'] and metrics.pedal_activity < self.thresholds['no_activity']) def _determine_intervention(self, metrics: DriverMetrics) -> InterventionLevel: """确定干预等级""" if self.current_state == DriverState.NORMAL: return InterventionLevel.NONE duration = time.time() - self.state_start_time if self.state_start_time else 0 if duration < 5: return InterventionLevel.WARNING_1 elif duration < 10: return InterventionLevel.WARNING_2 elif duration < 15: return InterventionLevel.ADAS_TAKEOVER else: return InterventionLevel.EMERGENCY_STOP
class EyeClosureDetector: """眼睛闭合检测器""" def __init__(self): self.perclos_window = deque(maxlen=150) def update(self, eye_openness: float) -> dict: """更新检测""" self.perclos_window.append(eye_openness < 0.2) perclos = sum(self.perclos_window) / len(self.perclos_window) * 100 return { 'perclos': perclos, 'closed': perclos > 80 }
class HeadPoseMonitor: """头部姿态监控器""" def __init__(self): self.pitch_history = deque(maxlen=90) def update(self, pitch: float) -> dict: """更新监控""" self.pitch_history.append(pitch) avg_pitch = sum(self.pitch_history) / len(self.pitch_history) return { 'avg_pitch': avg_pitch, 'head_down': avg_pitch > 30 }
class ActivityMonitor: """活动监控器""" def __init__(self): self.steering_history = deque(maxlen=300) self.pedal_history = deque(maxlen=300) def update(self, steering: float, pedal: float) -> dict: """更新监控""" self.steering_history.append(steering) self.pedal_history.append(pedal) steering_activity = np.std(list(self.steering_history)) pedal_activity = np.std(list(self.pedal_history)) return { 'steering_activity': steering_activity, 'pedal_activity': pedal_activity, 'no_activity': steering_activity < 0.1 and pedal_activity < 0.1 }
if __name__ == "__main__": detector = UnresponsiveDriverDetector() print("=== 无响应驾驶员检测测试 ===") print("\n[正常驾驶 5秒]") for i in range(150): metrics = DriverMetrics( eye_openness=0.8, head_pitch=5, head_yaw=0, steering_activity=0.5, pedal_activity=0.3, response_to_warning=False ) state, intervention, info = detector.update(metrics) if i % 50 == 0: print(f" {i//30}s: 状态={state.value}, 干预={intervention.value}") print("\n[无响应驾驶员 20秒]") for i in range(600): metrics = DriverMetrics( eye_openness=0.1 if i > 30 else 0.8, head_pitch=35 if i > 50 else 5, head_yaw=0, steering_activity=0.02, pedal_activity=0.01, response_to_warning=False ) state, intervention, info = detector.update(metrics) if intervention.value >= 1 and i % 30 == 0: print(f" {i//30}s: 状态={state.value}, 干预={intervention.name}, 时长={info['duration']:.1f}s")
|