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
| class EnhancedCabinFatigueModel: """ 借鉴航空方案的增强座舱疲劳检测模型 核心改进:从单一PERCLOS→多维融合+预防性 数据源(全部来自车载传感器,无需可穿戴): 1. 摄像头:PERCLOS + 眨眼频率 + 头部姿态 + 视线分散度 2. 方向盘ECG:HRV(当手握方向盘时) 3. rPPG摄像头:心率(替代ECG) 4. 驾驶行为:方向盘操作频率+车道保持+刹车平滑度 5. 上下文:驾驶时长+时间(昼夜节律)+历史数据 """ def __init__(self): self.weights = { 'perclos': 0.20, 'blink_frequency': 0.10, 'head_pose': 0.10, 'gaze_scatter': 0.10, 'hrv_proxy': 0.15, 'driving_behavior': 0.15, 'time_context': 0.10, 'history_trend': 0.10, } def compute_fatigue(self, features: dict) -> dict: """ 多维融合疲劳评分 Returns: { 'score': 0-100, 'level': 'normal'/'mild'/'moderate'/'severe', 'primary_indicator': '主要贡献因子', 'preventive_alert': bool # 预防性预警 } """ scores = {} perclos = features.get('perclos', 5) scores['perclos'] = min(100, perclos * 4) blink = features.get('blink_frequency', 17) scores['blink_frequency'] = min(100, abs(blink - 17) * 8) nod_count = features.get('head_nod_count_5min', 0) scores['head_pose'] = min(100, nod_count * 15) gaze_std = features.get('gaze_std_deg', 5) scores['gaze_scatter'] = min(100, max(0, (gaze_std - 15) * 5)) hrv_proxy = features.get('rppg_hrv_ms', 40) scores['hrv_proxy'] = max(0, (40 - hrv_proxy) / 20 * 100) lane_dev = features.get('lane_deviation_cm', 10) scores['driving_behavior'] = min(100, max(0, (lane_dev - 20) * 3)) drive_hours = features.get('drive_hours', 0) circadian = abs(features.get('circadian_phase', 0)) time_score = drive_hours * 8 + circadian * 15 scores['time_context'] = min(100, time_score) history = features.get('yesterday_sleep_hours', 7) scores['history_trend'] = max(0, (7 - history) / 3 * 100) total = sum(self.weights[k] * scores[k] for k in scores) primary = max(scores, key=scores.get) is_preventive = ( features.get('drive_hours', 0) > 2 and features.get('time_of_day', 12) in range(0, 6) ) if total < 25: level = 'normal' elif total < 45: level = 'mild' elif total < 65: level = 'moderate' else: level = 'severe' return { 'score': round(total, 1), 'level': level, 'primary_indicator': primary, 'preventive_alert': is_preventive, 'component_scores': {k: round(v, 1) for k, v in scores.items()} }
model = EnhancedCabinFatigueModel()
scenarios = { "精力充沛": { 'perclos': 3, 'blink_frequency': 17, 'head_nod_count_5min': 0, 'gaze_std_deg': 8, 'rppg_hrv_ms': 50, 'lane_deviation_cm': 8, 'drive_hours': 0.5, 'circadian_phase': 0, 'time_of_day': 14, 'yesterday_sleep_hours': 8 }, "长途夜间": { 'perclos': 12, 'blink_frequency': 25, 'head_nod_count_5min': 3, 'gaze_std_deg': 18, 'rppg_hrv_ms': 28, 'lane_deviation_cm': 25, 'drive_hours': 3.5, 'circadian_phase': -3, 'time_of_day': 3, 'yesterday_sleep_hours': 5 }, "微睡眠边缘": { 'perclos': 25, 'blink_frequency': 30, 'head_nod_count_5min': 8, 'gaze_std_deg': 25, 'rppg_hrv_ms': 20, 'lane_deviation_cm': 35, 'drive_hours': 4.5, 'circadian_phase': -3.5, 'time_of_day': 2, 'yesterday_sleep_hours': 4.5 } }
for name, features in scenarios.items(): result = model.compute_fatigue(features) print(f"\n{'='*50}") print(f"场景: {name}") print(f"总评分: {result['score']}/100 ({result['level']})") print(f"主要因子: {result['primary_indicator']}") print(f"预防性预警: {'是' if result['preventive_alert'] else '否'}") print("分项:") for k, v in result['component_scores'].items(): bar = '█' * int(v/5) print(f" {k:<22} {v:5.1f} {bar}")
|