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
| """ Euro NCAP 2026 损伤检测自适应基线建模 基于驾驶员历史行为的个性化损伤识别
算法依据: - Euro NCAP Safe Driving Protocol v1.1 Section 4.3 - NHTSA ANPRM 2024-01-05 Section B.2 """
import numpy as np from collections import deque from typing import Dict, Tuple, Optional from dataclasses import dataclass
@dataclass class DrivingBehaviorFeatures: """驾驶员行为特征向量(18维)""" steering_correction_freq: float steering_std_dev: float steering_correction_amplitude: float steering_overcorrection_ratio: float steering_hold_duration: float steering_reversal_freq: float gaze_road_ratio: float gaze_deviation_freq: float gaze_recovery_latency: float saccade_velocity_mean: float saccade_velocity_std: float fixation_duration_mean: float fcw_response_latency: float lane_correction_time: float speed_adjustment_latency: float accelerator_release_time: float eyelid_tremor_freq: float pupil_dilation_abnormal: float
class AdaptiveBaselineModel: """ 自适应基线建模 建立驾驶员个性化行为基线,用于损伤检测的相对偏差判定 算法核心: 1. 滑动窗口统计(最近N次驾驶) 2. 多维度基线均值±标准差 3. 异常检测:当前特征偏离基线>2σ 4. 时序累积:连续异常超过阈值触发损伤判定 """ def __init__(self, history_window_size: int = 20, anomaly_threshold_sigma: float = 2.0, accumulation_window_sec: int = 60): """ Args: history_window_size: 历史驾驶记录窗口大小(默认20次) anomaly_threshold_sigma: 异常判定阈值(默认2σ,95%置信区间) accumulation_window_sec: 异常累积窗口(默认60秒) """ self.window_size = history_window_size self.anomaly_threshold = anomaly_threshold_sigma self.accumulation_window = accumulation_window_sec self.driver_history: Dict[str, deque] = {} self.anomaly_accumulator: Dict[str, int] = {} def initialize_driver(self, driver_id: str): """初始化新驾驶员历史记录""" self.driver_history[driver_id] = deque(maxlen=self.window_size) self.anomaly_accumulator[driver_id] = 0 def update_baseline(self, driver_id: str, features: DrivingBehaviorFeatures) -> Tuple[bool, float]: """ 更新基线并检测异常 Args: driver_id: 驾驶员ID features: 当前驾驶行为特征 Returns: (is_anomaly, deviation_score): 是否异常,偏离分数 """ if driver_id not in self.driver_history: self.initialize_driver(driver_id) history = self.driver_history[driver_id] current_vec = np.array([ features.steering_correction_freq, features.steering_std_dev, features.steering_correction_amplitude, features.steering_overcorrection_ratio, features.steering_hold_duration, features.steering_reversal_freq, features.gaze_road_ratio, features.gaze_deviation_freq, features.gaze_recovery_latency, features.saccade_velocity_mean, features.saccade_velocity_std, features.fixation_duration_mean, features.fcw_response_latency, features.lane_correction_time, features.speed_adjustment_latency, features.accelerator_release_time, features.eyelid_tremor_freq, features.pupil_dilation_abnormal ]) if len(history) < 5: history.append(current_vec) return False, 0.0 history_matrix = np.array(list(history)) baseline_mean = np.mean(history_matrix, axis=0) baseline_std = np.std(history_matrix, axis=0) valid_dims = baseline_std > 0.001 deviation_scores = np.abs(current_vec[valid_dims] - baseline_mean[valid_dims]) / \ baseline_std[valid_dims] weights = np.array([1.5, 1.5, 1.2, 1.0, 0.8, 1.0, 1.5, 1.5, 1.2, 1.0, 0.8, 1.0, 1.8, 1.8, 1.5, 1.5, 0.5, 0.5]) total_deviation = np.mean(deviation_scores * weights[:len(deviation_scores)]) is_anomaly = total_deviation > self.anomaly_threshold if is_anomaly: self.anomaly_accumulator[driver_id] += 1 else: history.append(current_vec) self.anomaly_accumulator[driver_id] = max(0, self.anomaly_accumulator[driver_id] - 1) return is_anomaly, total_deviation def detect_impairment(self, driver_id: str) -> Tuple[bool, int]: """ 检测损伤状态 Args: driver_id: 驾驶员ID Returns: (is_impaired, anomaly_count): 是否损伤,异常累积次数 """ anomaly_count = self.anomaly_accumulator.get(driver_id, 0) threshold_count = int(self.accumulation_window / 2) is_impaired = anomaly_count >= threshold_count return is_impaired, anomaly_count
if __name__ == "__main__": np.random.seed(42) model = AdaptiveBaselineModel( history_window_size=20, anomaly_threshold_sigma=2.0, accumulation_window_sec=60 ) driver_id = "test_driver_001" model.initialize_driver(driver_id) print("建立正常驾驶基线...") for i in range(20): normal_features = DrivingBehaviorFeatures( steering_correction_freq=np.random.normal(0.8, 0.1), steering_std_dev=np.random.normal(2.5, 0.3), steering_correction_amplitude=np.random.normal(1.2, 0.2), steering_overcorrection_ratio=np.random.normal(0.05, 0.02), steering_hold_duration=np.random.normal(3.0, 0.5), steering_reversal_freq=np.random.normal(0.2, 0.05), gaze_road_ratio=np.random.normal(85.0, 5.0), gaze_deviation_freq=np.random.normal(0.3, 0.1), gaze_recovery_latency=np.random.normal(0.5, 0.1), saccade_velocity_mean=np.random.normal(120.0, 20.0), saccade_velocity_std=np.random.normal(30.0, 5.0), fixation_duration_mean=np.random.normal(2.5, 0.3), fcw_response_latency=np.random.normal(1.2, 0.2), lane_correction_time=np.random.normal(2.0, 0.3), speed_adjustment_latency=np.random.normal(1.5, 0.2), accelerator_release_time=np.random.normal(0.8, 0.1), eyelid_tremor_freq=np.random.normal(0.5, 0.1), pupil_dilation_abnormal=np.random.normal(0.1, 0.05) ) is_anomaly, deviation = model.update_baseline(driver_id, normal_features) print(f"基线建立完成,历史记录数:{len(model.driver_history[driver_id])}") print("\n模拟损伤驾驶...") for i in range(60): impaired_features = DrivingBehaviorFeatures( steering_correction_freq=np.random.normal(1.5, 0.3), steering_std_dev=np.random.normal(5.0, 0.8), steering_correction_amplitude=np.random.normal(3.0, 0.5), steering_overcorrection_ratio=np.random.normal(0.3, 0.1), steering_hold_duration=np.random.normal(1.5, 0.3), steering_reversal_freq=np.random.normal(0.8, 0.2), gaze_road_ratio=np.random.normal(60.0, 10.0), gaze_deviation_freq=np.random.normal(1.5, 0.3), gaze_recovery_latency=np.random.normal(2.0, 0.5), saccade_velocity_mean=np.random.normal(80.0, 15.0), saccade_velocity_std=np.random.normal(50.0, 10.0), fixation_duration_mean=np.random.normal(1.5, 0.3), fcw_response_latency=np.random.normal(3.5, 0.8), lane_correction_time=np.random.normal(5.0, 1.0), speed_adjustment_latency=np.random.normal(4.0, 0.8), accelerator_release_time=np.random.normal(2.5, 0.5), eyelid_tremor_freq=np.random.normal(2.0, 0.5), pupil_dilation_abnormal=np.random.normal(0.8, 0.2) ) is_anomaly, deviation = model.update_baseline(driver_id, impaired_features) if i % 10 == 0: impaired, count = model.detect_impairment(driver_id) print(f"时间{i}s: 异常={is_anomaly}, 偏离分数={deviation:.2f}, " f"累积异常={count}, 损伤判定={impaired}")
|