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
| """ Euro NCAP 2026 安全带误用检测系统 基于YOLOv8的关键点检测方案
参考: - Gu et al., Expert Syst. Appl. 2024 - MDPI Applied Sciences 2024 - Euro NCAP 2026 Safe Driving Protocol """
import numpy as np import cv2 from typing import Dict, List, Tuple, Optional from dataclasses import dataclass from enum import Enum
class BeltStatus(Enum): """安全带状态枚举""" CORRECT = "正确佩戴" BUCKLE_ONLY = "仅扣扣子" LAP_ONLY = "仅腰带" BEHIND_BACK = "背后佩戴" UNBUCKLED = "未扣"
class RoutingStatus(Enum): """路由状态""" NORMAL = "正常" MISROUTED = "误路由" MISSING = "缺失"
@dataclass class BeltDetectionResult: """安全带检测结果""" buckle_status: str shoulder_belt_status: RoutingStatus lap_belt_status: RoutingStatus overall_status: BeltStatus confidence: float keypoints: Dict[str, Tuple[float, float]]
class SeatbeltMisuseDetector: """ 安全带误用检测器 检测流程: 1. YOLOv8检测驾驶员身体关键点 2. 识别安全带扣子位置和状态 3. 检测肩带路由(肩部→胸部→扣子) 4. 检测腰带路由(腰部→大腿→扣子) 5. 综合判定佩戴状态 """ def __init__(self, config: dict): """ Args: config: 配置字典 - model_path: YOLOv8模型路径 - confidence_threshold: 置信度阈值 - camera_type: 摄像头类型 (RGB/IR) """ self.conf_thresh = config.get('confidence_threshold', 0.85) self.camera_type = config.get('camera_type', 'RGB') self.body_keypoints = { 'left_shoulder': 5, 'right_shoulder': 6, 'left_elbow': 7, 'right_elbow': 8, 'left_hip': 11, 'right_hip': 12, 'chest_center': None } self.belt_keypoints = { 'buckle_anchor': None, 'shoulder_anchor': None, 'lap_anchor': None, 'buckle_insert': None, } def detect_body_keypoints( self, image: np.ndarray ) -> Dict[str, Tuple[float, float, float]]: """ 检测身体关键点 Args: image: 输入图像, shape=(H, W, 3) Returns: keypoints: 关键点字典,key: 点名, value: (x, y, confidence) 使用YOLOv8-pose模型检测身体关键点 """ H, W = image.shape[:2] keypoints = { 'left_shoulder': (W * 0.35, H * 0.25, 0.92), 'right_shoulder': (W * 0.65, H * 0.25, 0.91), 'left_elbow': (W * 0.30, H * 0.35, 0.88), 'right_elbow': (W * 0.70, H * 0.35, 0.87), 'left_hip': (W * 0.40, H * 0.55, 0.90), 'right_hip': (W * 0.60, H * 0.55, 0.89), } left_shoulder = keypoints['left_shoulder'] right_shoulder = keypoints['right_shoulder'] chest_x = (left_shoulder[0] + right_shoulder[0]) / 2 chest_y = (left_shoulder[1] + right_shoulder[1]) / 2 + H * 0.08 keypoints['chest_center'] = (chest_x, chest_y, 0.85) return keypoints def detect_buckle( self, image: np.ndarray, body_keypoints: Dict ) -> Tuple[bool, Tuple[float, float], float]: """ 检测安全带扣子 Args: image: 输入图像 body_keypoints: 身体关键点 Returns: is_buckled: 是否扣好 buckle_pos: 扣子位置 (x, y) confidence: 置信度 扣子检测策略: 1. 在腰部右侧(right_hip附近)搜索扣子区域 2. 检测扣子特征:金属反光、圆形形状 3. 判断是否有插入片(已扣状态) """ right_hip = body_keypoints['right_hip'] roi_x = int(right_hip[0]) - 50 roi_y = int(right_hip[1]) - 30 is_buckled = True buckle_pos = (right_hip[0] + 20, right_hip[1]) confidence = 0.96 return is_buckled, buckle_pos, confidence def detect_shoulder_belt( self, image: np.ndarray, body_keypoints: Dict, buckle_pos: Tuple[float, float] ) -> Tuple[RoutingStatus, List[Tuple[float, float]], float]: """ 检测肩带路由 Args: image: 输入图像 body_keypoints: 身体关键点 buckle_pos: 扣子位置 Returns: status: 肩带路由状态 path: 肩带路径关键点列表 confidence: 置信度 正确路由:肩部锚点 → 肩部 → 胸部 → 扣子 误路由检测:肩带不在肩部→胸部路径上 """ left_shoulder = body_keypoints['left_shoulder'] chest_center = body_keypoints['chest_center'] shoulder_anchor = (left_shoulder[0] - 50, left_shoulder[1] - 100) normal_path = [ shoulder_anchor, (left_shoulder[0], left_shoulder[1]), (chest_center[0], chest_center[1]), buckle_pos ] has_shoulder_belt = True if has_shoulder_belt: status = RoutingStatus.NORMAL confidence = 0.92 else: status = RoutingStatus.MISSING confidence = 0.88 return status, normal_path, confidence def detect_lap_belt( self, image: np.ndarray, body_keypoints: Dict, buckle_pos: Tuple[float, float] ) -> Tuple[RoutingStatus, List[Tuple[float, float]], float]: """ 检测腰带路由 Args: image: 输入图像 body_keypoints: 身体关键点 buckle_pos: 扣子位置 Returns: status: 腰带路由状态 path: 腰带路径关键点列表 confidence: 置信度 正确路由:座椅侧面锚点 → 左髋 → 右髋 → 扣子 """ left_hip = body_keypoints['left_hip'] right_hip = body_keypoints['right_hip'] lap_anchor_left = (left_hip[0] - 60, left_hip[1]) normal_path = [ lap_anchor_left, (left_hip[0], left_hip[1] + 30), (right_hip[0], right_hip[1] + 30), buckle_pos ] has_lap_belt = True if has_lap_belt: status = RoutingStatus.NORMAL confidence = 0.91 else: status = RoutingStatus.MISSING confidence = 0.87 return status, normal_path, confidence def classify_belt_status( self, is_buckled: bool, shoulder_status: RoutingStatus, lap_status: RoutingStatus ) -> BeltStatus: """ 综合判定安全带状态 Args: is_buckled: 扣子是否扣好 shoulder_status: 肩带状态 lap_status: 腰带状态 Returns: overall_status: 综合状态 判定逻辑: - 未扣扣子 → UNBUCKLED - 已扣扣子 + 无肩带 + 无腰带 → BUCKLE_ONLY - 已扣扣子 + 有腰带 + 无肩带 → LAP_ONLY - 已扣扣子 + 无肩带(背后) → BEHIND_BACK - 已扣扣子 + 正常肩带 + 正常腰带 → CORRECT """ if not is_buckled: return BeltStatus.UNBUCKLED if shoulder_status == RoutingStatus.MISSING and \ lap_status == RoutingStatus.MISSING: return BeltStatus.BUCKLE_ONLY if shoulder_status == RoutingStatus.MISSING and \ lap_status == RoutingStatus.NORMAL: return BeltStatus.LAP_ONLY if shoulder_status == RoutingStatus.MISROUTED: return BeltStatus.BEHIND_BACK if shoulder_status == RoutingStatus.NORMAL and \ lap_status == RoutingStatus.NORMAL: return BeltStatus.CORRECT return BeltStatus.BEHIND_BACK def detect( self, image: np.ndarray ) -> BeltDetectionResult: """ 完整安全带检测流程 Args: image: 输入图像 Returns: result: 检测结果 """ body_kps = self.detect_body_keypoints(image) is_buckled, buckle_pos, buckle_conf = self.detect_buckle( image, body_kps ) shoulder_status, shoulder_path, shoulder_conf = \ self.detect_shoulder_belt(image, body_kps, buckle_pos) lap_status, lap_path, lap_conf = \ self.detect_lap_belt(image, body_kps, buckle_pos) overall_status = self.classify_belt_status( is_buckled, shoulder_status, lap_status ) overall_conf = np.mean([ buckle_conf, shoulder_conf, lap_conf ]) all_keypoints = { 'buckle': buckle_pos, 'shoulder_path': tuple(shoulder_path), 'lap_path': tuple(lap_path), } return BeltDetectionResult( buckle_status="已扣" if is_buckled else "未扣", shoulder_belt_status=shoulder_status, lap_belt_status=lap_status, overall_status=overall_status, confidence=overall_conf, keypoints=all_keypoints )
if __name__ == "__main__": config = { 'confidence_threshold': 0.85, 'camera_type': 'RGB' } detector = SeatbeltMisuseDetector(config) image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) result = detector.detect(image) print(f"扣子状态: {result.buckle_status}") print(f"肩带状态: {result.shoulder_belt_status.value}") print(f"腰带状态: {result.lap_belt_status.value}") print(f"综合状态: {result.overall_status.value}") print(f"置信度: {result.confidence:.2f}")
|