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
| class ImpairmentDetector: """ 损伤检测器 Euro NCAP 2026新增要求: - 10分钟内检测酒精/药物损伤 - 与驾驶员历史行为对比 - 区分损伤与疲劳 检测方法: - 眼动特征:扫视模式、注视稳定性 - 驾驶行为:方向盘操作、车道保持 - 历史对比:与基线行为比较 """ def __init__(self, baseline_window: int = 300, detection_threshold: float = 0.7): """ 初始化 Args: baseline_window: 基线建立窗口(秒) detection_threshold: 检测阈值 """ self.baseline_window = baseline_window self.detection_threshold = detection_threshold self.baseline = { 'gaze_variance': None, 'blink_rate': None, 'steering_entropy': None, 'lane_keeping_error': None } self.current_features = [] self.trip_start_time = None self.baseline_established = False def update(self, gaze_features: Dict, driving_features: Dict, timestamp: float, speed_kmh: float) -> Dict: """ 更新损伤状态 Args: gaze_features: 眼动特征 - gaze_variance: 注视点方差 - saccade_frequency: 扫视频率 - fixation_stability: 注视稳定性 driving_features: 驾驶特征 - steering_entropy: 方向盘熵 - lane_keeping_error: 车道保持误差 timestamp: 时间戳 speed_kmh: 车速 Returns: result: 检测结果 """ result = { 'impaired': False, 'confidence': 0.0, 'impairment_type': None, 'baseline_ready': False } if self.trip_start_time is None: self.trip_start_time = timestamp if speed_kmh < 50: return result trip_duration = timestamp - self.trip_start_time if trip_duration < self.baseline_window: self._update_baseline(gaze_features, driving_features) result['baseline_ready'] = False return result if not self.baseline_established: self._finalize_baseline() self.baseline_established = True result['baseline_ready'] = True impairment_score = self._calculate_impairment( gaze_features, driving_features ) result['confidence'] = impairment_score result['impaired'] = impairment_score > self.detection_threshold if result['impaired']: result['impairment_type'] = self._classify_impairment( gaze_features, driving_features ) return result def _update_baseline(self, gaze_features, driving_features): """更新基线数据""" self.current_features.append({ 'gaze_variance': gaze_features.get('gaze_variance', 0), 'saccade_frequency': gaze_features.get('saccade_frequency', 0), 'fixation_stability': gaze_features.get('fixation_stability', 0), 'steering_entropy': driving_features.get('steering_entropy', 0), 'lane_keeping_error': driving_features.get('lane_keeping_error', 0) }) def _finalize_baseline(self): """计算基线统计""" if not self.current_features: return features = np.array([ [f['gaze_variance'], f['saccade_frequency'], f['fixation_stability'], f['steering_entropy'], f['lane_keeping_error']] for f in self.current_features ]) self.baseline['gaze_variance'] = { 'mean': features[:, 0].mean(), 'std': features[:, 0].std() } self.baseline['saccade_frequency'] = { 'mean': features[:, 1].mean(), 'std': features[:, 1].std() } self.baseline['fixation_stability'] = { 'mean': features[:, 2].mean(), 'std': features[:, 2].std() } self.baseline['steering_entropy'] = { 'mean': features[:, 3].mean(), 'std': features[:, 3].std() } self.baseline['lane_keeping_error'] = { 'mean': features[:, 4].mean(), 'std': features[:, 4].std() } def _calculate_impairment(self, gaze_features, driving_features) -> float: """计算损伤评分""" scores = [] if self.baseline['gaze_variance']: current = gaze_features.get('gaze_variance', 0) mean = self.baseline['gaze_variance']['mean'] std = self.baseline['gaze_variance']['std'] if std > 0: z_score = abs(current - mean) / std scores.append(min(z_score / 3, 1.0)) if self.baseline['saccade_frequency']: current = gaze_features.get('saccade_frequency', 0) mean = self.baseline['saccade_frequency']['mean'] if mean > 0: ratio = current / mean scores.append(1 - ratio) if self.baseline['steering_entropy']: current = driving_features.get('steering_entropy', 0) mean = self.baseline['steering_entropy']['mean'] std = self.baseline['steering_entropy']['std'] if std > 0: z_score = (current - mean) / std scores.append(min(z_score / 3, 1.0)) return np.mean(scores) if scores else 0.0 def _classify_impairment(self, gaze_features, driving_features) -> str: """分类损伤类型""" gaze_variance = gaze_features.get('gaze_variance', 0) fixation_stability = gaze_features.get('fixation_stability', 0) if fixation_stability < 0.5: return 'ALCOHOL_SUSPECTED' if gaze_variance < 0.3: return 'DRUG_SUSPECTED' return 'IMPAIRMENT_UNKNOWN'
if __name__ == "__main__": detector = ImpairmentDetector() print("损伤检测测试:") print("-" * 50) for t in range(300): gaze = { 'gaze_variance': 0.5 + np.random.normal(0, 0.1), 'saccade_frequency': 3 + np.random.normal(0, 0.5), 'fixation_stability': 0.8 + np.random.normal(0, 0.1) } driving = { 'steering_entropy': 0.2 + np.random.normal(0, 0.05), 'lane_keeping_error': 0.1 + np.random.normal(0, 0.02) } detector.update(gaze, driving, t, 60) print("基线建立完成") for t in range(300, 600): gaze = { 'gaze_variance': 0.3 + np.random.normal(0, 0.05), 'saccade_frequency': 1.5 + np.random.normal(0, 0.3), 'fixation_stability': 0.4 + np.random.normal(0, 0.1) } driving = { 'steering_entropy': 0.4 + np.random.normal(0, 0.1), 'lane_keeping_error': 0.2 + np.random.normal(0, 0.05) } result = detector.update(gaze, driving, t, 60) if result['impaired']: print(f"t={t}s: 检测到损伤! 置信度={result['confidence']:.2f}, " f"类型={result['impairment_type']}")
|