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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| """ 眼动规律性分析 """
import numpy as np from scipy import stats from typing import Dict, List
class GazeRegularityAnalyzer: """ 眼动规律性分析器 检测维度: 1. 扫视频率(Saccade Frequency) 2. 扫视幅度(Saccade Amplitude) 3. 凝视持续时间(Fixation Duration) 4. 扫视熵(Saccade Entropy) """ def __init__(self, window_size: int = 300): """ Args: window_size: 分析窗口(帧数,约10秒@30fps) """ self.window_size = window_size self.gaze_history = [] self.saccade_history = [] self.fixation_history = [] def analyze(self, gaze_sequence: np.ndarray) -> Dict: """ 分析眼动规律性 Args: gaze_sequence: 注视点序列 (N, 2) Returns: result: 分析结果 """ if len(gaze_sequence) < 60: return {'regularity_score': 0.5} saccades = self.detect_saccades(gaze_sequence) fixations = self.detect_fixations(gaze_sequence) saccade_frequency = len(saccades) / (len(gaze_sequence) / 30) saccade_amplitude = np.mean([s['amplitude'] for s in saccades]) if saccades else 0 fixation_duration = np.mean([f['duration'] for f in fixations]) if fixations else 0 saccade_entropy = self.calculate_saccade_entropy(saccades) regularity_score = self.calculate_regularity( saccade_frequency, saccade_amplitude, fixation_duration, saccade_entropy ) return { 'regularity_score': regularity_score, 'saccade_frequency': saccade_frequency, 'saccade_amplitude': saccade_amplitude, 'fixation_duration': fixation_duration, 'saccade_entropy': saccade_entropy, 'saccade_count': len(saccades), 'fixation_count': len(fixations) } def detect_saccades(self, gaze: np.ndarray, velocity_threshold: float = 100, duration_threshold: int = 3) -> List[Dict]: """ 检测扫视事件 Args: gaze: 注视点序列 (N, 2) velocity_threshold: 速度阈值 (pixels/frame) duration_threshold: 持续时间阈值 (frames) Returns: saccades: 扫视事件列表 """ velocity = np.linalg.norm(np.diff(gaze, axis=0), axis=1) saccades = [] i = 0 while i < len(velocity): if velocity[i] > velocity_threshold: start = i while i < len(velocity) and velocity[i] > velocity_threshold * 0.5: i += 1 end = i if end - start >= duration_threshold: amplitude = np.linalg.norm(gaze[end] - gaze[start]) saccades.append({ 'start_frame': start, 'end_frame': end, 'duration': end - start, 'amplitude': amplitude }) else: i += 1 return saccades def detect_fixations(self, gaze: np.ndarray, dispersion_threshold: float = 20, duration_threshold: int = 10) -> List[Dict]: """ 检测凝视事件 Args: gaze: 注视点序列 (N, 2) dispersion_threshold: 离散阈值 (pixels) duration_threshold: 持续时间阈值 (frames) Returns: fixations: 凝视事件列表 """ fixations = [] i = 0 while i < len(gaze): j = i + duration_threshold if j > len(gaze): break window = gaze[i:j] center = np.mean(window, axis=0) dispersion = np.max(np.linalg.norm(window - center, axis=1)) if dispersion < dispersion_threshold: while j < len(gaze): window = gaze[i:j+1] center = np.mean(window, axis=0) dispersion = np.max(np.linalg.norm(window - center, axis=1)) if dispersion >= dispersion_threshold: break j += 1 fixations.append({ 'start_frame': i, 'end_frame': j, 'duration': j - i, 'center': center }) i = j else: i += 1 return fixations def calculate_saccade_entropy(self, saccades: List[Dict]) -> float: """ 计算扫视熵 扫视方向分布的熵,用于评估扫视随机性 """ if len(saccades) < 5: return 0.0 directions = [] for saccade in saccades: direction = np.random.rand() * 2 * np.pi directions.append(direction) hist, _ = np.histogram(directions, bins=8, range=(0, 2*np.pi)) hist = hist / len(saccades) entropy = -np.sum(hist * np.log2(hist + 1e-10)) return entropy def calculate_regularity(self, frequency: float, amplitude: float, duration: float, entropy: float) -> float: """ 计算规律性评分 Returns: score: 规律性评分 [0, 1],越高越规律 """ normal_frequency = 3.0 normal_amplitude = 50 normal_duration = 10 freq_score = 1 - min(abs(frequency - normal_frequency) / normal_frequency, 1) amp_score = 1 - min(abs(amplitude - normal_amplitude) / normal_amplitude, 1) dur_score = 1 - min(abs(duration - normal_duration) / normal_duration, 1) entropy_score = 1 - min(entropy / 3.0, 1) score = 0.3 * freq_score + 0.3 * amp_score + 0.2 * dur_score + 0.2 * entropy_score return score
class CognitiveDistractionDetector: """ 认知分心检测器 融合: 1. 眼动规律性 2. 眨眼模式 3. 驾驶行为 """ def __init__(self): self.gaze_analyzer = GazeRegularityAnalyzer() self.blink_history = [] self.steering_history = [] def detect(self, gaze: np.ndarray, blink_rate: float, steering_angle: float) -> Dict: """ 检测认知分心 Args: gaze: 注视点序列 blink_rate: 眨眼频率 steering_angle: 方向盘角度 Returns: result: 检测结果 """ gaze_result = self.gaze_analyzer.analyze(gaze) blink_anomaly = self.detect_blink_anomaly(blink_rate) steering_anomaly = self.detect_steering_anomaly(steering_angle) cognitive_score = self.fuse_scores( gaze_result['regularity_score'], blink_anomaly, steering_anomaly ) is_cognitive_distracted = cognitive_score > 0.6 return { 'is_cognitive_distracted': is_cognitive_distracted, 'cognitive_score': cognitive_score, 'gaze_regularity': gaze_result['regularity_score'], 'blink_anomaly': blink_anomaly, 'steering_anomaly': steering_anomaly } def detect_blink_anomaly(self, blink_rate: float) -> float: """ 检测眨眼异常 认知分心时眨眼频率可能降低 """ normal_rate = 17.5 anomaly = abs(blink_rate - normal_rate) / normal_rate return min(anomaly, 1.0) def detect_steering_anomaly(self, steering_angle: float) -> float: """ 检测方向盘异常 认知分心时方向盘微调减少 """ self.steering_history.append(steering_angle) if len(self.steering_history) < 30: return 0.0 recent = self.steering_history[-30:] variation = np.std(recent) normal_variation = 2.0 anomaly = 1 - min(variation / normal_variation, 1) return anomaly def fuse_scores(self, gaze_regularity: float, blink_anomaly: float, steering_anomaly: float) -> float: """ 融合评分 """ weights = { 'gaze': 0.5, 'blink': 0.25, 'steering': 0.25 } gaze_score = 1 - gaze_regularity cognitive_score = ( weights['gaze'] * gaze_score + weights['blink'] * blink_anomaly + weights['steering'] * steering_anomaly ) return cognitive_score
if __name__ == "__main__": np.random.seed(42) normal_gaze = np.random.randn(300, 2) * 50 + np.array([640, 360]) distracted_gaze = np.random.randn(300, 2) * 10 + np.array([640, 360]) analyzer = GazeRegularityAnalyzer() print("正常驾驶:") normal_result = analyzer.analyze(normal_gaze) print(f" 规律性评分: {normal_result['regularity_score']:.2f}") print(f" 扫视频率: {normal_result['saccade_frequency']:.1f} Hz") print("\n认知分心:") distracted_result = analyzer.analyze(distracted_gaze) print(f" 规律性评分: {distracted_result['regularity_score']:.2f}") print(f" 扫视频率: {distracted_result['saccade_frequency']:.1f} Hz")
|