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
| import numpy as np from scipy.stats import entropy from typing import List, Tuple
class SaccadeEntropyAnalyzer: """ 扫视熵分析器 计算眼动扫视方向的熵值 认知分心时熵值降低(扫视更规律) """ def __init__(self, num_bins: int = 36): """ Args: num_bins: 扫视方向量化箱数(每10度一个箱) """ self.num_bins = num_bins self.history = [] def add_saccade(self, start: Tuple[float, float], end: Tuple[float, float]): """ 添加扫视事件 Args: start: 起始注视点 (x, y) end: 结束注视点 (x, y) """ dx = end[0] - start[0] dy = end[1] - start[1] angle = np.arctan2(dy, dx) amplitude = np.sqrt(dx**2 + dy**2) self.history.append({ 'angle': angle, 'amplitude': amplitude }) def compute_entropy(self, window_size: int = 50) -> float: """ 计算扫视熵 Args: window_size: 分析窗口大小 Returns: 扫视熵值(0-1,越高越不规律) """ if len(self.history) < window_size: return 1.0 recent = self.history[-window_size:] angles = [s['angle'] for s in recent] bins = np.linspace(-np.pi, np.pi, self.num_bins + 1) hist, _ = np.histogram(angles, bins=bins) prob = hist / hist.sum() max_entropy = np.log(self.num_bins) actual_entropy = entropy(prob + 1e-10) normalized = actual_entropy / max_entropy return normalized def detect_cognitive_distraction(self, threshold: float = 0.6) -> Tuple[bool, float]: """ 检测认知分心 熵值低于阈值表示认知分心 Returns: (是否分心, 熵值) """ entropy_value = self.compute_entropy() is_distracted = entropy_value < threshold return is_distracted, entropy_value
if __name__ == "__main__": analyzer = SaccadeEntropyAnalyzer() for _ in range(30): angle = np.random.uniform(-np.pi, np.pi) amplitude = np.random.uniform(50, 200) start = (640, 400) end = (640 + amplitude * np.cos(angle), 400 + amplitude * np.sin(angle)) analyzer.add_saccade(start, end) normal_entropy = analyzer.compute_entropy() print(f"正常驾驶熵值: {normal_entropy:.3f}") analyzer.history = [] for i in range(30): angle = np.random.normal(0, 0.3) amplitude = np.random.uniform(30, 80) start = (640, 400) end = (640 + amplitude * np.cos(angle), 400 + amplitude * np.sin(angle)) analyzer.add_saccade(start, end) distracted_entropy = analyzer.compute_entropy() print(f"认知分心熵值: {distracted_entropy:.3f}")
|