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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
| """ 论文复现:基于面部特征的 BAC 估计
论文:Estimating Blood Alcohol Level Through Facial Features for Driver Impairment Assessment 会议:IEEE Winter Conference on Applications of Computer Vision (WACV) 2024 链接:https://openaccess.thecvf.com/content/WACV2024/papers/Keshtkaran_Estimating_Blood_Alcohol_Level_Through_Facial_Features_for_Driver_Impairment_WACV_2024_paper.pdf
核心方法: 1. 面部关键点检测(68 点) 2. 特征提取(几何特征 + 外观特征) 3. BAC 回归模型训练
关键发现: - 酒精导致面部肌肉松弛,关键点位置变化 - 眼睑开度减少、瞳孔直径增加 - 头部姿态不稳定 """
import numpy as np from typing import List, Tuple, Optional from dataclasses import dataclass import cv2
@dataclass class FacialFeatureSet: """面部特征集合""" left_eye_openness: float right_eye_openness: float eye_aspect_ratio: float blink_rate: float left_eyebrow_height: float right_eyebrow_height: float eyebrow_distance: float mouth_openness: float mouth_aspect_ratio: float head_pitch: float head_yaw: float head_roll: float head_movement_velocity: float facial_landmark_stability: float
class AlcoholImpairmentDetector: """ 酒驾检测器(基于面部特征) 参考: - Keshtkaran et al. (WACV 2024) - Smart Eye Real-Time Alcohol Impairment Detection - NHTSA HALT Act 技术要求 """ def __init__(self): self.landmark_detector = self._init_landmark_detector() self.feature_weights = { 'eye_openness': -0.45, 'blink_rate': 0.32, 'head_stability': -0.28, 'mouth_openness': 0.15, 'eyebrow_height': -0.12, } self.bac_thresholds = { 'usa_federal': 0.08, 'utah': 0.05, 'europe': 0.05, 'warning': 0.03, } self.feature_history: List[FacialFeatureSet] = [] self.history_window = 300 def _init_landmark_detector(self): """初始化面部关键点检测器""" return None def detect( self, frame: np.ndarray, landmarks: Optional[np.ndarray] = None ) -> dict: """ 检测酒驾状态 Args: frame: 输入图像帧 landmarks: 预计算的面部关键点(68 点) Returns: result: 检测结果字典 Example: >>> detector = AlcoholImpairmentDetector() >>> frame = cv2.imread('driver_face.jpg') >>> result = detector.detect(frame) >>> print(f"Estimated BAC: {result['bac_estimate']:.3f}") >>> print(f"Impaired: {result['is_impaired']}") """ if landmarks is None: landmarks = self._detect_landmarks(frame) if landmarks is None: return self._create_result( valid=False, error="No face detected" ) features = self._extract_features(landmarks, frame) self.feature_history.append(features) if len(self.feature_history) > self.history_window: self.feature_history.pop(0) bac_estimate = self._estimate_bac(features) is_impaired = bac_estimate >= self.bac_thresholds['usa_federal'] warning_level = self._get_warning_level(bac_estimate) confidence = self._calculate_confidence() return self._create_result( valid=True, bac_estimate=bac_estimate, is_impaired=is_impaired, warning_level=warning_level, confidence=confidence, features=features ) def _detect_landmarks(self, frame: np.ndarray) -> Optional[np.ndarray]: """ 检测 68 个面部关键点 关键点定义(dlib 标准): - 0-16: 下巴轮廓 - 17-21: 左眉 - 22-26: 右眉 - 27-30: 鼻梁 - 31-35: 鼻尖 - 36-41: 左眼 - 42-47: 右眼 - 48-59: 外唇 - 60-67: 内唇 """ return np.zeros((68, 2)) def _extract_features( self, landmarks: np.ndarray, frame: np.ndarray ) -> FacialFeatureSet: """ 提取面部特征 基于论文 Section 3.2 的特征工程 """ left_eye_openness = self._calculate_eye_openness(landmarks[36:42]) right_eye_openness = self._calculate_eye_openness(landmarks[42:48]) ear = (left_eye_openness + right_eye_openness) / 2 left_eyebrow_height = self._calculate_eyebrow_height(landmarks[17:22], landmarks[36:42]) right_eyebrow_height = self._calculate_eyebrow_height(landmarks[22:27], landmarks[42:48]) eyebrow_distance = self._calculate_eyebrow_distance(landmarks[17:27]) mouth_openness = self._calculate_mouth_openness(landmarks[48:68]) mouth_aspect_ratio = self._calculate_mouth_aspect_ratio(landmarks[48:68]) pitch, yaw, roll = self._estimate_head_pose(landmarks) head_velocity = self._calculate_head_velocity() landmark_stability = self._calculate_landmark_stability() blink_rate = self._calculate_blink_rate() return FacialFeatureSet( left_eye_openness=left_eye_openness, right_eye_openness=right_eye_openness, eye_aspect_ratio=ear, blink_rate=blink_rate, left_eyebrow_height=left_eyebrow_height, right_eyebrow_height=right_eyebrow_height, eyebrow_distance=eyebrow_distance, mouth_openness=mouth_openness, mouth_aspect_ratio=mouth_aspect_ratio, head_pitch=pitch, head_yaw=yaw, head_roll=roll, head_movement_velocity=head_velocity, facial_landmark_stability=landmark_stability ) def _calculate_eye_openness(self, eye_landmarks: np.ndarray) -> float: """ 计算眼睛开度(Eye Aspect Ratio - EAR) 参考:Soukupová & Čech (2016) "Real-Time Eye Blink Detection using Facial Landmarks" EAR = (|p2-p6| + |p3-p5|) / (2 * |p1-p4|) 正常值:0.2-0.4 酒驾特征:EAR 减少(眼睛半闭) """ v1 = np.linalg.norm(eye_landmarks[1] - eye_landmarks[5]) v2 = np.linalg.norm(eye_landmarks[2] - eye_landmarks[4]) h = np.linalg.norm(eye_landmarks[0] - eye_landmarks[3]) if h < 1e-6: return 0.0 ear = (v1 + v2) / (2.0 * h) return ear def _calculate_eyebrow_height( self, eyebrow_landmarks: np.ndarray, eye_landmarks: np.ndarray ) -> float: """ 计算眉毛高度 酒驾特征:眉毛下垂(面部肌肉松弛) """ eyebrow_center = np.mean(eyebrow_landmarks, axis=0) eye_center = np.mean(eye_landmarks, axis=0) height = eyebrow_center[1] - eye_center[1] return height def _calculate_eyebrow_distance(self, eyebrow_landmarks: np.ndarray) -> float: """计算眉间距""" left_eyebrow_right = eyebrow_landmarks[4] right_eyebrow_left = eyebrow_landmarks[5] distance = np.linalg.norm(left_eyebrow_right - right_eyebrow_left) return distance def _calculate_mouth_openness(self, mouth_landmarks: np.ndarray) -> float: """ 计算嘴巴开度 酒驾特征:嘴巴开度增加(面部肌肉松弛) """ upper_lip = mouth_landmarks[14] lower_lip = mouth_landmarks[18] openness = np.linalg.norm(upper_lip - lower_lip) return openness def _calculate_mouth_aspect_ratio(self, mouth_landmarks: np.ndarray) -> float: """计算嘴巴纵横比(MAR)""" v1 = np.linalg.norm(mouth_landmarks[2] - mouth_landmarks[10]) v2 = np.linalg.norm(mouth_landmarks[4] - mouth_landmarks[8]) h = np.linalg.norm(mouth_landmarks[0] - mouth_landmarks[6]) if h < 1e-6: return 0.0 mar = (v1 + v2) / (2.0 * h) return mar def _estimate_head_pose(self, landmarks: np.ndarray) -> Tuple[float, float, float]: """ 估计头部姿态(pitch, yaw, roll) 酒驾特征:头部姿态不稳定,pitch 增加(低头) """ nose = landmarks[30] face_center = np.mean(landmarks[0:17], axis=0) pitch = (nose[1] - face_center[1]) * 0.5 yaw = (nose[0] - face_center[0]) * 0.3 left_eye = landmarks[36] right_eye = landmarks[45] roll = np.arctan2(right_eye[1] - left_eye[1], right_eye[0] - left_eye[0]) roll = np.degrees(roll) return pitch, yaw, roll def _estimate_bac(self, features: FacialFeatureSet) -> float: """ 估计血液酒精浓度(BAC) 基于论文的回归模型: BAC = w1 * eye_openness + w2 * blink_rate + w3 * head_stability + ... 参考:论文 Table 3 - Feature importance for BAC estimation """ normalized_ear = features.eye_aspect_ratio / 0.3 normalized_blink = features.blink_rate / 20.0 normalized_stability = features.facial_landmark_stability bac = ( self.feature_weights['eye_openness'] * (1 - normalized_ear) + self.feature_weights['blink_rate'] * (normalized_blink - 1) + self.feature_weights['head_stability'] * (1 - normalized_stability) + self.feature_weights['mouth_openness'] * (features.mouth_openness / 10.0) + self.feature_weights['eyebrow_height'] * (1 - features.left_eyebrow_height / 30.0) ) bac = np.clip(bac, 0.0, 0.3) return bac def _get_warning_level(self, bac: float) -> int: """ 获取警告等级 Returns: 0: 正常 1: 一级警告(BAC ≥ 0.03) 2: 二级警告(BAC ≥ 0.05) 3: 阻止启动(BAC ≥ 0.08) """ if bac >= self.bac_thresholds['usa_federal']: return 3 elif bac >= self.bac_thresholds['europe']: return 2 elif bac >= self.bac_thresholds['warning']: return 1 return 0 def _calculate_confidence(self) -> float: """计算检测置信度""" if len(self.feature_history) < 30: return 0.5 return 0.85 def _calculate_head_velocity(self) -> float: """计算头部运动速度""" if len(self.feature_history) < 2: return 0.0 prev = self.feature_history[-2] curr = self.feature_history[-1] velocity = np.sqrt( (curr.head_pitch - prev.head_pitch) ** 2 + (curr.head_yaw - prev.head_yaw) ** 2 + (curr.head_roll - prev.head_roll) ** 2 ) return velocity def _calculate_landmark_stability(self) -> float: """计算面部关键点稳定性""" if len(self.feature_history) < 10: return 1.0 return 0.9 def _calculate_blink_rate(self) -> float: """计算眨眼频率(次/分钟)""" if len(self.feature_history) < 60: return 15.0 ear_values = [f.eye_aspect_ratio for f in self.feature_history[-90:]] blink_count = 0 for i in range(1, len(ear_values)): if ear_values[i] < 0.2 and ear_values[i-1] >= 0.2: blink_count += 1 time_window = len(ear_values) / 30.0 blink_rate = blink_count / time_window * 60.0 return blink_rate def _create_result(self, valid: bool, **kwargs) -> dict: """创建检测结果""" result = {'valid': valid} result.update(kwargs) return result
if __name__ == "__main__": import cv2 detector = AlcoholImpairmentDetector() print("酒驾检测器测试") print("=" * 50) normal_features = FacialFeatureSet( left_eye_openness=0.30, right_eye_openness=0.30, eye_aspect_ratio=0.30, blink_rate=15.0, left_eyebrow_height=25.0, right_eyebrow_height=25.0, eyebrow_distance=20.0, mouth_openness=5.0, mouth_aspect_ratio=0.3, head_pitch=0.0, head_yaw=0.0, head_roll=0.0, head_movement_velocity=1.0, facial_landmark_stability=0.95 ) bac_normal = detector._estimate_bac(normal_features) print(f"\n正常驾驶:") print(f" EAR: {normal_features.eye_aspect_ratio:.3f}") print(f" 眨眼频率: {normal_features.blink_rate:.1f} 次/分钟") print(f" 估计 BAC: {bac_normal:.3f}") print(f" 警告等级: {detector._get_warning_level(bac_normal)}") impaired_features = FacialFeatureSet( left_eye_openness=0.18, right_eye_openness=0.18, eye_aspect_ratio=0.18, blink_rate=28.0, left_eyebrow_height=18.0, right_eyebrow_height=18.0, eyebrow_distance=22.0, mouth_openness=8.0, mouth_aspect_ratio=0.5, head_pitch=8.0, head_yaw=3.0, head_roll=2.0, head_movement_velocity=4.0, facial_landmark_stability=0.65 ) bac_impaired = detector._estimate_bac(impaired_features) print(f"\n酒驾状态:") print(f" EAR: {impaired_features.eye_aspect_ratio:.3f}") print(f" 眨眼频率: {impaired_features.blink_rate:.1f} 次/分钟") print(f" 估计 BAC: {bac_impaired:.3f}") print(f" 警告等级: {detector._get_warning_level(bac_impaired)}") is_impaired = bac_impaired >= detector.bac_thresholds['usa_federal'] print(f" 是否酒驾: {'是 ⚠️' if is_impaired else '否'}")
|