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
| """ 车辆健康监测管道 方向盘ECG + 安全带呼吸 + rPPG 融合
依赖: pip install numpy scipy """
import numpy as np from scipy.signal import butter, filtfilt, find_peaks from dataclasses import dataclass from typing import Optional, Tuple
@dataclass class HealthMetrics: """健康指标""" heart_rate: float hrv_sdnn: float respiratory_rate: float stress_index: float anomaly_flag: bool source: str
class SteeringWheelECG: """方向盘 ECG 传感器""" def __init__(self, sample_rate: int = 256): self.fs = sample_rate def generate(self, hr: float = 72, duration_sec: int = 30) -> Tuple[np.ndarray, bool]: """生成 ECG 信号""" t = np.arange(0, duration_sec, 1/self.fs) hands_on = np.random.random() > 0.15 if not hands_on: return np.zeros(len(t)), False rr = 60 / hr + np.random.normal(0, 0.04, int(duration_sec * hr / 60)) r_times = np.cumsum(rr) ecg = np.zeros(len(t)) for r_t in r_times: idx = int(r_t * self.fs) if 0 <= idx < len(ecg): ecg[idx] = 1.5 if idx > 5: ecg[idx-5] = -0.15 if idx+8 < len(ecg): ecg[idx+8] = -0.35 if idx+15 < len(ecg): ecg[idx+15] = 0.1 return ecg + np.random.normal(0, 0.02, len(ecg)), True def extract_hr_hrv(self, ecg: np.ndarray) -> Tuple[float, float]: """提取心率和 HRV""" peaks, _ = find_peaks(ecg, height=0.5, distance=0.3*self.fs) if len(peaks) < 3: return 0, 0 rr = np.diff(peaks) / self.fs hr = 60 / np.mean(rr) hrv = np.std(rr) * 1000 return hr, hrv
class SeatbeltRespiration: """安全带呼吸传感器""" def __init__(self, sample_rate: int = 50): self.fs = sample_rate def generate(self, resp_rate: float = 16, duration_sec: int = 30) -> np.ndarray: """生成呼吸信号""" t = np.arange(0, duration_sec, 1/self.fs) signal = np.sin(2 * np.pi * resp_rate / 60 * t) signal += 0.05 * np.random.randn(len(t)) return signal def extract_resp_rate(self, signal: np.ndarray) -> float: """提取呼吸频率""" fft = np.abs(np.fft.rfft(signal)) freqs = np.fft.rfftfreq(len(signal), 1/self.fs) resp_freq = freqs[np.argmax(fft)] return resp_freq * 60
class rPPGCamera: """rPPG 摄像头""" def __init__(self, sample_rate: int = 30): self.fs = sample_rate def generate(self, hr: float = 72, duration_sec: int = 30) -> np.ndarray: """生成 rPPG 信号""" t = np.arange(0, duration_sec, 1/self.fs) hr_freq = hr / 60 signal = 0.5 * np.sin(2 * np.pi * hr_freq * t) signal += 0.3 * np.sin(2 * np.pi * hr_freq * 2 * t) signal += 0.1 * np.random.randn(len(t)) signal += 0.2 * np.sin(2 * np.pi * 0.1 * t) return signal def extract_hr(self, signal: np.ndarray) -> Tuple[float, float]: """提取心率和置信度""" nyq = self.fs / 2 b, a = butter(2, [0.7/nyq, 3.0/nyq], btype='band') filtered = filtfilt(b, a, signal) fft = np.abs(np.fft.rfft(filtered)) freqs = np.fft.rfftfreq(len(filtered), 1/self.fs) peak_idx = np.argmax(fft) hr = freqs[peak_idx] * 60 confidence = fft[peak_idx] / (np.sum(fft) + 1e-8) * 100 return hr, confidence
class VehicleHealthMonitor: """车辆健康监测融合系统""" def __init__(self): self.ecg_sensor = SteeringWheelECG() self.resp_sensor = SeatbeltRespiration() self.rppg_sensor = rPPGCamera() self.baseline_hr = 72 self.baseline_hrv = 45 self.baseline_resp = 16 def monitor(self, duration_sec: int = 30) -> HealthMetrics: """监测健康状态""" ecg, hands_on = self.ecg_sensor.generate(72, duration_sec) resp = self.resp_sensor.generate(16, duration_sec) rppg = self.rppg_sensor.generate(72, duration_sec) ecg_hr, ecg_hrv = self.ecg_sensor.extract_hr_hrv(ecg) if hands_on else (0, 0) resp_rate = self.resp_sensor.extract_resp_rate(resp) rppg_hr, rppg_conf = self.rppg_sensor.extract_hr(rppg) if hands_on and ecg_hr > 0: hr = ecg_hr hrv = ecg_hrv source = 'ecg' else: hr = rppg_hr hrv = 0 source = 'rppg' hr_dev = abs(hr - self.baseline_hr) resp_dev = abs(resp_rate - self.baseline_resp) stress_index = min(100, hr_dev * 3 + resp_dev * 5) anomaly = (hr_dev > 20 or resp_dev > 8 or hr < 40 or hr > 120) return HealthMetrics( heart_rate=round(hr, 1), hrv_sdnn=round(hrv, 1), respiratory_rate=round(resp_rate, 1), stress_index=round(stress_index, 1), anomaly_flag=anomaly, source=source )
if __name__ == "__main__": print("=" * 70) print("车辆健康监测管道") print("方向盘ECG + 安全带呼吸 + rPPG") print("=" * 70) monitor = VehicleHealthMonitor() print("\n=== 正常驾驶 ===") result = monitor.monitor(30) print(f" 心率: {result.heart_rate} bpm ({result.source})") print(f" HRV: {result.hrv_sdnn} ms") print(f" 呼吸: {result.respiratory_rate} 次/分") print(f" 压力指数: {result.stress_index}/100") print(f" 异常: {'⚠️' if result.anomaly_flag else '✅'}") print(f"\n{'='*70}") print("传感器特性对比") print(f"{'='*70}") print(f"\n{'传感器':<15s} {'信号':<15s} {'精度':<10s} {'持续':<8s} {'接触':<8s}") print("-" * 55) print(f"{'方向盘ECG':<15s} {'心电':<15s} {'±5ms':<10s} {'间歇':<8s} {'接触':<8s}") print(f"{'安全带电极':<15s} {'呼吸':<15s} {'±2次/分':<10s} {'持续':<8s} {'接触':<8s}") print(f"{'rPPG摄像头':<15s} {'心率':<15s} {'±15ms':<10s} {'持续':<8s} {'非接触':<8s}") print(f"{'融合系统':<15s} {'全信号':<15s} {'最佳':<10s} {'持续':<8s} {'混合':<8s}") print(f"\n{'='*70}") print("IMS 部署路线") print(f"{'='*70}") print(""" Phase 1 (2026-2027): 方向盘ECG + 安全带呼吸 - 成本: +$5-8 (电极) - 精度: HR ±2bpm, 呼吸 ±1次/分 - 限制: 手离开方向盘时ECG中断
Phase 2 (2027-2028): + rPPG 摄像头 - 成本: 摄像头已有(DMS复用) - 精度: HR ±5bpm (rPPG备份) - 优势: ECG中断时无缝切换
Phase 3 (2028-2029): + AI 异常检测 - 功能: 心律失常/心率突变/呼吸异常 - 警报: 健康风险 → 靠边停车建议
Phase 4 (2029+): 完整健康监测 - 功能: 心血管/呼吸/压力/疲劳综合评估 - 价值: 预防性健康干预 """)
|