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
| import numpy as np from typing import Tuple, List
class AviationFatigueMetrics: """航空级疲劳指标计算器""" def __init__(self): self.aviation_thresholds = { 'perclos': 0.15, 'blink_rate_drop': 0.20, 'saccade_velocity_drop': 0.15, 'pupil_diameter_change': 0.10 } self.automotive_thresholds = { 'perclos': 0.20, 'blink_rate_drop': 0.30, 'saccade_velocity_drop': None, 'pupil_diameter_change': None } def calculate_saccade_velocity(self, gaze_sequence: np.ndarray, timestamps: np.ndarray) -> np.ndarray: """ 计算扫视速度 Args: gaze_sequence: (N, 2) 注视点序列 (x, y) timestamps: (N,) 时间戳 Returns: velocities: (N-1,) 扫视速度序列 """ displacement = np.sqrt(np.sum(np.diff(gaze_sequence, axis=0)**2, axis=1)) time_diff = np.diff(timestamps) velocities = displacement / time_diff return velocities def detect_fatigue_early_warning(self, gaze_data: np.ndarray, timestamps: np.ndarray) -> dict: """ 航空级疲劳早期预警 Args: gaze_data: (N, 4) 眼动数据 [x, y, pupil_diameter, blink_flag] timestamps: (N,) 时间戳 Returns: warning: 疲劳预警结果 """ saccade_velocities = self.calculate_saccade_velocity( gaze_data[:, :2], timestamps ) baseline_velocity = np.percentile(saccade_velocities[:int(0.1*len(saccade_velocities))], 50) current_velocity = np.percentile(saccade_velocities[-int(0.1*len(saccade_velocities)):], 50) velocity_drop = 1 - current_velocity / baseline_velocity blink_flags = gaze_data[:, 3] total_time = timestamps[-1] - timestamps[0] blink_rate = np.sum(blink_flags) / (total_time / 60) baseline_blink_rate = 20 blink_rate_drop = 1 - blink_rate / baseline_blink_rate eye_closure_ratio = np.mean(blink_flags) fatigue_level = self._calculate_fatigue_level( velocity_drop, blink_rate_drop, eye_closure_ratio ) return { 'fatigue_level': fatigue_level, 'saccade_velocity_drop': velocity_drop, 'blink_rate_drop': blink_rate_drop, 'perclos': eye_closure_ratio, 'early_warning': velocity_drop > self.aviation_thresholds['saccade_velocity_drop'] } def _calculate_fatigue_level(self, velocity_drop: float, blink_drop: float, perclos: float) -> str: """计算疲劳等级""" score = 0 if velocity_drop > self.aviation_thresholds['saccade_velocity_drop']: score += 1 if blink_drop > self.aviation_thresholds['blink_rate_drop']: score += 1 if perclos > self.aviation_thresholds['perclos']: score += 1 levels = ['清醒', '轻度疲劳', '中度疲劳', '重度疲劳'] return levels[min(score, 3)]
class ClosedLoopIntervention: """闭环干预系统(航空级)""" def __init__(self): self.intervention_history = [] self.interventions = { 'level_0': {'action': 'monitor', 'description': '持续监测'}, 'level_1': {'action': 'audio_alert', 'description': '声音警报'}, 'level_2': {'action': 'vibration', 'description': '座椅振动'}, 'level_3': {'action': 'vns_stimulation', 'description': '迷走神经刺激'}, 'level_4': {'action': 'auto_land', 'description': '自动降落'} } def decide_intervention(self, fatigue_level: str, context: dict) -> dict: """ 决定干预策略 Args: fatigue_level: 疲劳等级 context: 上下文(飞行阶段、高度等) Returns: intervention: 干预动作 """ level_map = { '清醒': 'level_0', '轻度疲劳': 'level_1', '中度疲劳': 'level_2', '重度疲劳': 'level_3' } intervention_level = level_map.get(fatigue_level, 'level_0') if fatigue_level == '重度疲劳' and context.get('phase') == 'cruise': intervention_level = 'level_4' intervention = self.interventions[intervention_level].copy() intervention['level'] = intervention_level intervention['timestamp'] = __import__('time').time() self.intervention_history.append(intervention) return intervention
if __name__ == "__main__": metrics = AviationFatigueMetrics() intervention = ClosedLoopIntervention() n_samples = 300 timestamps = np.linspace(0, 10, n_samples) gaze_normal = np.random.randn(n_samples, 4) gaze_normal[:, 3] = np.random.choice([0, 1], n_samples, p=[0.95, 0.05]) result_normal = metrics.detect_fatigue_early_warning(gaze_normal, timestamps) print(f"正常状态: {result_normal}") gaze_fatigue = np.random.randn(n_samples, 4) * 0.5 gaze_fatigue[:, 3] = np.random.choice([0, 1], n_samples, p=[0.98, 0.02]) result_fatigue = metrics.detect_fatigue_early_warning(gaze_fatigue, timestamps) print(f"\n疲劳状态: {result_fatigue}") intervention_result = intervention.decide_intervention( result_fatigue['fatigue_level'], {'phase': 'cruise', 'altitude': 35000} ) print(f"\n干预策略: {intervention_result}")
|