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
| """ 安全带误用检测算法
支持检测: 1. 肩带在背后 2. 肩带在腋下 3. 腰带位置过高 4. 安全带松动 """
import numpy as np from typing import Tuple, List, Dict import cv2
class SeatbeltDetector: """安全带误用检测器""" def __init__(self): self.keypoint_model = None self.segmentation_model = None self.shoulder_angle_range = (20, 45) self.lap_position_range = (0.4, 0.6) def detect_keypoints(self, image: np.ndarray) -> Dict[str, np.ndarray]: """ 检测人体关键点 Args: image: 输入图像 Returns: keypoints: 关键点字典 """ return { 'left_shoulder': np.array([100, 150]), 'right_shoulder': np.array([200, 150]), 'chest_center': np.array([150, 180]), 'left_hip': np.array([120, 280]), 'right_hip': np.array([180, 280]) } def segment_seatbelt(self, image: np.ndarray) -> np.ndarray: """ 分割安全带 Args: image: 输入图像 Returns: mask: 安全带掩码 """ mask = np.zeros(image.shape[:2], dtype=np.uint8) cv2.line(mask, (120, 120), (140, 250), 1, 3) cv2.line(mask, (110, 250), (190, 250), 1, 3) return mask def analyze_geometry( self, keypoints: Dict[str, np.ndarray], seatbelt_mask: np.ndarray ) -> Dict: """ 分析安全带几何关系 Returns: analysis: 分析结果 """ lines = cv2.HoughLinesP( seatbelt_mask, rho=1, theta=np.pi/180, threshold=50, minLineLength=30, maxLineGap=10 ) if lines is None: return {'status': 'NO_SEATBELT'} shoulder_belt = None lap_belt = None for line in lines: x1, y1, x2, y2 = line[0] angle = np.abs(np.arctan2(y2-y1, x2-x1) * 180 / np.pi) if angle > 60: shoulder_belt = line[0] elif angle < 30: lap_belt = line[0] return { 'shoulder_belt': shoulder_belt, 'lap_belt': lap_belt, 'keypoints': keypoints } def classify_misuse(self, analysis: Dict) -> Tuple[str, float]: """ 分类误用类型 Args: analysis: 几何分析结果 Returns: misuse_type: 误用类型 confidence: 置信度 """ if analysis['status'] == 'NO_SEATBELT': return 'NO_SEATBELT', 0.95 shoulder_belt = analysis['shoulder_belt'] lap_belt = analysis['lap_belt'] keypoints = analysis['keypoints'] if shoulder_belt is None: return 'SHOULDER_BELT_MISSING', 0.80 shoulder_center = (keypoints['left_shoulder'] + keypoints['right_shoulder']) / 2 chest = keypoints['chest_center'] belt_y_at_chest = shoulder_belt[1] + (shoulder_belt[3] - shoulder_belt[1]) * \ (chest[0] - shoulder_belt[0]) / (shoulder_belt[2] - shoulder_belt[0] + 1e-6) if belt_y_at_chest < chest[1] - 30: return 'SHOULDER_BEHIND_BACK', 0.75 if belt_y_at_chest > chest[1] + 50: return 'SHOULDER_UNDER_ARM', 0.70 if lap_belt is None: return 'LAP_BELT_MISSING', 0.75 hip_center = (keypoints['left_hip'] + keypoints['right_hip']) / 2 lap_y = (lap_belt[1] + lap_belt[3]) / 2 if lap_y < hip_center[1] - 50: return 'LAP_BELT_TOO_HIGH', 0.70 return 'CORRECT', 0.90 def detect(self, image: np.ndarray) -> Dict: """ 完整检测流程 Args: image: 输入图像 Returns: result: 检测结果 """ keypoints = self.detect_keypoints(image) seatbelt_mask = self.segment_seatbelt(image) analysis = self.analyze_geometry(keypoints, seatbelt_mask) misuse_type, confidence = self.classify_misuse(analysis) return { 'misuse_type': misuse_type, 'confidence': confidence, 'keypoints': keypoints, 'seatbelt_mask': seatbelt_mask }
if __name__ == "__main__": detector = SeatbeltDetector() image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) result = detector.detect(image) print("=" * 60) print("安全带误用检测结果") print("=" * 60) print(f"误用类型: {result['misuse_type']}") print(f"置信度: {result['confidence']:.2f}")
|