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
| """ Cabin Breath Alcohol Sensor - Multi-Point Sampling System 多点采样座舱呼吸酒精传感器模拟 """
import numpy as np from typing import Tuple, List from dataclasses import dataclass
@dataclass class SensorReading: """单传感器读数""" sensor_id: str alcohol_ppm: float timestamp: float temperature: float humidity: float airflow_speed: float
class CabinAlcoholDetector: """ 多点座舱酒精检测系统 传感器部署: - 驾驶员方向盘柱(近距离呼吸区) - 副驾驶侧(区分乘客来源) - 后视镜区域(环境基线) - HVAC 出风口(通风系统影响) 核心算法: 1. 梯度分析:驾驶员侧 vs 其他位置的浓度差 2. 时序模式:呼吸节律相关的浓度波动 3. 环境补偿:温度/湿度/气流修正 """ def __init__(self): self.sensors = ['steering', 'passenger', 'mirror', 'hvac'] self.driver_threshold_ppm = 15.0 self.gradient_threshold = 5.0 self.breath_pattern_freq = (0.1, 0.5) def analyze(self, readings: List[SensorReading]) -> dict: """ 分析多点传感器读数,判定驾驶员是否饮酒 Returns: dict: { 'driver_alcohol_detected': bool, 'estimated_bac': float, # g/dL 'confidence': float, 'contamination_flag': bool, } """ by_sensor = {} for r in readings: if r.sensor_id not in by_sensor: by_sensor[r.sensor_id] = [] by_sensor[r.sensor_id].append(r) driver_avg = np.mean([r.alcohol_ppm for r in by_sensor.get('steering', [])]) passenger_avg = np.mean([r.alcohol_ppm for r in by_sensor.get('passenger', [])]) env_avg = np.mean([r.alcohol_ppm for r in by_sensor.get('mirror', [])]) gradient = driver_avg - max(passenger_avg, env_avg) driver_series = [r.alcohol_ppm for r in by_sensor.get('steering', [])] if len(driver_series) > 10: freqs = np.fft.rfftfreq(len(driver_series), d=0.1) spectrum = np.abs(np.fft.rfft(driver_series)) breath_mask = (freqs >= self.breath_pattern_freq[0]) & \ (freqs <= self.breath_pattern_freq[1]) breath_power = np.sum(spectrum[breath_mask]) total_power = np.sum(spectrum) + 1e-10 breath_ratio = breath_power / total_power else: breath_ratio = 0 contamination = env_avg > 10.0 calibration = 0.003 estimated_bac = max(0, (driver_avg - env_avg) * calibration) is_detected = (gradient > self.gradient_threshold and breath_ratio > 0.15 and not contamination and estimated_bac > 0.02) confidence = min(1.0, gradient / 20.0 * 0.4 + breath_ratio * 2.0 * 0.6) return { 'driver_alcohol_detected': is_detected, 'estimated_bac': round(estimated_bac, 4), 'confidence': round(confidence, 3), 'contamination_flag': contamination, 'gradient': round(gradient, 2), 'breath_ratio': round(breath_ratio, 3), }
if __name__ == "__main__": detector = CabinAlcoholDetector() readings_driver = [ SensorReading('steering', 25 + 5*np.sin(2*np.pi*0.3*t*0.1) + np.random.randn()*2, t*0.1, 22, 45, 0.5) for t in range(50) ] readings_passenger = [ SensorReading('passenger', 8 + np.random.randn()*1, t*0.1, 22, 45, 0.3) for t in range(50) ] readings_env = [ SensorReading('mirror', 5 + np.random.randn()*1, t*0.1, 22, 45, 0.2) for t in range(50) ] result = detector.analyze(readings_driver + readings_passenger + readings_env) print("场景1 - 驾驶员饮酒:") print(f" 检测到: {result['driver_alcohol_detected']}") print(f" 估计BAC: {result['estimated_bac']:.4f}") print(f" 置信度: {result['confidence']:.1%}") readings_driver2 = [ SensorReading('steering', 7 + np.random.randn()*1, t*0.1, 22, 45, 0.5) for t in range(50) ] readings_passenger2 = [ SensorReading('passenger', 30 + np.random.randn()*3, t*0.1, 22, 45, 0.3) for t in range(50) ] readings_env2 = [ SensorReading('mirror', 12 + np.random.randn()*2, t*0.1, 22, 45, 0.2) for t in range(50) ] result2 = detector.analyze(readings_driver2 + readings_passenger2 + readings_env2) print("\n场景2 - 乘客饮酒,驾驶员清醒:") print(f" 检测到: {result2['driver_alcohol_detected']}") print(f" 估计BAC: {result2['estimated_bac']:.4f}") print(f" 环境污染: {result2['contamination_flag']}") readings_driver3 = [ SensorReading('steering', 80 + np.random.randn()*5, t*0.1, 22, 45, 0.5) for t in range(50) ] readings_env3 = [ SensorReading('mirror', 75 + np.random.randn()*5, t*0.1, 22, 45, 0.2) for t in range(50) ] result3 = detector.analyze(readings_driver3 + readings_env3) print("\n场景3 - 酒精洗手液污染:") print(f" 检测到: {result3['driver_alcohol_detected']}") print(f" 环境污染: {result3['contamination_flag']}")
|