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
| """ 认知分心眼动熵检测算法 基于Shannon熵计算眼动规律性
参考:Euro NCAP Driver Engagement Protocol 2026 """
import numpy as np from typing import Tuple, List from dataclasses import dataclass
@dataclass class GazeEntropyFeatures: """眼动熵特征""" spatial_entropy: float temporal_entropy: float gaze_stability: float entropy_drop_percent: float
class CognitiveDistractionDetector: """认知分心检测器(眼动熵方法)""" def __init__(self): self.entropy_drop_threshold = 20.0 self.gaze_duration_threshold = 5.0 def calculate_spatial_entropy( self, gaze_directions: np.ndarray ) -> float: """ 计算空间熵 Args: gaze_directions: 眼动方向序列 Returns: 空间熵值 """ unique, counts = np.unique(gaze_directions, return_counts=True) probabilities = counts / len(gaze_directions) entropy = -np.sum(probabilities * np.log2(probabilities + 1e-10)) return entropy def calculate_temporal_entropy( self, gaze_sequence: np.ndarray ) -> float: """ 计算时间熵 Args: gaze_sequence: 眼动时间序列 Returns: 时间熵值 """ intervals = np.diff(gaze_sequence) unique, counts = np.unique(intervals, return_counts=True) probabilities = counts / len(intervals) entropy = -np.sum(probabilities * np.log2(probabilities + 1e-10)) return entropy def detect_cognitive_distraction( self, gaze_directions: np.ndarray, baseline_entropy: float ) -> Tuple[bool, float]: """ 检测认知分心 Args: gaze_directions: 当前眼动方向 baseline_entropy: 正常驾驶基线熵 Returns: (是否认知分心, 熵值下降百分比) """ current_entropy = self.calculate_spatial_entropy(gaze_directions) entropy_drop = (baseline_entropy - current_entropy) / baseline_entropy * 100 is_cognitive_distraction = entropy_drop > self.entropy_drop_threshold return is_cognitive_distraction, entropy_drop
if __name__ == "__main__": detector = CognitiveDistractionDetector() normal_gaze = np.random.randint(0, 10, 100) distracted_gaze = np.random.randint(0, 3, 100) baseline_entropy = detector.calculate_spatial_entropy(normal_gaze) print("正常驾驶熵值:", baseline_entropy) print("认知分心熵值:", detector.calculate_spatial_entropy(distracted_gaze)) is_distraction, entropy_drop = detector.detect_cognitive_distraction( distracted_gaze, baseline_entropy ) print(f"熵值下降: {entropy_drop:.1f}%") print(f"认知分心检测: {is_distraction}")
|