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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| import numpy as np from typing import Dict, Tuple from dataclasses import dataclass from enum import Enum
class FatigueLevel(Enum): """疲劳等级""" ALERT = 0 MILD_FATIGUE = 1 MODERATE_FATIGUE = 2 SEVERE_FATIGUE = 3 CRITICAL = 4
@dataclass class EyeMetrics: """眼动指标""" blink_rate: float pupil_diameter: float saccade_velocity: float fixation_duration: float sge: float blink_rate_change: float pupil_change: float saccade_change: float
class PilotFatigueModel: """飞行员疲劳评分模型 基于航空研究验证的眼动指标组合 参考:Frontiers in Neuroergonomics (2025) """ def __init__(self): self.baseline = { 'blink_rate': 18.0, 'pupil_diameter': 5.0, 'saccade_velocity': 250.0, 'fixation_duration': 250.0, 'sge': 0.7 } self.weights = { 'blink_rate': 0.25, 'pupil_diameter': 0.20, 'saccade_velocity': 0.25, 'fixation_duration': 0.15, 'sge': 0.15 } self.thresholds = { FatigueLevel.ALERT: 0.0, FatigueLevel.MILD_FATIGUE: 0.3, FatigueLevel.MODERATE_FATIGUE: 0.5, FatigueLevel.SEVERE_FATIGUE: 0.7, FatigueLevel.CRITICAL: 0.85 } def assess(self, metrics: EyeMetrics) -> Tuple[FatigueLevel, float, Dict]: """ 评估疲劳等级 Args: metrics: 眼动指标 Returns: level: 疲劳等级 score: 疲劳得分(0-1) details: 详细分析 """ deviations = self._calculate_deviations(metrics) score = sum( self.weights[key] * deviations[key] for key in self.weights.keys() ) level = self._classify_level(score) details = { 'deviations': deviations, 'primary_indicator': self._identify_primary_indicator(deviations), 'trend': self._analyze_trend(metrics), 'recommendation': self._generate_recommendation(level) } return level, score, details def _calculate_deviations(self, metrics: EyeMetrics) -> Dict: """计算各指标偏差""" deviations = {} blink_deviation = max(0, self.baseline['blink_rate'] - metrics.blink_rate) / self.baseline['blink_rate'] deviations['blink_rate'] = min(blink_deviation * 2, 1.0) pupil_deviation = max(0, self.baseline['pupil_diameter'] - metrics.pupil_diameter) / self.baseline['pupil_diameter'] deviations['pupil_diameter'] = min(pupil_deviation * 2, 1.0) saccade_deviation = max(0, self.baseline['saccade_velocity'] - metrics.saccade_velocity) / self.baseline['saccade_velocity'] deviations['saccade_velocity'] = min(saccade_deviation * 2, 1.0) fixation_deviation = max(0, metrics.fixation_duration - self.baseline['fixation_duration']) / self.baseline['fixation_duration'] deviations['fixation_duration'] = min(fixation_deviation, 1.0) sge_deviation = max(0, metrics.sge - self.baseline['sge']) / (1.0 - self.baseline['sge']) deviations['sge'] = min(sge_deviation, 1.0) return deviations def _classify_level(self, score: float) -> FatigueLevel: """分类疲劳等级""" if score < self.thresholds[FatigueLevel.MILD_FATIGUE]: return FatigueLevel.ALERT elif score < self.thresholds[FatigueLevel.MODERATE_FATIGUE]: return FatigueLevel.MILD_FATIGUE elif score < self.thresholds[FatigueLevel.SEVERE_FATIGUE]: return FatigueLevel.MODERATE_FATIGUE elif score < self.thresholds[FatigueLevel.CRITICAL]: return FatigueLevel.SEVERE_FATIGUE else: return FatigueLevel.CRITICAL def _identify_primary_indicator(self, deviations: Dict) -> str: """识别主要疲劳指标""" max_key = max(deviations, key=deviations.get) indicator_map = { 'blink_rate': '眨眼频率显著下降', 'pupil_diameter': '瞳孔直径持续收缩', 'saccade_velocity': '扫视速度明显减慢', 'fixation_duration': '注视时长异常增加', 'sge': '注视分布过度分散' } return indicator_map.get(max_key, '未知') def _analyze_trend(self, metrics: EyeMetrics) -> str: """分析趋势""" if metrics.blink_rate_change < -0.15: return "疲劳持续加重" elif metrics.blink_rate_change > 0.05: return "疲劳有所缓解" else: return "疲劳状态稳定" def _generate_recommendation(self, level: FatigueLevel) -> str: """生成建议""" recommendations = { FatigueLevel.ALERT: "状态良好,继续监控", FatigueLevel.MILD_FATIGUE: "建议适度休息,补充水分", FatigueLevel.MODERATE_FATIGUE: "建议15分钟内休息,调整座椅", FatigueLevel.SEVERE_FATIGUE: "建议立即停车休息,摄入咖啡因", FatigueLevel.CRITICAL: "危险状态,建议立即接管并安全停车" } return recommendations.get(level, "")
class MultiModalFatigueDetector: """多模态疲劳检测器 融合眼动、心率变异性、语音情绪 参考:美国空军研究实验室方案 """ def __init__(self): self.eye_model = PilotFatigueModel() self.hrv_baseline = {'rmssd': 50, 'lf_hf_ratio': 1.5} self.voice_baseline = {'pitch_variability': 20, 'speech_rate': 150} def detect(self, eye_metrics: EyeMetrics, hrv_data: Dict, voice_data: Dict) -> Dict: """ 多模态疲劳检测 Args: eye_metrics: 眼动指标 hrv_data: 心率变异性数据 voice_data: 语音情绪数据 Returns: result: 综合检测结果 """ eye_level, eye_score, eye_details = self.eye_model.assess(eye_metrics) hrv_score = self._assess_hrv(hrv_data) voice_score = self._assess_voice(voice_data) weights = {'eye': 0.5, 'hrv': 0.3, 'voice': 0.2} combined_score = ( weights['eye'] * eye_score + weights['hrv'] * hrv_score + weights['voice'] * voice_score ) combined_level = self.eye_model._classify_level(combined_score) return { 'fatigue_level': combined_level.name, 'fatigue_score': combined_score, 'modality_scores': { 'eye': eye_score, 'hrv': hrv_score, 'voice': voice_score }, 'eye_details': eye_details, 'confidence': self._calculate_confidence(eye_score, hrv_score, voice_score) } def _assess_hrv(self, hrv_data: Dict) -> float: """评估HRV疲劳指标""" rmssd_deviation = max(0, self.hrv_baseline['rmssd'] - hrv_data.get('rmssd', 50)) / self.hrv_baseline['rmssd'] lf_hf_deviation = max(0, hrv_data.get('lf_hf_ratio', 1.5) - self.hrv_baseline['lf_hf_ratio']) / (3.0 - self.hrv_baseline['lf_hf_ratio']) return min((rmssd_deviation + lf_hf_deviation) / 2, 1.0) def _assess_voice(self, voice_data: Dict) -> float: """评估语音疲劳指标""" pitch_deviation = max(0, self.voice_baseline['pitch_variability'] - voice_data.get('pitch_variability', 20)) / self.voice_baseline['pitch_variability'] rate_deviation = max(0, self.voice_baseline['speech_rate'] - voice_data.get('speech_rate', 150)) / self.voice_baseline['speech_rate'] return min((pitch_deviation + rate_deviation) / 2, 1.0) def _calculate_confidence(self, eye_score: float, hrv_score: float, voice_score: float) -> float: """计算置信度""" scores = [eye_score, hrv_score, voice_score] variance = np.var(scores) confidence = 1.0 - min(variance * 4, 0.5) return confidence
if __name__ == "__main__": eye_metrics = EyeMetrics( blink_rate=12.0, pupil_diameter=4.0, saccade_velocity=180.0, fixation_duration=350.0, sge=0.85, blink_rate_change=-0.20, pupil_change=-0.15, saccade_change=-0.25 ) model = PilotFatigueModel() level, score, details = model.assess(eye_metrics) print(f"疲劳等级: {level.name}") print(f"疲劳得分: {score:.2f}") print(f"主要指标: {details['primary_indicator']}") print(f"趋势分析: {details['trend']}") print(f"建议: {details['recommendation']}") detector = MultiModalFatigueDetector() hrv_data = {'rmssd': 35, 'lf_hf_ratio': 2.2} voice_data = {'pitch_variability': 12, 'speech_rate': 120} result = detector.detect(eye_metrics, hrv_data, voice_data) print(f"\n多模态融合结果:") print(f"疲劳等级: {result['fatigue_level']}") print(f"疲劳得分: {result['fatigue_score']:.2f}") print(f"置信度: {result['confidence']:.2f}") print(f"各模态得分: {result['modality_scores']}")
|