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
| import numpy as np import cv2 from typing import List, Tuple, Optional from dataclasses import dataclass from enum import Enum
class BeltStatus(Enum): """安全带状态枚举""" UNBUCKLED = "unbuckled" CORRECT = "correct" BUCKLE_ONLY = "buckle_only" BEHIND_BACK = "behind_back" LAP_ONLY = "lap_only"
@dataclass class Keypoint: """关键点""" name: str x: float y: float confidence: float = 1.0
class SeatbeltMisuseDetector: """ 安全带误用检测器 基于 RGB 摄像头检测安全带路由 """ def __init__(self): self.belt_keypoints = [ 'anchor_shoulder', 'anchor_hip', 'buckle', 'shoulder_point', 'chest_point', 'lap_point_left', 'lap_point_right', ] self.body_keypoints = [ 'left_shoulder', 'right_shoulder', 'neck', 'chest', 'left_hip', 'right_hip' ] self.min_belt_width = 20 self.max_belt_width = 80 self.belt_color_lower = np.array([0, 0, 100]) self.belt_color_upper = np.array([180, 100, 255]) def detect(self, image: np.ndarray, body_keypoints: List[Keypoint]) -> Tuple[BeltStatus, dict]: """ 检测安全带状态 Args: image: 输入图像 body_keypoints: 人体关键点列表 Returns: status: 安全带状态 info: 检测信息 """ belt_segments = self._detect_belt_segments(image) body_dict = {kp.name: kp for kp in body_keypoints} status, info = self._analyze_belt_routing(belt_segments, body_dict) return status, info def _detect_belt_segments(self, image: np.ndarray) -> List[dict]: """ 检测安全带线段 使用颜色分割 + 霍夫变换检测安全带 """ 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) edges = cv2.Canny(mask, 50, 150) lines = cv2.HoughLinesP( edges, rho=1, theta=np.pi/180, threshold=50, minLineLength=50, maxLineGap=10 ) segments = [] if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] segments.append({ 'start': (x1, y1), 'end': (x2, y2), 'length': np.sqrt((x2-x1)**2 + (y2-y1)**2), 'angle': np.arctan2(y2-y1, x2-x1) }) return segments def _analyze_belt_routing(self, belt_segments: List[dict], body_keypoints: dict) -> Tuple[BeltStatus, dict]: """ 分析安全带路由 """ info = { 'segments_count': len(belt_segments), 'shoulder_belt_detected': False, 'lap_belt_detected': False, 'buckle_detected': False } if not belt_segments: return BeltStatus.UNBUCKLED, info if 'right_shoulder' not in body_keypoints or 'left_hip' not in body_keypoints: return BeltStatus.UNBUCKLED, info right_shoulder = body_keypoints['right_shoulder'] left_ship = body_keypoints['left_hip'] neck = body_keypoints.get('neck', Keypoint('neck', (right_shoulder.x + body_keypoints['left_shoulder'].x) / 2, right_shoulder.y - 20)) shoulder_belt = self._find_diagonal_belt( belt_segments, (right_shoulder.x, right_shoulder.y), (left_hip.x, left_hip.y) ) info['shoulder_belt_detected'] = shoulder_belt is not None lap_belt = self._find_horizontal_belt( belt_segments, (body_keypoints['left_hip'].x, body_keypoints['left_hip'].y), (body_keypoints['right_hip'].x, body_keypoints['right_hip'].y) ) info['lap_belt_detected'] = lap_belt is not None if info['shoulder_belt_detected'] and info['lap_belt_detected']: if self._check_behind_back(shoulder_belt, body_keypoints): return BeltStatus.BEHIND_BACK, info else: return BeltStatus.CORRECT, info elif info['lap_belt_detected'] and not info['shoulder_belt_detected']: return BeltStatus.LAP_ONLY, info elif not info['shoulder_belt_detected'] and not info['lap_belt_detected']: if self._check_buckle_only(belt_segments, body_keypoints): return BeltStatus.BUCKLE_ONLY, info else: return BeltStatus.UNBUCKLED, info return BeltStatus.UNBUCKLED, info def _find_diagonal_belt(self, segments: List[dict], start_point: Tuple[float, float], end_point: Tuple[float, float]) -> Optional[dict]: """查找对角线安全带(肩带)""" expected_angle = np.arctan2( end_point[1] - start_point[1], end_point[0] - start_point[0] ) for seg in segments: angle_diff = abs(seg['angle'] - expected_angle) if angle_diff < np.pi / 6: return seg return None def _find_horizontal_belt(self, segments: List[dict], left_point: Tuple[float, float], right_point: Tuple[float, float]) -> Optional[dict]: """查找横向安全带(腰带)""" for seg in segments: if abs(seg['angle']) < np.pi / 6 or abs(abs(seg['angle']) - np.pi) < np.pi / 6: y_min = min(left_point[1], right_point[1]) - 30 y_max = max(left_point[1], right_point[1]) + 30 if y_min <= seg['start'][1] <= y_max: return seg return None def _check_behind_back(self, shoulder_belt: dict, body_keypoints: dict) -> bool: """ 检测安全带是否在背后 原理:如果在背后,安全带相对于身体表面的可见性会降低 """ return False def _check_buckle_only(self, segments: List[dict], body_keypoints: dict) -> bool: """ 检测仅扣扣子 原理:扣子区域有线段,但无肩带/腰带 """ return False
class SeatbeltMisuseCNN: """ 基于 CNN 的安全带误用检测 使用端到端深度学习模型 """ def __init__(self, model_path: str = None): self.model = self._build_model() if model_path: self.model.load_weights(model_path) def _build_model(self): """构建模型""" import tensorflow as tf from tensorflow.keras import layers, models image_input = layers.Input(shape=(224, 224, 3), name='image') heatmap_input = layers.Input(shape=(224, 224, 17), name='heatmap') x1 = layers.Conv2D(32, 3, strides=2, padding='same')(image_input) x1 = layers.BatchNormalization()(x1) x1 = layers.Activation('relu')(x1) x1 = layers.Conv2D(64, 3, strides=2, padding='same')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Activation('relu')(x1) x1 = layers.Conv2D(128, 3, strides=2, padding='same')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Activation('relu')(x1) x2 = layers.Conv2D(32, 3, strides=2, padding='same')(heatmap_input) x2 = layers.BatchNormalization()(x2) x2 = layers.Activation('relu')(x2) x2 = layers.Conv2D(64, 3, strides=2, padding='same')(x2) x2 = layers.BatchNormalization()(x2) x2 = layers.Activation('relu')(x2) x = layers.Concatenate()([x1, x2]) x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(128, activation='relu')(x) x = layers.Dropout(0.5)(x) output = layers.Dense(5, activation='softmax', name='status')(x) model = models.Model( inputs=[image_input, heatmap_input], outputs=output ) return model def predict(self, image: np.ndarray, keypoints: np.ndarray) -> Tuple[BeltStatus, float]: """ 预测安全带状态 Args: image: 输入图像 keypoints: 人体关键点热力图 Returns: status: 安全带状态 confidence: 置信度 """ image = cv2.resize(image, (224, 224)) image = image.astype(np.float32) / 255.0 heatmap = cv2.resize(keypoints, (224, 224)) preds = self.model.predict([ image[np.newaxis, ...], heatmap[np.newaxis, ...] ]) class_idx = np.argmax(preds[0]) confidence = preds[0][class_idx] status_map = { 0: BeltStatus.UNBUCKLED, 1: BeltStatus.CORRECT, 2: BeltStatus.BUCKLE_ONLY, 3: BeltStatus.BEHIND_BACK, 4: BeltStatus.LAP_ONLY } return status_map[class_idx], confidence
|