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
| """ 安全带误用视觉检测算法 """
import numpy as np from typing import Tuple, List, Optional from dataclasses import dataclass from enum import IntEnum
class BeltStatus(IntEnum): """安全带状态""" NORMAL = 0 UNBUCKLED = 1 BEHIND_BACK = 2 UNDER_ARM = 3 TOO_LOOSE = 4 UNKNOWN = 5
@dataclass class BeltSegment: """安全带线段""" start: Tuple[int, int] end: Tuple[int, int] confidence: float type: str
class BeltMisuseDetector: """ 安全带误用检测器 基于 YOLOv8 关键点检测 + 规则判断 """ KEYPOINTS = { 'left_shoulder': 0, 'right_shoulder': 1, 'neck': 2, 'left_hip': 3, 'right_hip': 4, 'belt_buckle': 5, 'belt_shoulder_point': 6, 'belt_lap_left': 7, 'belt_lap_right': 8 } def __init__(self): self.model = None self.thresholds = { 'buckle_distance': 50, 'shoulder_angle': 15, 'lap_width': 100, 'loose_distance': 80 } def detect( self, image: np.ndarray, keypoints: Optional[np.ndarray] = None ) -> dict: """ 检测安全带状态 Args: image: 输入图像 keypoints: 预计算的关键点(可选) Returns: { 'status': BeltStatus, 'confidence': float, 'details': dict } """ if keypoints is None: keypoints = self._detect_keypoints(image) belt_segments = self._detect_belt_segments(image, keypoints) status, confidence, details = self._classify_status(keypoints, belt_segments) return { 'status': status, 'confidence': confidence, 'details': details } def _detect_keypoints(self, image: np.ndarray) -> np.ndarray: """检测人体和安全带关键点""" return np.zeros((9, 3)) def _detect_belt_segments( self, image: np.ndarray, keypoints: np.ndarray ) -> List[BeltSegment]: """检测安全带线段""" return [] def _classify_status( self, keypoints: np.ndarray, belt_segments: List[BeltSegment] ) -> Tuple[BeltStatus, float, dict]: """ 分类安全带状态 Returns: (status, confidence, details) """ details = {} buckle_detected = keypoints[5, 2] > 0.5 details['buckle_detected'] = buckle_detected if not buckle_detected: return BeltStatus.UNBUCKLED, 0.95, details shoulder_status = self._check_shoulder_belt(keypoints, belt_segments) details['shoulder_status'] = shoulder_status if shoulder_status == 'behind': return BeltStatus.BEHIND_BACK, 0.85, details elif shoulder_status == 'under_arm': return BeltStatus.UNDER_ARM, 0.80, details lap_status = self._check_lap_belt(keypoints, belt_segments) details['lap_status'] = lap_status looseness = self._check_looseness(keypoints, belt_segments) details['looseness'] = looseness if looseness > self.thresholds['loose_distance']: return BeltStatus.TOO_LOOSE, 0.75, details return BeltStatus.NORMAL, 0.90, details def _check_shoulder_belt( self, keypoints: np.ndarray, belt_segments: List[BeltSegment] ) -> str: """ 检查肩带位置 Returns: 'normal', 'behind', 'under_arm', 'unknown' """ left_shoulder = keypoints[0, :2] right_shoulder = keypoints[1, :2] neck = keypoints[2, :2] belt_shoulder = keypoints[6, :2] left_dist = np.linalg.norm(belt_shoulder - left_shoulder) right_dist = np.linalg.norm(belt_shoulder - right_shoulder) if left_dist < self.thresholds['buckle_distance']: return 'normal' elif right_dist < self.thresholds['buckle_distance']: return 'normal' elif belt_shoulder[1] < neck[1] - 50: return 'behind' elif belt_shoulder[1] > left_shoulder[1] + 30: return 'under_arm' else: return 'unknown' def _check_lap_belt( self, keypoints: np.ndarray, belt_segments: List[BeltSegment] ) -> str: """检查腰带位置""" return 'normal' def _check_looseness( self, keypoints: np.ndarray, belt_segments: List[BeltSegment] ) -> float: """检查松紧度""" return 0.0
if __name__ == "__main__": detector = BeltMisuseDetector() print("安全带误用检测测试:") print("=" * 50) test_cases = { '正常佩戴': BeltStatus.NORMAL, '未系安全带': BeltStatus.UNBUCKLED, '安全带在背后': BeltStatus.BEHIND_BACK, '安全带在腋下': BeltStatus.UNDER_ARM, } for name, expected in test_cases.items(): print(f"\n{name}:") print(f" 预期状态: {expected}")
|