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
| """ 可穿戴+车载多模态疲劳融合系统
基于 arXiv:2412.16847 综述方法 融合: PPG心率(HRV) + 加速度(活动量) + DMS PERCLOS + 雷达呼吸率 依赖: pip install numpy scipy torch """
import numpy as np from scipy.signal import butter, filtfilt from typing import Dict, Tuple, Optional from dataclasses import dataclass
@dataclass class WearableData: """可穿戴设备数据包""" timestamp: float heart_rate: float hrv_rmssd: float hrv_sdnn: float steps_per_min: float spo2: float skin_temp: float sleep_quality_last_night: float device_type: str
@dataclass class CabinData: """车载传感器数据包""" timestamp: float perclos: float blink_rate: float eye_openness: float gaze_entropy: float breathing_rate: float steering_jerk: float lane_deviation: float
class WearableCabinFatigueFusion: """ 可穿戴+车载疲劳融合模型 方法: 时序LSTM + 注意力机制 参考: arXiv:2412.16847 (2024) 优势: - 可穿戴提供生理基线(夜间睡眠质量) - 车载提供实时状态 - 融合后可区分疲劳原因(睡眠不足vs驾驶疲劳) """ def __init__(self): self.weights = { 'perclos': 0.20, 'hrv_rmssd': 0.15, 'breathing_rate': 0.10, 'gaze_entropy': 0.15, 'blink_rate': 0.10, 'steering_jerk': 0.10, 'sleep_quality': 0.10, 'lane_deviation': 0.05, 'spo2': 0.05, } self.baseline = { 'hr': 72, 'hrv': 45, 'br': 16, 'perclos': 0.08, 'blink_rate': 18, } self.baseline_initialized = False def update_baseline(self, history: list): """从历史数据更新个人基线""" if len(history) < 10: return hr_vals = [d.heart_rate for d in history if d.heart_rate > 0] hrv_vals = [d.hrv_rmssd for d in history if d.hrv_rmssd > 0] br_vals = [d.breathing_rate for d in history if d.breathing_rate > 0] if hr_vals: self.baseline['hr'] = np.median(hr_vals) self.baseline['hrv'] = np.median(hrv_vals) self.baseline['br'] = np.median(br_vals) self.baseline_initialized = True def compute_fatigue(self, wearable: WearableData, cabin: CabinData) -> Dict: """ 计算综合疲劳分数 Returns: { 'fatigue_score': 0-100, 'level': 'normal'/'mild'/'moderate'/'severe', 'fatigue_type': 'sleep_deprivation'/'driving_fatigue'/'mixed', 'components': 各项得分, 'confidence': 置信度 } """ perclos_score = min(cabin.perclos / 0.3, 1.0) if self.baseline_initialized: hrv_ratio = wearable.hrv_rmssd / (self.baseline['hrv'] + 1e-6) hrv_score = max(0, 1 - hrv_ratio) else: hrv_score = max(0, 1 - wearable.hrv_rmssd / 50) br_expected = self.baseline.get('br', 16) br_score = max(0, (br_expected - cabin.breathing_rate) / br_expected) gaze_score = max(0, 1 - cabin.gaze_entropy / 4.0) blink_baseline = self.baseline.get('blink_rate', 18) blink_score = min(cabin.blink_rate / (blink_baseline * 2), 1.0) steering_score = min(cabin.steering_jerk / 500, 1.0) sleep_score = 1 - wearable.sleep_quality_last_night lane_score = min(cabin.lane_deviation / 0.5, 1.0) spo2_score = max(0, (96 - wearable.spo2) / 5) components = { 'perclos': perclos_score, 'hrv': hrv_score, 'breathing': br_score, 'gaze_entropy': gaze_score, 'blink': blink_score, 'steering': steering_score, 'sleep': sleep_score, 'lane': lane_score, 'spo2': spo2_score, } fatigue_score = sum( self.weights[k] * v for k, v in components.items() ) * 100 if sleep_score > 0.5 and wearable.sleep_quality_last_night < 0.3: fatigue_type = 'sleep_deprivation' elif steering_score > 0.5 and perclos_score > 0.5: fatigue_type = 'driving_fatigue' else: fatigue_type = 'mixed' if fatigue_score < 25: level = 'normal' elif fatigue_score < 50: level = 'mild' elif fatigue_score < 75: level = 'moderate' else: level = 'severe' n_valid = sum(1 for v in components.values() if v > 0) confidence = min(n_valid / 9, 1.0) return { 'fatigue_score': round(fatigue_score, 1), 'level': level, 'fatigue_type': fatigue_type, 'components': {k: round(v, 2) for k, v in components.items()}, 'confidence': round(confidence, 2), }
if __name__ == "__main__": np.random.seed(42) fusion = WearableCabinFatigueFusion() normal_wearable = WearableData( timestamp=0, heart_rate=72, hrv_rmssd=45, hrv_sdnn=50, steps_per_min=0, spo2=98, skin_temp=33.5, sleep_quality_last_night=0.85, device_type='watch' ) normal_cabin = CabinData( timestamp=0, perclos=0.08, blink_rate=18, eye_openness=0.9, gaze_entropy=3.5, breathing_rate=16, steering_jerk=100, lane_deviation=0.1 ) fatigue_wearable = WearableData( timestamp=0, heart_rate=65, hrv_rmssd=15, hrv_sdnn=20, steps_per_min=0, spo2=94, skin_temp=35.0, sleep_quality_last_night=0.3, device_type='watch' ) fatigue_cabin = CabinData( timestamp=0, perclos=0.25, blink_rate=35, eye_openness=0.6, gaze_entropy=2.0, breathing_rate=12, steering_jerk=350, lane_deviation=0.4 ) print("=== 正常驾驶 ===") result_n = fusion.compute_fatigue(normal_wearable, normal_cabin) print(f"疲劳分数: {result_n['fatigue_score']}/100 ({result_n['level']})") print(f"疲劳类型: {result_n['fatigue_type']}") print(f"置信度: {result_n['confidence']:.0%}") print("各分量:") for k, v in result_n['components'].items(): print(f" {k}: {v:.2f}") print(f"\n=== 疲劳驾驶 ===") result_f = fusion.compute_fatigue(fatigue_wearable, fatigue_cabin) print(f"疲劳分数: {result_f['fatigue_score']}/100 ({result_f['level']})") print(f"疲劳类型: {result_f['fatigue_type']}") print(f"置信度: {result_f['confidence']:.0%}") print("各分量:") for k, v in result_f['components'].items(): print(f" {k}: {v:.2f}") print(f"\n=== 差异 ===") print(f"分数差: {result_f['fatigue_score'] - result_n['fatigue_score']:.1f}") print(f"最大贡献: HRV({result_f['components']['hrv']}) + " f"PERCLOS({result_f['components']['perclos']}) + " f"睡眠({result_f['components']['sleep']})")
|