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
| import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA
class EDrunkDrivingSystem: """ e-Nose辅助多信号融合酒驾检测系统 论文: Sensors and Actuators B: Chemical (2025) 灵敏度: 97.14%, 准确率: 98.67% """ MOS_SENSORS = { "MQ-3": {"target": "酒精(C2H5OH)", "range": "0.05-10mg/L", "sensitivity": 0.5}, "MQ-135": {"target": "氨气/CO2", "range": "10-1000ppm", "sensitivity": 0.3}, "MQ-2": {"target": "烟雾/可燃气体", "range": "100-10000ppm", "sensitivity": 0.4}, "MQ-7": {"target": "一氧化碳CO", "range": "20-2000ppm", "sensitivity": 0.2}, "MQ-4": {"target": "甲烷CH4", "range": "200-10000ppm", "sensitivity": 0.3}, "MQ-8": {"target": "氢气H2", "range": "100-1000ppm", "sensitivity": 0.4}, "MQ-9": {"target": "CO/可燃气体", "range": "100-10000ppm", "sensitivity": 0.3}, "MQ-131": {"target": "臭氧O3", "range": "10-1000ppb", "sensitivity": 0.2} } def __init__(self): self.scaler = StandardScaler() self.pca = PCA(n_components=12) self.classifier = RandomForestClassifier( n_estimators=200, max_depth=15, random_state=42 ) def extract_gas_features(self, sensor_readings): """ 从8个MOS传感器读数中提取特征 Args: sensor_readings: shape=(N, 8), 各传感器响应值 Returns: features: 提取的特征向量 """ features = [] for i in range(sensor_readings.shape[1]): sig = sensor_readings[:, i] features.extend([ np.mean(sig), np.std(sig), np.max(sig), np.min(sig), np.median(sig), np.percentile(sig, 75) - np.percentile(sig, 25), np.mean(np.abs(np.diff(sig))), np.max(np.abs(np.diff(sig))), sig[-10:].mean(), sig[-10:].std(), ]) return np.array(features) def extract_physio_features(self, physio_data): """ 生理信号特征提取 心率、皮肤电反应(GSR)、体温 """ features = [] for signal in physio_data: features.extend([ np.mean(signal), np.std(signal), np.max(signal), np.min(signal) ]) return np.array(features) def fuse_signals(self, gas_features, physio_features, visual_features): """多信号特征级融合""" gas_reduced = self.pca.fit_transform( gas_features.reshape(1, -1) ).flatten() fused = np.concatenate([ gas_reduced, physio_features, visual_features ]) return fused def predict(self, fused_features): """ 判断是否酒驾 Returns: prediction: 0=正常, 1=醉酒 confidence: 置信度 """ proba = self.classifier.predict_proba( fused_features.reshape(1, -1) )[0] return int(proba[1] > 0.5), proba[1]
if __name__ == "__main__": system = EDrunkDrivingSystem() np.random.seed(42) normal_gas = np.random.normal(0.3, 0.05, (30, 8)) drunk_gas = np.random.normal(0.7, 0.1, (30, 8)) gas_features_normal = system.extract_gas_features(normal_gas) gas_features_drunk = system.extract_gas_features(drunk_gas) print(f"正常驾驶气体特征维度: {gas_features_normal.shape}") print(f"醉酒驾驶气体特征维度: {gas_features_drunk.shape}") print(f"正常驾驶MQ-3均值: {np.mean(normal_gas[:, 0]):.3f}") print(f"醉酒驾驶MQ-3均值: {np.mean(drunk_gas[:, 0]):.3f}")
|