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
| import numpy as np from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler
class DrowsinessFeatureExtractor: """ Mercedes-Benz疲劳检测特征提取器 论文方法:从标准座舱摄像头提取35个特征 分3大类:眨眼动态 + 头部运动 + PERCLOS """ def extract_blink_features(self, eye_tracking_data): """ 眨眼动态特征(~15个) """ blink_events = self._detect_blinks(eye_tracking_data) features = { 'blink_frequency': len(blink_events) / (len(eye_tracking_data) / 30), 'blink_interval_mean': np.mean([b['interval'] for b in blink_events]), 'blink_interval_std': np.std([b['interval'] for b in blink_events]), 'blink_duration_mean': np.mean([b['duration'] for b in blink_events]), 'blink_duration_std': np.std([b['duration'] for b in blink_events]), 'blink_duration_max': np.max([b['duration'] for b in blink_events]), 'closing_velocity_mean': np.mean([b['closing_speed'] for b in blink_events]), 'closing_velocity_std': np.std([b['closing_speed'] for b in blink_events]), 'reopening_velocity_mean': np.mean([b['reopening_speed'] for b in blink_events]), 'reopening_velocity_std': np.std([b['reopening_speed'] for b in blink_events]), 'eyelid_cleft_max': np.max([b['max_openness'] for b in blink_events]), 'eyelid_cleft_mean': np.mean([b['max_openness'] for b in blink_events]), 'eyelid_cleft_std': np.std([b['max_openness'] for b in blink_events]), 'reopening_duration_mean': np.mean([b['reopen_duration'] for b in blink_events]), 'reopening_duration_std': np.std([b['reopen_duration'] for b in blink_events]), } return features def extract_head_features(self, head_pose_data): """ 头部运动特征(~10个) """ roll = head_pose_data['roll'] pitch = head_pose_data['pitch'] yaw = head_pose_data['yaw'] features = { 'head_nodding_freq': self._count_nods(pitch) / (len(pitch) / 30), 'head_nodding_amplitude': np.std(np.diff(pitch)), 'head_nodding_mean': np.mean(pitch), 'head_nodding_std': np.std(pitch), 'head_bobbing_freq': self._count_bobs(roll) / (len(roll) / 30), 'head_bobbing_amplitude': np.std(np.diff(roll)), 'head_bobbing_mean': np.mean(roll), 'head_bobbing_std': np.std(roll), 'head_movement_total': np.mean(np.sqrt( np.diff(pitch)**2 + np.diff(roll)**2 + np.diff(yaw)**2 )), 'head_stillness_ratio': self._calc_stillness_ratio(pitch, roll, yaw), } return features def extract_perclos_features(self, eye_openness): """ PERCLOS特征(~10个) """ threshold_80 = 0.2 features = { 'perclos_60s': self._calc_perclos(eye_openness, window=60, threshold=threshold_80), 'perclos_30s': self._calc_perclos(eye_openness, window=30, threshold=threshold_80), 'perclos_120s': self._calc_perclos(eye_openness, window=120, threshold=threshold_80), 'eye_openness_mean': np.mean(eye_openness), 'eye_openness_std': np.std(eye_openness), 'eye_openness_min': np.min(eye_openness), 'closed_time_ratio': np.mean(eye_openness < threshold_80), 'longest_closure': self._longest_closure(eye_openness, threshold_80), 'closure_count': np.sum(np.diff((eye_openness < threshold_80).astype(int)) > 0), 'mean_closure_duration': self._mean_closure_duration(eye_openness, threshold_80), } return features def _detect_blinks(self, data): """检测眨眼事件""" blinks = [] is_closed = data < 0.2 transitions = np.diff(is_closed.astype(int)) close_idx = np.where(transitions == 1)[0] open_idx = np.where(transitions == -1)[0] for c, o in zip(close_idx, open_idx): blinks.append({ 'duration': (o - c) / 30, 'closing_speed': 0.3 / (c - max(close_idx[close_idx < c-1], default=c-5) + 1), 'reopening_speed': 0.3 / (o - c), 'max_openness': 0.8, 'reopen_duration': (o - c) / 30, 'interval': 0 }) return blinks def _calc_perclos(self, data, window, threshold): """计算PERCLOS""" n = len(data) w = window * 30 values = [] for i in range(0, n - w, w): values.append(np.mean(data[i:i+w] < threshold) * 100) return np.mean(values) if values else 0 def _count_nods(self, pitch): return np.sum(np.abs(np.diff(pitch)) > 5) def _count_bobs(self, roll): return np.sum(np.abs(np.diff(roll)) > 5) def _calc_stillness_ratio(self, p, r, y): movement = np.sqrt(np.diff(p)**2 + np.diff(r)**2 + np.diff(y)**2) return np.mean(movement < 1) def _longest_closure(self, data, threshold): is_closed = data < threshold max_len = current = 0 for c in is_closed: if c: current += 1 max_len = max(max_len, current) else: current = 0 return max_len / 30 def _mean_closure_duration(self, data, threshold): is_closed = data < threshold transitions = np.diff(is_closed.astype(int)) close_idx = np.where(transitions == 1)[0] open_idx = np.where(transitions == -1)[0] durations = [(o - c) / 30 for c, o in zip(close_idx, open_idx)] return np.mean(durations) if durations else 0
class MercedesDrowsinessClassifier: """ Mercedes-Benz k-NN疲劳分类器 论文核心模型 二分类: Awake vs Drowsy (84.2% BA) 三分类: Awake/Questionable/Drowsy (70.0% with OvO) """ def __init__(self, n_neighbors=100): self.scaler = StandardScaler() self.knn_binary = KNeighborsClassifier( n_neighbors=n_neighbors, weights='distance', metric='euclidean' ) self.knn_multiclass = KNeighborsClassifier( n_neighbors=n_neighbors, weights='distance', metric='euclidean' ) self.feature_extractor = DrowsinessFeatureExtractor() def fit(self, features, labels): """训练""" features_scaled = self.scaler.fit_transform(features) self.knn_binary.fit(features_scaled, labels) def predict(self, features): """预测""" features_scaled = self.scaler.transform(features) return self.knn_binary.predict(features_scaled) def explain(self, features, k=5): """ 可解释性:返回k个最近邻 安全关键系统的可审计需求 """ features_scaled = self.scalaler.transform(features) distances, indices = self.knn_binary.kneighbors(features_scaled, k) return { 'prediction': self.knn_binary.predict(features_scaled), 'neighbors': indices, 'distances': distances, 'explanation': f"决策基于{k}个最相似的历史样本" }
if __name__ == "__main__": np.random.seed(42) n_samples = 1000 n_features = 35 X = np.random.randn(n_samples, n_features) y = np.random.choice(['awake', 'drowsy'], n_samples, p=[0.6, 0.4]) clf = MercedesDrowsinessClassifier(n_neighbors=100) clf.fit(X, y) pred = clf.predict(X[:5]) print(f"预测结果: {pred}") print(f"\n特征数: {n_features}") print("论文发现: ~10个特征足够(Sequential Forward Selection)") print("关键特征: reopening_duration, eyelid_cleft, head_nodding")
|