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
| """ Euro NCAP 2026 酒驾检测框架 基于行为分析与历史对比 """
import numpy as np from enum import Enum from dataclasses import dataclass from typing import Optional, Dict, List from collections import deque
class ImpairmentType(Enum): """损伤类型""" NONE = "none" FATIGUE = "fatigue" ALCOHOL = "alcohol" UNKNOWN = "unknown"
@dataclass class BehavioralFeatures: """行为特征""" gaze_stability: float head_stability: float blink_regularity: float reaction_score: float lane_keeping_score: float steering_jerkiness: float
class ImpairmentDetector: """ Euro NCAP 2026 损伤检测器 功能: 1. 区分疲劳与酒驾 2. 基于历史驾驶模式对比 3. 多特征融合判断 """ def __init__(self): self.DETECTION_WINDOW = 600 self.SPEED_THRESHOLD = 50.0 self.baseline_features: Optional[Dict[str, float]] = None self.feature_history: deque = deque(maxlen=300) self.trip_start_time: Optional[float] = None def start_trip(self, timestamp: float): """开始新行程""" self.trip_start_time = timestamp self.feature_history.clear() def update(self, features: BehavioralFeatures, vehicle_speed: float, timestamp: float) -> Tuple[ImpairmentType, Optional[str]]: """ 更新损伤检测状态 Args: features: 行为特征 vehicle_speed: 车速 timestamp: 时间戳 Returns: (损伤类型, 警告类型) """ if vehicle_speed < self.SPEED_THRESHOLD: return ImpairmentType.NONE, None if self.trip_start_time is None: self.start_trip(timestamp) elapsed = timestamp - self.trip_start_time if elapsed > self.DETECTION_WINDOW: return ImpairmentType.NONE, None self.feature_history.append(features) if len(self.feature_history) < 90: return ImpairmentType.NONE, None stats = self._compute_statistics() deviation = self._compute_deviation(stats) impairment_type, warning = self._classify_impairment(stats, deviation) return impairment_type, warning def _compute_statistics(self) -> Dict[str, float]: """计算统计特征""" features = list(self.feature_history) return { 'gaze_stability_mean': np.mean([f.gaze_stability for f in features]), 'gaze_stability_std': np.std([f.gaze_stability for f in features]), 'head_stability_mean': np.mean([f.head_stability for f in features]), 'head_stability_std': np.std([f.head_stability for f in features]), 'blink_regularity': np.mean([f.blink_regularity for f in features]), 'reaction_score': np.mean([f.reaction_score for f in features]), 'steering_jerkiness': np.mean([f.steering_jerkiness for f in features]), } def _compute_deviation(self, stats: Dict[str, float]) -> Dict[str, float]: """计算与基线的偏差""" if self.baseline_features is None: self.baseline_features = { 'gaze_stability': 0.85, 'head_stability': 0.90, 'blink_regularity': 0.80, 'reaction_score': 0.85, 'steering_jerkiness': 0.20, } deviation = {} for key in stats: if key in self.baseline_features: deviation[key] = abs(stats[key] - self.baseline_features[key]) else: base_key = key.replace('_mean', '').replace('_std', '') if base_key in self.baseline_features: deviation[key] = abs(stats[key] - self.baseline_features[base_key]) return deviation def _classify_impairment(self, stats: Dict[str, float], deviation: Dict[str, float]) -> Tuple[ImpairmentType, Optional[str]]: """ 分类损伤类型 疲劳特征: - 视线稳定性下降但波动小 - 头部稳定性下降 - 眨眼规律性下降 酒驾特征: - 视线稳定性高度波动(std大) - 头部稳定性波动大 - 反应评分下降明显 - 方向盘抖动增加 """ gaze_std = stats.get('gaze_stability_std', 0) head_std = stats.get('head_stability_std', 0) steering_jerkiness = stats.get('steering_jerkiness', 0) blink_reg = stats.get('blink_regularity', 0) if gaze_std > 0.3 and head_std > 0.2: if steering_jerkiness > 0.5: return ImpairmentType.ALCOHOL, "ALCOHOL_IMPAIRMENT" if blink_reg < 0.5 and gaze_std < 0.2: return ImpairmentType.FATIGUE, "FATIGUE_DETECTED" if deviation.get('reaction_score', 0) > 0.3: return ImpairmentType.UNKNOWN, "IMPAIRMENT_DETECTED" return ImpairmentType.NONE, None
if __name__ == "__main__": detector = ImpairmentDetector() print("=== 场景1:正常驾驶 ===") detector.start_trip(0.0) for i in range(100): features = BehavioralFeatures( gaze_stability=0.85 + np.random.randn() * 0.05, head_stability=0.90 + np.random.randn() * 0.03, blink_regularity=0.80, reaction_score=0.85, lane_keeping_score=0.90, steering_jerkiness=0.15 + np.random.randn() * 0.05, ) imp_type, warning = detector.update(features, 60.0, i * 0.033) if warning: print(f"警告: {warning}") print(f"结果: {imp_type.value}") print("\n=== 场景2:酒驾模拟 ===") detector = ImpairmentDetector() detector.start_trip(0.0) for i in range(100): features = BehavioralFeatures( gaze_stability=0.5 + np.random.randn() * 0.35, head_stability=0.6 + np.random.randn() * 0.25, blink_regularity=0.7, reaction_score=0.5, lane_keeping_score=0.6, steering_jerkiness=0.6 + np.random.randn() * 0.1, ) imp_type, warning = detector.update(features, 60.0, i * 0.033) if warning: print(f"[{i*0.033:.1f}s] 检测到: {imp_type.value}, 警告: {warning}") print(f"最终结果: {imp_type.value}")
|