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
| import numpy as np from dataclasses import dataclass from typing import Dict, List, Optional from enum import Enum
class ImpairmentType(Enum): """损伤类型""" NONE = 0 FATIGUE = 1 ALCOHOL = 2 DRUG = 3 MEDICAL = 4
@dataclass class ImpairmentResult: """损伤检测结果""" impairment_type: ImpairmentType severity: float confidence: float features: Dict[str, float] timestamp: float
class SeeingMachinesImpairmentDetector: """ Seeing Machines风格损伤检测器 核心技术栈: 1. 眼动追踪(EOG替代) 2. 面部关键点分析 3. 行为模式识别 4. 多模态融合 """ def __init__(self, fps: int = 30): self.fps = fps self.alcohol_feature_weights = { 'saccade_latency': 0.25, 'nystagmus_score': 0.20, 'eye_openness_var': 0.15, 'pupil_response': 0.15, 'face_asymmetry': 0.10, 'behavior_anomaly': 0.15 } self.feature_history = { 'saccade_latency': [], 'eye_openness': [], 'pupil_diameter': [], 'face_asymmetry': [] } self.thresholds = { 'saccade_latency_normal': 0.22, 'saccade_latency_impaired': 0.35, 'nystagmus_threshold': 0.15, 'pupil_var_normal': 0.3, 'face_asymmetry_threshold': 0.08 } def detect( self, eye_features: Dict, face_features: Dict, behavior_features: Dict, timestamp: float ) -> ImpairmentResult: """ 综合损伤检测 Args: eye_features: 眼动特征 - saccade_latency: 扫视延迟 - eye_openness: 眼睑开度 - pupil_diameter: 瞳孔直径 - gaze_velocity: 视线速度 face_features: 面部特征 - landmarks: 关键点 - expression: 表情 behavior_features: 行为特征 - steering_variance: 方向盘方差 - lane_deviation: 车道偏离 Returns: ImpairmentResult: 检测结果 """ self._update_history(eye_features) scores = { ImpairmentType.FATIGUE: self._compute_fatigue_score(eye_features), ImpairmentType.ALCOHOL: self._compute_alcohol_score(eye_features, face_features), ImpairmentType.DRUG: self._compute_drug_score(eye_features, face_features), ImpairmentType.MEDICAL: self._compute_medical_score(behavior_features) } max_type = max(scores, key=scores.get) max_score = scores[max_type] if max_score < 0.3: max_type = ImpairmentType.NONE max_score = 0.0 return ImpairmentResult( impairment_type=max_type, severity=max_score, confidence=self._compute_confidence(), features=scores, timestamp=timestamp ) def _update_history(self, eye_features: Dict): """更新特征历史""" if 'saccade_latency' in eye_features: self.feature_history['saccade_latency'].append(eye_features['saccade_latency']) if len(self.feature_history['saccade_latency']) > 100: self.feature_history['saccade_latency'].pop(0) if 'eye_openness' in eye_features: self.feature_history['eye_openness'].append(eye_features['eye_openness']) if len(self.feature_history['eye_openness']) > 300: self.feature_history['eye_openness'].pop(0) def _compute_fatigue_score(self, eye_features: Dict) -> float: """计算疲劳分数""" score = 0.0 if len(self.feature_history['eye_openness']) > 90: closed_count = sum(1 for e in self.feature_history['eye_openness'][-90:] if e < 0.2) perclos = closed_count / 90 score += 0.5 * perclos if 'eye_openness' in eye_features: if eye_features['eye_openness'] < 0.5: score += 0.3 return min(score, 1.0) def _compute_alcohol_score(self, eye_features: Dict, face_features: Dict) -> float: """ 计算酒精损伤分数 关键特征: 1. 扫视延迟(最强指标) 2. 眼震 3. 瞳孔反应 4. 面部不对称 """ score = 0.0 if len(self.feature_history['saccade_latency']) > 10: mean_latency = np.mean(self.feature_history['saccade_latency'][-10:]) if mean_latency > self.thresholds['saccade_latency_impaired']: score += 0.4 elif mean_latency > self.thresholds['saccade_latency_normal']: ratio = (mean_latency - self.thresholds['saccade_latency_normal']) / \ (self.thresholds['saccade_latency_impaired'] - self.thresholds['saccade_latency_normal']) score += 0.2 * ratio if 'gaze_velocity' in eye_features: nystagmus_score = self._detect_nystagmus(eye_features['gaze_velocity']) if nystagmus_score > self.thresholds['nystagmus_threshold']: score += 0.25 if 'pupil_diameter' in eye_features: pupil_var = np.std(self.feature_history.get('pupil_diameter', [1])) if pupil_var < self.thresholds['pupil_var_normal']: score += 0.15 if 'asymmetry' in face_features: if face_features['asymmetry'] > self.thresholds['face_asymmetry_threshold']: score += 0.2 return min(score, 1.0) def _detect_nystagmus(self, gaze_velocity: float) -> float: """检测眼震""" return abs(gaze_velocity) if abs(gaze_velocity) > 0.1 else 0.0 def _compute_drug_score(self, eye_features: Dict, face_features: Dict) -> float: """计算药物损伤分数""" score = 0.0 if 'pupil_diameter' in eye_features: pupil = eye_features['pupil_diameter'] if pupil > 6.0 or pupil < 2.5: score += 0.3 return min(score, 1.0) def _compute_medical_score(self, behavior_features: Dict) -> float: """计算医疗突发分数""" score = 0.0 if 'steering_variance' in behavior_features: if behavior_features['steering_variance'] > 2.0: score += 0.3 if 'lane_deviation' in behavior_features: if behavior_features['lane_deviation'] > 0.5: score += 0.4 return min(score, 1.0) def _compute_confidence(self) -> float: """计算检测置信度""" data_amount = min(len(self.feature_history['saccade_latency']) / 50, 1.0) return data_amount
if __name__ == "__main__": detector = SeeingMachinesImpairmentDetector(fps=30) print("=" * 60) print("损伤检测测试") print("=" * 60) print("\n场景: 酒精损伤驾驶员") for i in range(100): eye_features = { 'saccade_latency': 0.38 + np.random.normal(0, 0.05), 'eye_openness': 0.7 + np.random.normal(0, 0.1), 'pupil_diameter': 5.0 + np.random.normal(0, 0.2), 'gaze_velocity': 0.15 + np.random.normal(0, 0.05) } face_features = { 'asymmetry': 0.12 + np.random.normal(0, 0.02) } behavior_features = { 'steering_variance': 1.5 + np.random.normal(0, 0.3), 'lane_deviation': 0.3 + np.random.normal(0, 0.1) } result = detector.detect(eye_features, face_features, behavior_features, i * 0.033) if i % 20 == 19: print(f" {i+1}帧: 类型={result.impairment_type.name}, " f"严重度={result.severity:.2f}, " f"置信度={result.confidence:.2f}")
|