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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
| """ 可解释驾驶员行为状态监测系统 论文: Frontiers in Robotics and AI, 2026
硬件: Intel RealSense L515 + Polaris GEM e6 软件: ROS + Foxglove + MediaPipe + OpenCV
依赖: mediapipe, opencv, scikit-learn, numpy """
import numpy as np import cv2 import mediapipe as mp from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier from dataclasses import dataclass from typing import Tuple, Dict
@dataclass class DriverStateConfig: """驾驶员状态监测配置""" states = ['neutral', 'eye_closure', 'out_of_safe_zone', 'yawning', 'excessive_blinking'] left_eye_indices = [33, 160, 158, 133, 153, 144] right_eye_indices = [362, 385, 387, 263, 373, 380] mouth_indices = [61, 291, 13, 14, 78, 308] ear_threshold_closed: float = 0.21 ear_threshold_blink: float = 0.25 mar_threshold_yawn: float = 0.6 pitch_threshold: float = 15.0 yaw_threshold: float = 25.0 roll_threshold: float = 20.0 blink_rate_threshold: float = 20.0 blink_duration_range: Tuple[float, float] = (0.1, 0.4)
class FaceLandmarkExtractor: """MediaPipe面部关键点提取器""" def __init__(self): self.mp_face = mp.solutions.face_mesh self.face_mesh = self.mp_face.FaceMesh( static_image_mode=False, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5, ) self.config = DriverStateConfig() def process(self, frame: np.ndarray) -> dict: """ 处理一帧图像,提取面部特征 Args: frame: (H, W, 3) BGR图像 Returns: features: dict, 包含 EAR, MAR, 头部角度, 关键点 """ h, w = frame.shape[:2] rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = self.face_mesh.process(rgb) if results.multi_face_landmarks is None: return None landmarks = results.multi_face_landmarks[0].landmark left_ear = self._compute_ear(landmarks, self.config.left_eye_indices, w, h) right_ear = self._compute_ear(landmarks, self.config.right_eye_indices, w, h) ear = (left_ear + right_ear) / 2 mar = self._compute_mar(landmarks, self.config.mouth_indices, w, h) pitch, yaw, roll = self._estimate_head_pose(landmarks, w, h) return { 'ear': ear, 'left_ear': left_ear, 'right_ear': right_ear, 'mar': mar, 'pitch': pitch, 'yaw': yaw, 'roll': roll, 'landmarks': landmarks, } def _compute_ear(self, landmarks, indices, w, h): """ 计算Eye Aspect Ratio (EAR) EAR = (|P2-P6| + |P3-P5|) / (2 × |P1-P4|) 闭眼时EAR趋近0,睁眼时EAR约0.25-0.35 """ pts = [(landmarks[i].x * w, landmarks[i].y * h) for i in indices] v1 = np.linalg.norm(np.array(pts[1]) - np.array(pts[5])) v2 = np.linalg.norm(np.array(pts[2]) - np.array(pts[4])) h_dist = np.linalg.norm(np.array(pts[0]) - np.array(pts[3])) if h_dist < 1e-6: return 0 return (v1 + v2) / (2 * h_dist) def _compute_mar(self, landmarks, indices, w, h): """ 计算Mouth Aspect Ratio (MAR) MAR = (|上唇-下唇|) / (|左嘴角-右嘴角|) 打哈欠时MAR显著增大 """ pts = [(landmarks[i].x * w, landmarks[i].y * h) for i in indices] v = np.linalg.norm(np.array(pts[2]) - np.array(pts[3])) h_dist = np.linalg.norm(np.array(pts[0]) - np.array(pts[1])) if h_dist < 1e-6: return 0 return v / h_dist def _estimate_head_pose(self, landmarks, w, h): """ 从面部关键点估计头部姿态 (俯仰/偏航/翻滚) 简化版: 使用关键点几何关系 论文使用MediaPipe的transform matrix """ nose = np.array([landmarks[1].x, landmarks[1].y]) left_eye = np.array([landmarks[33].x, landmarks[33].y]) right_eye = np.array([landmarks[263].x, landmarks[263].y]) eye_mid = (left_eye + right_eye) / 2 eye_dist = np.linalg.norm(right_eye - left_eye) nose_offset = (nose[0] - eye_mid[0]) / (eye_dist + 1e-6) yaw = np.degrees(np.arcsin(np.clip(nose_offset, -1, 1))) pitch = np.degrees(np.arctan2( nose[1] - eye_mid[1], eye_dist )) roll = np.degrees(np.arctan2( right_eye[1] - left_eye[1], right_eye[0] - left_eye[0] )) return pitch, yaw, roll
class IndividualCalibrator: """ 个性化校准器 论文核心: 每个驾驶员建立独立中性基线 - 收集正常驾驶时EAR, MAR, 头部角度 - 计算各特征的均值和标准差 - 后续检测基于相对偏差 """ def __init__(self, calibration_duration_sec=30, fps=30): self.duration = calibration_duration_sec self.fps = fps self.n_samples = calibration_duration_sec * fps self.baselines = {} self.is_calibrated = False def collect_baseline(self, features_list: list): """ 收集基线特征 Args: features_list: list of feature dicts from FaceLandmarkExtractor """ if len(features_list) < self.n_samples * 0.5: return False ears = [f['ear'] for f in features_list if f is not None] mars = [f['mar'] for f in features_list if f is not None] pitches = [f['pitch'] for f in features_list if f is not None] yaws = [f['yaw'] for f in features_list if f is not None] rolls = [f['roll'] for f in features_list if f is not None] self.baselines = { 'ear_mean': np.mean(ears), 'ear_std': np.std(ears), 'mar_mean': np.mean(mars), 'mar_std': np.std(mars), 'pitch_mean': np.mean(pitches), 'pitch_std': np.std(pitches), 'yaw_mean': np.mean(yaws), 'yaw_std': np.std(yaws), 'roll_mean': np.mean(rolls), 'roll_std': np.std(rolls), } self.is_calibrated = True return True def get_normalized_features(self, features: dict) -> np.ndarray: """ 获取相对基线的归一化特征 Returns: feat_vector: (10,) 归一化特征 """ if not self.is_calibrated: return np.zeros(10) feat = np.array([ (features['ear'] - self.baselines['ear_mean']) / (self.baselines['ear_std'] + 1e-6), (features['mar'] - self.baselines['mar_mean']) / (self.baselines['mar_std'] + 1e-6), (features['pitch'] - self.baselines['pitch_mean']) / (self.baselines['pitch_std'] + 1e-6), (features['yaw'] - self.baselines['yaw_mean']) / (self.baselines['yaw_std'] + 1e-6), (features['roll'] - self.baselines['roll_mean']) / (self.baselines['roll_std'] + 1e-6), features['ear'], features['mar'], features['pitch'], features['yaw'], features['roll'], ]) return feat
class DriverStateClassifier: """ 驾驶员状态分类器 论文对比: SVM vs RF vs CNN vs RNN SVM最优: 91% F1 (室内) / 稳定 (室外) RF: 91% F1 (室内) / 70% F1 (室外) - 跨场景退化 CNN: ~80% F1 / RNN: ~80% F1 """ def __init__(self, model_type='svm'): self.config = DriverStateConfig() self.calibrator = IndividualCalibrator() if model_type == 'svm': self.classifier = SVC( kernel='rbf', C=10.0, gamma='scale', probability=True, random_state=42 ) elif model_type == 'rf': self.classifier = RandomForestClassifier( n_estimators=100, max_depth=10, random_state=42 ) else: raise ValueError(f"Unknown model type: {model_type}") self.model_type = model_type self.is_trained = False self.blink_counter = 0 self.blink_timestamps = [] def train(self, X_train, y_train): """训练分类器""" self.classifier.fit(X_train, y_train) self.is_trained = True def predict(self, features: dict) -> dict: """ 预测驾驶员状态 Args: features: from FaceLandmarkExtractor Returns: result: dict with state, confidence, features """ if not self.is_trained or not self.calibrator.is_calibrated: return {'state': 'unknown', 'confidence': 0} feat_vec = self.calibrator.get_normalized_features(features).reshape(1, -1) state_idx = self.classifier.predict(feat_vec)[0] proba = self.classifier.predict_proba(feat_vec)[0] confidence = proba[state_idx] state_name = self.config.states[state_idx] if state_name == 'out_of_safe_zone' and confidence < 0.6: if abs(features['yaw']) < self.config.yaw_threshold * 0.7: state_name = 'neutral' return { 'state': state_name, 'state_index': state_idx, 'confidence': confidence, 'ear': features['ear'], 'mar': features['mar'], 'pitch': features['pitch'], 'yaw': features['yaw'], 'roll': features['roll'], }
if __name__ == "__main__": print("=" * 60) print("可解释驾驶员行为状态监测系统") print("=" * 60) config = DriverStateConfig() print(f"\n五种行为状态:") for i, state in enumerate(config.states): print(f" {i}: {state}") print(f"\n关键阈值:") print(f" EAR闭眼阈值: < {config.ear_threshold_closed}") print(f" MAR打哈欠阈值: > {config.mar_threshold_yawn}") print(f" 头部偏航阈值: > {config.yaw_threshold}°") print(f" 眨眼频率阈值: > {config.blink_rate_threshold}/min") extractor = FaceLandmarkExtractor() frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) features = extractor.process(frame) if features: print(f"\n模拟特征提取:") print(f" EAR: {features['ear']:.4f}") print(f" MAR: {features['mar']:.4f}") print(f" Pitch: {features['pitch']:.2f}°") print(f" Yaw: {features['yaw']:.2f}°") print(f" Roll: {features['roll']:.2f}°") calibrator = IndividualCalibrator(calibration_duration_sec=10, fps=30) np.random.seed(42) baseline_features = [] for _ in range(300): baseline_features.append({ 'ear': np.random.normal(0.30, 0.03), 'mar': np.random.normal(0.15, 0.02), 'pitch': np.random.normal(-5, 3), 'yaw': np.random.normal(0, 5), 'roll': np.random.normal(0, 2), }) calibrator.collect_baseline(baseline_features) print(f"\n校准结果:") for k, v in calibrator.baselines.items(): print(f" {k}: {v:.4f}") from sklearn.model_selection import train_test_split, cross_val_score n_per_class = 100 X_sim = [] y_sim = [] for class_idx in range(5): for _ in range(n_per_class): if class_idx == 0: ear = np.random.normal(0.30, 0.03) mar = np.random.normal(0.15, 0.02) pitch = np.random.normal(0, 3) yaw = np.random.normal(0, 3) roll = np.random.normal(0, 2) elif class_idx == 1: ear = np.random.normal(0.15, 0.02) mar = np.random.normal(0.15, 0.02) pitch = np.random.normal(0, 3) yaw = np.random.normal(0, 3) roll = np.random.normal(0, 2) elif class_idx == 2: ear = np.random.normal(0.30, 0.03) mar = np.random.normal(0.15, 0.02) pitch = np.random.normal(0, 3) yaw = np.random.normal(35, 5) roll = np.random.normal(0, 2) elif class_idx == 3: ear = np.random.normal(0.28, 0.03) mar = np.random.normal(0.75, 0.05) pitch = np.random.normal(-10, 3) yaw = np.random.normal(0, 3) roll = np.random.normal(0, 2) elif class_idx == 4: ear = np.random.normal(0.22, 0.05) mar = np.random.normal(0.15, 0.02) pitch = np.random.normal(0, 3) yaw = np.random.normal(0, 3) roll = np.random.normal(0, 2) X_sim.append([ear, mar, pitch, yaw, roll, ear, mar, pitch, yaw, roll]) y_sim.append(class_idx) X_sim = np.array(X_sim) y_sim = np.array(y_sim) classifier = DriverStateClassifier(model_type='svm') X_train, X_test, y_train, y_test = train_test_split( X_sim, y_sim, test_size=0.3, stratify=y_sim, random_state=42 ) classifier.train(X_train, y_train) from sklearn.metrics import classification_report, f1_score y_pred = classifier.classifier.predict(X_test) f1 = f1_score(y_test, y_pred, average='macro') print(f"\nSVM 分类器性能:") print(f" Macro F1: {f1:.3f}") print(f"\n分类报告:") print(classification_report(y_test, y_pred, target_names=config.states)) rf_classifier = DriverStateClassifier(model_type='rf') rf_classifier.train(X_train, y_train) y_pred_rf = rf_classifier.classifier.predict(X_test) f1_rf = f1_score(y_test, y_pred_rf, average='macro') print(f"\nRF 分类器 F1: {f1_rf:.3f}") print(f"\n{'='*60}") print(f"论文报告性能对比:") print(f"{'模型':<15} {'室内F1':<12} {'室外F1':<12} {'说明'}") print(f"{'-'*60}") print(f"{'SVM (RBF)':<15} {'91%':<12} {'~88%':<12} {'最稳定'}") print(f"{'Random Forest':<15} {'91%':<12} {'~70%':<12} {'跨场景退化'}") print(f"{'CNN':<15} {'~80%':<12} {'~75%':<12} {'特征学习'}") print(f"{'RNN':<15} {'~80%':<12} {'~75%':<12} {'时序建模'}") print(f"{'启发式规则':<15} {'~81%':<12} {'-':<12} {'基线模型'}")
|