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
| """ WACV 2024 论文复现:通过面部特征估计BAC 参考:Keshtkaran et al., WACV 2024
核心方法: 1. MediaPipe提取68个面部关键点 2. 计算EAR/MAR/头部姿态特征 3. 特征序列标准化 4. LSTM时序建模 5. BAC回归预测 """
import numpy as np import cv2 from typing import Dict, List, Tuple from dataclasses import dataclass
@dataclass class FacialFeatures: """面部特征数据结构""" ear_left: float ear_right: float mar: float pupil_diameter: float head_pitch: float head_roll: float head_yaw: float timestamp: float
class AlcoholImpairmentDetector: """ 酒驾损伤检测器 流程: 1. 面部关键点检测 2. 特征计算(EAR/MAR/Head Pose) 3. 时序特征聚合 4. BAC估计 """ def __init__(self): self.left_eye_indices = [33, 133, 159, 145] self.right_eye_indices = [362, 263, 387, 373] self.mouth_indices = [61, 291, 0, 17] def calculate_ear( self, landmarks: np.ndarray, eye_indices: List[int] ) -> float: """ 计算Eye Aspect Ratio (EAR) EAR = |p2-p6| + |p3-p5| / 2|p1-p4| Args: landmarks: 面部关键点, shape=(468, 3) eye_indices: 眼睛关键点索引 Returns: ear: 眼睛开度比值 EAR < 0.2 表示闭眼 EAR > 0.3 表示睁眼 酒精影响:EAR平均值下降(眼睑下垂) """ p1 = landmarks[eye_indices[0]] p4 = landmarks[eye_indices[1]] p2 = landmarks[eye_indices[2]] p3 = landmarks[eye_indices[2] + 1] p5 = landmarks[eye_indices[3]] p6 = landmarks[eye_indices[3] + 1] vertical_1 = np.linalg.norm(p2 - p6) vertical_2 = np.linalg.norm(p3 - p5) horizontal = np.linalg.norm(p1 - p4) ear = (vertical_1 + vertical_2) / (2.0 * horizontal + 1e-6) return ear def calculate_mar( self, landmarks: np.ndarray ) -> float: """ 计算Mouth Aspect Ratio (MAR) MAR = |p8-p10| / |p7-p9| Args: landmarks: 面部关键点 Returns: mar: 嘴巴开度比值 酒精影响:MAR平均值增加(面部松弛张嘴) """ left_corner = landmarks[61] right_corner = landmarks[291] upper_lip = landmarks[0] lower_lip = landmarks[17] vertical = np.linalg.norm(upper_lip - lower_lip) horizontal = np.linalg.norm(left_corner - right_corner) mar = vertical / (horizontal + 1e-6) return mar def estimate_head_pose( self, landmarks: np.ndarray, image_size: Tuple[int, int] ) -> Tuple[float, float, float]: """ 估计头部姿态(Pitch/Roll/Yaw) Args: landmarks: 面部关键点 image_size: 图像尺寸 (H, W) Returns: pitch: 俯仰角(度) roll: 侧倾角(度) yaw: 偏航角(度) 方法:solvePnP求解3D-2D对应 酒精影响:头部稳定性下降,角度抖动增加 """ model_points = np.array([ (0.0, 0.0, 0.0), (0.0, -330.0, -65.0), (-225.0, 170.0, -135.0), (225.0, 170.0, -135.0), (-150.0, -150.0, -125.0), (150.0, -150.0, -125.0) ]) image_points = np.array([ landmarks[1], landmarks[152], landmarks[33], landmarks[263], landmarks[61], landmarks[291] ], dtype=np.float64) focal_length = image_size[1] camera_matrix = np.array([ [focal_length, 0, image_size[1] / 2], [0, focal_length, image_size[0] / 2], [0, 0, 1] ], dtype=np.float64) dist_coeffs = np.zeros((4, 1)) success, rotation_vector, translation_vector = cv2.solvePnP( model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE ) if not success: return 0.0, 0.0, 0.0 rotation_matrix = cv2.Rodrigues(rotation_vector)[0] pitch = np.degrees(np.arcsin(-rotation_matrix[2, 1])) roll = np.degrees(np.arctan2(rotation_matrix[2, 0], rotation_matrix[2, 2])) yaw = np.degrees(np.arctan2(rotation_matrix[0, 1], rotation_matrix[1, 1])) return pitch, roll, yaw def extract_features( self, frame: np.ndarray, landmarks: np.ndarray ) -> FacialFeatures: """ 提取完整面部特征 Args: frame: RGB图像帧 landmarks: MediaPipe关键点 Returns: features: 面部特征数据 """ ear_left = self.calculate_ear(landmarks, self.left_eye_indices) ear_right = self.calculate_ear(landmarks, self.right_eye_indices) mar = self.calculate_mar(landmarks) pitch, roll, yaw = self.estimate_head_pose( landmarks, frame.shape[:2] ) pupil_diameter = 0.0 return FacialFeatures( ear_left=ear_left, ear_right=ear_right, mar=mar, pupil_diameter=pupil_diameter, head_pitch=pitch, head_roll=roll, head_yaw=yaw, timestamp=0.0 )
def estimate_bac( self, feature_sequence: List[FacialFeatures], window_seconds: int = 60 ) -> float: """ 从特征序列估计BAC Args: feature_sequence: 特征序列(多帧) window_seconds: 时间窗口 Returns: estimated_bac: 估计BAC值 (g/dL) 方法:特征统计 + 线性回归 论文实际使用Random Forest回归器 """ if len(feature_sequence) < 10: return 0.0 ear_values = [f.ear_left + f.ear_right for f in feature_sequence] mar_values = [f.mar for f in feature_sequence] pitch_values = [f.head_pitch for f in feature_sequence] ear_mean = np.mean(ear_values) ear_std = np.std(ear_values) mar_mean = np.mean(mar_values) mar_std = np.std(mar_values) pitch_std = np.std(pitch_values) bac_estimate = 0.0 if ear_mean < 0.25: bac_estimate += 0.02 if ear_std > 0.05: bac_estimate += 0.01 if mar_mean > 0.3: bac_estimate += 0.02 if pitch_std > 5.0: bac_estimate += 0.02 return min(bac_estimate, 0.15)
if __name__ == "__main__": detector = AlcoholImpairmentDetector() np.random.seed(42) landmarks = np.random.randn(468, 3) * 100 + np.array([320, 240, 0]) frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) features = detector.extract_features(frame, landmarks) print(f"左眼EAR: {features.ear_left:.3f}") print(f"右眼EAR: {features.ear_right:.3f}") print(f"嘴巴MAR: {features.mar:.3f}") print(f"头部Pitch: {features.head_pitch:.1f}°") print(f"头部Roll: {features.head_roll:.1f}°") print(f"头部Yaw: {features.head_yaw:.1f}°") feature_sequence = [] for i in range(120): alcohol_effect = i / 120 * 0.1 f = FacialFeatures( ear_left=0.3 - alcohol_effect * 0.1, ear_right=0.3 - alcohol_effect * 0.1, mar=0.2 + alcohol_effect * 0.2, pupil_diameter=0.0, head_pitch=np.random.randn() * (5 + alcohol_effect * 10), head_roll=np.random.randn() * 2, head_yaw=np.random.randn() * 2, timestamp=i / 30 ) feature_sequence.append(f) bac = detector.estimate_bac(feature_sequence) print(f"\n估计BAC: {bac:.3f} g/dL") print(f"判定: {'酒驾风险' if bac >= 0.05 else '正常'}")
|