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
| class CockpitEyeTrackingSuite: """ 航空座舱眼动追踪完整指标套件 7大指标计算与综合评估 """ def __init__(self, fps: int = 60): self.fps = fps self.gaze_analyzer = InstrumentGazeAnalyzer(fps) def compute_all_metrics( self, gaze_x: np.ndarray, gaze_y: np.ndarray, timestamps: np.ndarray, instrument_labels: np.ndarray, pupil_diameter: np.ndarray, blink_events: np.ndarray ) -> dict: """ 计算全部7个指标 Args: gaze_x, gaze_y: 注视点坐标序列 timestamps: 时间戳序列 instrument_labels: 各帧注视的仪表标签 pupil_diameter: 瞳孔直径序列 blink_events: 眨眼事件序列 Returns: metrics: 7大指标结果 """ m1 = self._metric_instrument_time(gaze_x, gaze_y, instrument_labels, timestamps) m2 = self._metric_eye_movement_distance(gaze_x, gaze_y) m3 = self._metric_visual_search_pattern(gaze_x, gaze_y, instrument_labels) m4 = self._metric_pupil_changes(pupil_diameter, timestamps) m5 = self._metric_blink_patterns(blink_events, timestamps) m6 = self._metric_attention_distribution(instrument_labels) m7 = self._metric_scan_speed(gaze_x, gaze_y, timestamps) return { 'instrument_time': m1, 'eye_movement_distance': m2, 'visual_search_pattern': m3, 'pupil_changes': m4, 'blink_patterns': m5, 'attention_distribution': m6, 'scan_speed': m7 } def _metric_eye_movement_distance(self, x: np.ndarray, y: np.ndarray) -> dict: """指标2: 眼动距离""" distances = np.sqrt(np.diff(x)**2 + np.diff(y)**2) total_distance = np.sum(distances) avg_distance = np.mean(distances) efficiency = 1.0 - min(avg_distance / 0.3, 1.0) return { 'total_distance': total_distance, 'avg_distance_per_saccade': avg_distance, 'efficiency_score': efficiency, 'interpretation': '高效' if efficiency > 0.7 else '需改进' } def _metric_visual_search_pattern(self, x, y, labels) -> dict: """指标3: 视觉搜索模式""" patterns = { 'speed': 0, 'vertical': 0, 'lateral': 0, 'general': 0 } transitions = {} for i in range(1, len(labels)): pair = (labels[i-1], labels[i]) transitions[pair] = transitions.get(pair, 0) + 1 patterns['speed'] = transitions.get(('ATTI', 'ASI'), 0) + transitions.get(('ASI', 'ATTI'), 0) patterns['vertical'] = transitions.get(('ATTI', 'ALT'), 0) + transitions.get(('ALT', 'ATTI'), 0) patterns['lateral'] = transitions.get(('ATTI', 'HSI'), 0) + transitions.get(('HSI', 'ATTI'), 0) dominant = max(patterns, key=patterns.get) return { 'patterns': patterns, 'dominant_pattern': dominant, 'structure_score': max(patterns.values()) / max(sum(patterns.values()), 1), 'is_structured': max(patterns.values()) > 10 } def _metric_pupil_changes(self, pupil: np.ndarray, ts: np.ndarray) -> dict: """指标4: 瞳孔直径变化""" from scipy.signal import detrend pupil_detrended = detrend(pupil) load = np.std(pupil_detrended) / (np.mean(pupil) + 1e-8) return { 'mean_diameter': np.mean(pupil), 'std_diameter': np.std(pupil), 'cognitive_load_index': load, 'high_load_flag': load > 0.15, 'max_dilation': np.max(pupil) - np.min(pupil) } def _metric_blink_patterns(self, blinks: np.ndarray, ts: np.ndarray) -> dict: """指标5: 眨眼模式""" total_sec = (ts[-1] - ts[0]) if len(ts) > 1 else 1 blink_rate = np.sum(blinks) / (total_sec / 60) return { 'blink_rate_per_min': blink_rate, 'total_blinks': int(np.sum(blinks)), 'fatigue_flag': blink_rate > 25, 'strain_flag': blink_rate < 10 } def _metric_attention_distribution(self, labels: np.ndarray) -> dict: """指标6: 注意力分配""" unique, counts = np.unique(labels, return_counts=True) dist = dict(zip(unique, counts / len(labels))) entropy = -sum(p * np.log2(p + 1e-10) for p in dist.values()) max_entropy = np.log2(len(unique)) if len(unique) > 0 else 1 return { 'distribution': dist, 'entropy': entropy, 'normalized_entropy': entropy / max_entropy if max_entropy > 0 else 0, 'tunnel_vision_flag': entropy / max_entropy < 0.5 if max_entropy > 0 else False } def _metric_scan_speed(self, x: np.ndarray, y: np.ndarray, ts: np.ndarray) -> dict: """指标7: 扫视频率""" velocities = np.sqrt(np.diff(x)**2 + np.diff(y)**2) / np.diff(ts + 1e-10) return { 'avg_scan_speed': np.mean(velocities), 'max_scan_speed': np.max(velocities), 'consistency': 1.0 - np.std(velocities) / (np.mean(velocities) + 1e-8) }
if __name__ == "__main__": np.random.seed(42) suite = CockpitEyeTrackingSuite(fps=60) n = 3600 ts = np.linspace(0, 60, n) instrument_positions = { 'ASI': (0.3, 0.4), 'ATTI': (0.5, 0.4), 'ALT': (0.7, 0.4), 'HSI': (0.5, 0.2), 'ECAM': (0.5, 0.7), 'PFD': (0.3, 0.5) } labels = np.random.choice(list(instrument_positions.keys()), n) gaze_x = np.array([instrument_positions[l][0] + np.random.normal(0, 0.02) for l in labels]) gaze_y = np.array([instrument_positions[l][1] + np.random.normal(0, 0.02) for l in labels]) pupil = 4.0 + 0.5 * np.sin(2 * np.pi * 0.1 * ts) + np.random.normal(0, 0.1, n) blinks = (np.random.rand(n) < 0.003).astype(float) metrics = suite.compute_all_metrics(gaze_x, gaze_y, ts, labels, pupil, blinks) print("=== 航空座舱眼动追踪 7大指标 ===\n") for name, result in metrics.items(): print(f"指标: {name}") for k, v in result.items(): if isinstance(v, (int, float, bool, str)): print(f" {k}: {v}") print()
|