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
| """ 铁路司前筛查系统模拟实现
四模态并行采集 + 融合决策 """
import numpy as np from dataclasses import dataclass, field from typing import Optional, Tuple, List from enum import Enum
class HealthStatus(Enum): PASS = "通过" WARN = "警告" FAIL = "不通过"
class RiskFactor(Enum): NONE = 0 FEVER = 1 ALCOHOL = 2 HIGH_HR = 3 LOW_HRV = 4 ABNORMAL_RR = 5
@dataclass class ScreeningResult: """单次筛查结果""" subject_id: str = "" temperature: float = 0.0 heart_rate: float = 0.0 hrv_rmssd: float = 0.0 resp_rate: float = 0.0 bac_estimate: float = 0.0 status: HealthStatus = HealthStatus.PASS risk_factors: List[RiskFactor] = field(default_factory=list) screening_time: float = 0.0
class MultimodalScreeningSystem: """ 多模态无接触司前筛查系统 四模态并行采集 → 融合决策 → 通过/警告/不通过 设计目标: 30秒内完成全流程 """ THRESHOLDS = { 'temp_low': 35.5, 'temp_high': 37.3, 'hr_low': 50, 'hr_high': 100, 'hrv_low': 20, 'rr_low': 12, 'rr_high': 25, 'bac_limit': 0.02, 'screening_timeout': 30 } def __init__(self): self.results = ScreeningResult() def rfid_identify(self, card_id: str) -> str: """RFID 身份识别""" employee_db = { "EMP001": "张三", "EMP002": "李四", "EMP003": "王五" } return employee_db.get(card_id, "未知") def infrared_temperature(self, thermal_frame: np.ndarray) -> float: """ 红外测温 Args: thermal_frame: 红外热像帧, shape=(H, W) Returns: temperature: 额温 °C """ forehead_roi = thermal_frame[100:150, 150:250] max_temp = forehead_roi.max() calibrated = max_temp - 2.5 return float(calibrated) def rppg_measure(self, video_frames: np.ndarray) -> Tuple[float, float, float]: """ 面部 rPPG 生理测量 Args: video_frames: 面部视频, shape=(T, H, W, 3) Returns: (heart_rate, hrv_rmssd, resp_rate) """ T = len(video_frames) roi_signal = video_frames[:, 100:200, 150:250, 1].mean(axis=(1, 2)) from numpy.fft import fft, ifft, fftfreq fft_signal = fft(roi_signal - roi_signal.mean()) freqs = fftfreq(T, 1/30) hr_mask = (np.abs(freqs) >= 0.7) & (np.abs(freqs) <= 3.5) hr_spectrum = np.abs(fft_signal * hr_mask) hr_freq = freqs[np.argmax(hr_spectrum)] heart_rate = abs(hr_freq) * 60 if heart_rate > 0: ibi = 60.0 / heart_rate hrv_rmssd = np.std(np.diff(np.arange(0, T/30, ibi))) * 1000 else: hrv_rmssd = 0 resp_mask = (np.abs(freqs) >= 0.2) & (np.abs(freqs) <= 0.5) resp_spectrum = np.abs(fft_signal * resp_mask) resp_freq = freqs[np.argmax(resp_spectrum)] resp_rate = abs(resp_freq) * 60 return heart_rate, float(hrv_rmssd), resp_rate def alcohol_breath_test(self, breath_signal: np.ndarray) -> float: """ 非接触呼气酒精检测 Args: breath_signal: 呼气传感器信号 Returns: bac: 血液酒精浓度估计 (%) """ peak = np.max(breath_signal) bac = max(0, (peak - 0.1) * 0.01) return float(bac) def fuse_decision(self) -> ScreeningResult: """融合多模态结果做出决策""" r = self.results risk_factors = [] if r.temperature > self.THRESHOLDS['temp_high']: risk_factors.append(RiskFactor.FEVER) elif r.temperature < self.THRESHOLDS['temp_low']: risk_factors.append(RiskFactor.FEVER) if r.heart_rate > self.THRESHOLDS['hr_high']: risk_factors.append(RiskFactor.HIGH_HR) if r.hrv_rmssd < self.THRESHOLDS['hrv_low']: risk_factors.append(RiskFactor.LOW_HRV) if r.resp_rate < self.THRESHOLDS['rr_low'] or r.resp_rate > self.THRESHOLDS['rr_high']: risk_factors.append(RiskFactor.ABNORMAL_RR) if r.bac_estimate > self.THRESHOLDS['bac_limit']: risk_factors.append(RiskFactor.ALCOHOL) r.risk_factors = risk_factors if RiskFactor.ALCOHOL in risk_factors or RiskFactor.FEVER in risk_factors: r.status = HealthStatus.FAIL elif len(risk_factors) >= 2: r.status = HealthStatus.WARN else: r.status = HealthStatus.PASS return r def run_screening(self, card_id: str, thermal: np.ndarray, video: np.ndarray, breath: np.ndarray) -> ScreeningResult: """ 执行完整筛查流程 Args: card_id: 员工卡 ID thermal: 红外热像帧 video: 面部视频 breath: 呼气信号 Returns: result: 筛查结果 """ import time start = time.time() self.results.subject_id = self.rfid_identify(card_id) self.results.temperature = self.infrared_temperature(thermal) hr, hrv, rr = self.rppg_measure(video) self.results.heart_rate = hr self.results.hrv_rmssd = hrv self.results.resp_rate = rr self.results.bac_estimate = self.alcohol_breath_test(breath) self.fuse_decision() self.results.screening_time = time.time() - start return self.results
if __name__ == "__main__": system = MultimodalScreeningSystem() np.random.seed(42) thermal = np.random.normal(35, 2, (200, 300)) + 5 video = np.random.randint(0, 255, (300, 480, 640, 3), dtype=np.uint8) breath = np.random.normal(0.05, 0.02, 1000) result = system.run_screening("EMP001", thermal, video, breath) print("=== 铁路司前筛查结果 ===") print(f"员工: {result.subject_id}") print(f"体温: {result.temperature:.1f}°C") print(f"心率: {result.heart_rate:.1f} bpm") print(f"HRV (RMSSD): {result.hrv_rmssd:.1f} ms") print(f"呼吸率: {result.resp_rate:.1f} rpm") print(f"酒精浓度: {result.bac_estimate:.3f}%") print(f"风险因素: {[f.name for f in result.risk_factors]}") print(f"筛查结果: {result.status.value}") print(f"耗时: {result.screening_time:.1f}s") print("\n--- 酒驾场景测试 ---") breath_drunk = np.random.normal(0.15, 0.05, 1000) system2 = MultimodalScreeningSystem() result2 = system2.run_screening("EMP002", thermal, video, breath_drunk) print(f"员工: {result2.subject_id}") print(f"酒精浓度: {result2.bac_estimate:.3f}%") print(f"筛查结果: {result2.status.value}") print(f"风险因素: {[f.name for f in result2.risk_factors]}")
|