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
| """ TDGV: 时间仪表盘视线方差 基于时间控制理论 (Moertl & Moore, 2018)
核心假设: - 驾驶是周期性控制任务,驾驶员通过仪表盘扫视维持控制 - 认知分心破坏时间控制节奏 → 方差变化 - TDGV = 仪表盘扫视时间间隔的方差
依赖: numpy, scipy """
import numpy as np from scipy import stats from typing import List, Tuple
class TDGVDetector: """ Temporal Dashboard Gaze Variance (TDGV) 认知分心检测器 算法流程: 1. 检测仪表盘扫视事件 (gaze enters dashboard AOI) 2. 计算连续扫视间的时间间隔 3. 在滑动窗口内计算时间间隔的方差 (TDGV) 4. 与基线比较,方差显著变化 = 认知分心 参考: Marx, C. et al. (2022). Sensors, 22(23), 9556. """ def __init__(self, sampling_rate: int = 60, window_size_sec: int = 60, baseline_window_sec: int = 300, dashboard_aoi: Tuple[float, float, float, float] = None): """ Args: sampling_rate: 眼动仪采样率 (Hz) window_size_sec: TDGV计算窗口 (秒) baseline_window_sec: 基线建立窗口 (秒) dashboard_aoi: 仪表盘AOI (x_min, y_min, x_max, y_max) 归一化坐标 """ self.fs = sampling_rate self.window_size = int(window_size_sec * sampling_rate) self.baseline_window = int(baseline_window_sec * sampling_rate) if dashboard_aoi is None: self.dashboard_aoi = (0.3, 0.5, 0.7, 0.9) else: self.dashboard_aoi = dashboard_aoi def detect_dashboard_gaze_events(self, gaze_x: np.ndarray, gaze_y: np.ndarray) -> np.ndarray: """ 检测仪表盘扫视事件 Args: gaze_x: (N,) 水平视线位置 (归一化 0-1) gaze_y: (N,) 垂直视线位置 (归一化 0-1) Returns: event_indices: 仪表盘扫视开始的帧索引 """ x_min, y_min, x_max, y_max = self.dashboard_aoi in_dashboard = ( (gaze_x >= x_min) & (gaze_x <= x_max) & (gaze_y >= y_min) & (gaze_y <= y_max) ) events = [] prev_in = False for i, curr_in in enumerate(in_dashboard): if curr_in and not prev_in: events.append(i) prev_in = curr_in return np.array(events) def calculate_tdgv(self, event_indices: np.ndarray) -> np.ndarray: """ 计算TDGV (时间仪表盘视线方差) 核心公式: intervals = diff(event_times) # 连续扫视的时间间隔 TDGV = variance(intervals) in sliding window Args: event_indices: 仪表盘扫视事件的帧索引 Returns: tdgv_values: (M,) TDGV值序列 """ if len(event_indices) < 3: return np.array([]) event_times = event_indices / self.fs intervals = np.diff(event_times) tdgv_values = [] for i in range(len(intervals) - self.window_size + 1): window = intervals[i:i + self.window_size] tdgv = np.var(window) tdgv_values.append(tdgv) return np.array(tdgv_values) def detect_cognitive_distraction(self, gaze_x: np.ndarray, gaze_y: np.ndarray) -> dict: """ 完整的认知分心检测流程 Args: gaze_x: (N,) 水平视线位置 gaze_y: (N,) 垂直视线位置 Returns: result: dict, 包含: - tdgv_baseline: 基线TDGV - tdgv_current: 当前TDGV - distraction_score: 分心分数 (0-1) - is_distracted: bool - confidence: 置信度 """ events = self.detect_dashboard_gaze_events(gaze_x, gaze_y) if len(events) < 10: return { 'tdgv_baseline': 0, 'tdgv_current': 0, 'distraction_score': 0, 'is_distracted': False, 'confidence': 0, 'message': 'Insufficient events' } tdgv = self.calculate_tdgv(events) if len(tdgv) < 2: return { 'tdgv_baseline': 0, 'tdgv_current': 0, 'distraction_score': 0, 'is_distracted': False, 'confidence': 0, 'message': 'Insufficient TDGV data' } baseline_end = min(self.baseline_window, len(tdgv)) tdgv_baseline = np.mean(tdgv[:baseline_end]) tdgv_current = np.mean(tdgv[-self.window_size:]) if tdgv_baseline > 0: distraction_score = abs( tdgv_current - tdgv_baseline ) / tdgv_baseline else: distraction_score = 0 if len(tdgv) >= 2 * self.window_size: baseline_data = tdgv[:self.window_size] current_data = tdgv[-self.window_size:] t_stat, p_value = stats.ttest_ind( baseline_data, current_data ) is_significant = p_value < 0.05 else: p_value = 1.0 is_significant = False is_distracted = distraction_score > 0.5 and is_significant return { 'tdgv_baseline': tdgv_baseline, 'tdgv_current': tdgv_current, 'distraction_score': min(distraction_score, 1.0), 'is_distracted': is_distracted, 'confidence': 1 - p_value, 'p_value': p_value, 'num_events': len(events), 'num_tdgv_samples': len(tdgv), }
if __name__ == "__main__": np.random.seed(42) detector = TDGVDetector( sampling_rate=60, window_size_sec=30, baseline_window_sec=120, ) print("=" * 60) print("TDGV 认知分心检测器测试") print("=" * 60) n_samples = 300 * 60 normal_gaze_x = np.random.normal(0.5, 0.15, n_samples) normal_gaze_y = np.random.normal(0.3, 0.12, n_samples) for t in range(0, n_samples, int(60 * np.random.uniform(3, 5))): duration = int(0.5 * 60) end = min(t + duration, n_samples) normal_gaze_x[t:end] = np.random.uniform(0.35, 0.65, end-t) normal_gaze_y[t:end] = np.random.uniform(0.55, 0.85, end-t) result_normal = detector.detect_cognitive_distraction( normal_gaze_x, normal_gaze_y ) print(f"\n正常驾驶:") for k, v in result_normal.items(): print(f" {k}: {v}") distracted_gaze_x = np.random.normal(0.5, 0.10, n_samples) distracted_gaze_y = np.random.normal(0.25, 0.08, n_samples) for t in range(0, n_samples, int(60 * np.random.uniform(1, 12))): duration = int(0.3 * 60) end = min(t + duration, n_samples) distracted_gaze_x[t:end] = np.random.uniform(0.35, 0.65, end-t) distracted_gaze_y[t:end] = np.random.uniform(0.55, 0.85, end-t) result_distracted = detector.detect_cognitive_distraction( distracted_gaze_x, distracted_gaze_y ) print(f"\n认知分心驾驶:") for k, v in result_distracted.items(): print(f" {k}: {v}") print(f"\n{'='*60}") print(f"对比分析:") print(f" 正常 TDGV基线: {result_normal['tdgv_baseline']:.4f}") print(f" 正常 TDGV当前: {result_normal['tdgv_current']:.4f}") print(f" 分心 TDGV基线: {result_distracted['tdgv_baseline']:.4f}") print(f" 分心 TDGV当前: {result_distracted['tdgv_current']:.4f}") print(f" 正常分心分数: {result_normal['distraction_score']:.4f}") print(f" 分心分心分数: {result_distracted['distraction_score']:.4f}") print(f" 正常是否分心: {result_normal['is_distracted']}") print(f" 分心是否分心: {result_distracted['is_distracted']}")
|