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
| import numpy as np from typing import Tuple, Optional from dataclasses import dataclass from enum import Enum
class SamplingLocation(Enum): """采样位置""" STEERING_WHEEL = "steering_wheel" DASHBOARD = "dashboard" ROOF_CONSOLE = "roof_console"
@dataclass class BreathSample: """呼吸样本""" ethanol_ppm: float co2_ppm: float temperature: float humidity: float flow_rate: float location: SamplingLocation timestamp: float
class DualSensorDADSS: """ 双传感器 DADSS 系统 结合呼吸和触摸检测 """ def __init__(self): self.breath_detector = EthanolDetector( optical_path_length=10.0 ) self.bac_threshold_warning = 20 self.bac_threshold_illegal = 50 self.driver_zone = { "x_range": (-0.5, 0.5), "y_range": (-0.3, 0.3), "z_range": (0.6, 1.2) } def distinguish_driver_breath( self, samples: list[BreathSample] ) -> list[BreathSample]: """ 区分驾驶员和乘客呼吸 通过多采样点的时间序列分析 Args: samples: 多个采样点的数据 Returns: driver_samples: 判定为驾驶员的样本 """ driver_samples = [] for sample in samples: if sample.location == SamplingLocation.STEERING_WHEEL: if sample.co2_ppm > 30000: driver_samples.append(sample) elif sample.location == SamplingLocation.DASHBOARD: if (sample.co2_ppm > 20000 and 30 < sample.temperature < 37): driver_samples.append(sample) return driver_samples def measure_breath_alcohol( self, samples: list[BreathSample] ) -> dict: """ 测量呼吸酒精 Args: samples: 多采样点数据 Returns: result: 测量结果 """ driver_samples = self.distinguish_driver_breath(samples) if not driver_samples: return { "status": "no_driver_breath_detected", "bac": 0, "confidence": 0 } ethanol_readings = [] co2_readings = [] for sample in driver_samples: result = self.breath_detector.detect_ethanol( sample.ethanol_ppm, sample.co2_ppm ) ethanol_readings.append(result["bac_mg_100mL"]) co2_readings.append(sample.co2_ppm) bac_median = np.median(ethanol_readings) bac_std = np.std(ethanol_readings) confidence = min(1.0, np.mean(co2_readings) / 40000) * (1 - bac_std / (bac_median + 1)) return { "status": "measured", "bac": round(bac_median, 1), "bac_std": round(bac_std, 2), "confidence": round(max(0, confidence), 2), "n_samples": len(driver_samples) } def measure_touch_alcohol( self, touch_data: dict ) -> dict: """ 触摸式酒精检测 Args: touch_data: 触摸传感器数据 { "nir_spectrum": 近红外光谱, "contact_quality": 接触质量 } Returns: result: 测量结果 """ spectrum = touch_data.get("nir_spectrum", {}) contact_quality = touch_data.get("contact_quality", 0) ethanol_signal = spectrum.get("absorbance_1410nm", 0) water_signal = spectrum.get("absorbance_1450nm", 1) normalized_signal = ethanol_signal / water_signal bac_estimate = normalized_signal * 200 - 10 bac_estimate = max(0, bac_estimate) confidence = contact_quality * 0.9 return { "status": "measured", "bac": round(bac_estimate, 1), "confidence": round(confidence, 2), "method": "tissue_spectroscopy" } def fusion_decision( self, breath_result: dict, touch_result: Optional[dict] = None ) -> dict: """ 融合决策 Args: breath_result: 呼吸检测结果 touch_result: 触摸检测结果(可选) Returns: decision: { "final_bac": 最终 BAC, "intervention": 干预措施, "confidence": 置信度 } """ if touch_result and touch_result["status"] == "measured": breath_weight = breath_result["confidence"] touch_weight = touch_result["confidence"] total_weight = breath_weight + touch_weight if total_weight > 0: final_bac = ( breath_result["bac"] * breath_weight + touch_result["bac"] * touch_weight ) / total_weight else: final_bac = breath_result["bac"] confidence = total_weight / 2 else: final_bac = breath_result["bac"] confidence = breath_result["confidence"] if final_bac >= self.bac_threshold_illegal: intervention = "block_ignition" elif final_bac >= self.bac_threshold_warning: intervention = "warn_driver" else: intervention = "none" return { "final_bac": round(final_bac, 1), "intervention": intervention, "confidence": round(confidence, 2), "breath_bac": breath_result["bac"], "touch_bac": touch_result["bac"] if touch_result else None }
if __name__ == "__main__": dadss = DualSensorDADSS() samples = [ BreathSample( ethanol_ppm=150, co2_ppm=38000, temperature=34.5, humidity=85, flow_rate=0.5, location=SamplingLocation.STEERING_WHEEL, timestamp=0.0 ), BreathSample( ethanol_ppm=80, co2_ppm=25000, temperature=33.0, humidity=80, flow_rate=0.3, location=SamplingLocation.DASHBOARD, timestamp=0.5 ) ] breath_result = dadss.measure_breath_alcohol(samples) print("呼吸检测结果:") print(f" BAC: {breath_result['bac']} mg/100mL") print(f" 置信度: {breath_result['confidence']}") touch_data = { "nir_spectrum": { "absorbance_1410nm": 0.35, "absorbance_1450nm": 0.90 }, "contact_quality": 0.85 } touch_result = dadss.measure_touch_alcohol(touch_data) print(f"\n触摸检测结果:") print(f" BAC: {touch_result['bac']} mg/100mL") print(f" 置信度: {touch_result['confidence']}") decision = dadss.fusion_decision(breath_result, touch_result) print(f"\n融合决策:") print(f" 最终 BAC: {decision['final_bac']} mg/100mL") print(f" 干预措施: {decision['intervention']}") print(f" 置信度: {decision['confidence']}")
|