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
| import numpy as np import cv2 from typing import Tuple, List, Optional from enum import Enum
class SeatbeltState(Enum): """安全带状态""" CORRECT = "correct" NOT_FASTENED = "not_fastened" BUCKLE_ONLY = "buckle_only" LAP_ONLY = "lap_only" BEHIND_BACK = "behind_back"
class SeatbeltMisuseDetector: """ 安全带误用检测器 基于摄像头检测安全带路径 """ def __init__(self): self.belt_color_lower = np.array([100, 100, 100]) self.belt_color_upper = np.array([200, 200, 200]) self.shoulder_region = [(0.3, 0.2), (0.7, 0.5)] self.torso_region = [(0.3, 0.5), (0.7, 0.8)] self.lap_region = [(0.3, 0.8), (0.7, 1.0)] def detect(self, image: np.ndarray, body_keypoints: np.ndarray) -> Tuple[SeatbeltState, float]: """ 检测安全带状态 Args: image: 车内图像 body_keypoints: 身体关键点 (17, 2) Returns: (状态, 置信度) """ belt_mask = self._detect_belt_color(image) belt_path = self._analyze_belt_path(belt_mask, body_keypoints) state, confidence = self._classify_state(belt_path, body_keypoints) return state, confidence def _detect_belt_color(self, image: np.ndarray) -> np.ndarray: """ 检测安全带颜色区域 安全带通常为深色/灰色 """ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, self.belt_color_lower, self.belt_color_upper) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) return mask def _analyze_belt_path(self, belt_mask: np.ndarray, body_keypoints: np.ndarray) -> dict: """ 分析安全带路径 Returns: { 'shoulder_coverage': float, # 肩部区域覆盖 'torso_coverage': float, # 躯干区域覆盖 'lap_coverage': float, # 腰部区域覆盖 'path_continuous': bool # 路径连续性 } """ h, w = belt_mask.shape def region_coverage(mask, region): y1 = int(region[0][1] * h) y2 = int(region[1][1] * h) x1 = int(region[0][0] * w) x2 = int(region[1][0] * w) region_mask = mask[y1:y2, x1:x2] return np.sum(region_mask > 0) / region_mask.size shoulder_cov = region_coverage(belt_mask, self.shoulder_region) torso_cov = region_coverage(belt_mask, self.torso_region) lap_cov = region_coverage(belt_mask, self.lap_region) diagonal_mask = self._detect_diagonal_pattern(belt_mask) path_continuous = np.mean(diagonal_mask) > 0.1 return { 'shoulder_coverage': shoulder_cov, 'torso_coverage': torso_cov, 'lap_coverage': lap_cov, 'path_continuous': path_continuous } def _detect_diagonal_pattern(self, mask: np.ndarray) -> np.ndarray: """ 检测对角线方向的安全带模式 正确佩戴时安全带应从肩部斜跨到腰部 """ lines = cv2.HoughLinesP(mask, 1, np.pi/180, threshold=50, minLineLength=50, maxLineGap=10) if lines is None: return np.zeros_like(mask) diagonal_mask = np.zeros_like(mask) for line in lines: x1, y1, x2, y2 = line[0] angle = np.abs(np.arctan2(y2-y1, x2-x1)) if np.pi/4 < angle < np.pi/2: cv2.line(diagonal_mask, (x1, y1), (x2, y2), 255, 3) return diagonal_mask def _classify_state(self, belt_path: dict, body_keypoints: np.ndarray) -> Tuple[SeatbeltState, float]: """ 根据安全带路径判断状态 """ shoulder = belt_path['shoulder_coverage'] torso = belt_path['torso_coverage'] lap = belt_path['lap_coverage'] continuous = belt_path['path_continuous'] if continuous and shoulder > 0.1 and torso > 0.1 and lap > 0.05: return SeatbeltState.CORRECT, 0.9 if lap > 0.05 and shoulder < 0.05: return SeatbeltState.LAP_ONLY, 0.8 if shoulder < 0.05 and torso < 0.05 and lap < 0.05: return SeatbeltState.BUCKLE_ONLY, 0.7 if torso > 0.1 and not continuous: return SeatbeltState.BEHIND_BACK, 0.6 return SeatbeltState.NOT_FASTENED, 0.5
if __name__ == "__main__": detector = SeatbeltMisuseDetector() image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) body_keypoints = np.random.randint(100, 400, (17, 2)) state, confidence = detector.detect(image, body_keypoints) print(f"安全带状态: {state.value}") print(f"置信度: {confidence:.2f}")
|