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
| import numpy as np
class LapOnlyDetector: """ 仅扣腰带检测器 Euro NCAP 2026 场景2 检测方法: 1. 视觉检测肩带路径 2. 腰带张力正常 3. 肩带不在胸前位置 """ def __init__(self, shoulder_path_model: str = None): self.normal_shoulder_path = [ (0.3, 0.2), (0.5, 0.3), (0.7, 0.4), ] def detect_visual(self, keypoints: np.ndarray, belt_visual_path: np.ndarray) -> dict: """ 视觉检测肩带路径 Args: keypoints: 身体关键点, shape=(N, 3) belt_visual_path: 安全带视觉路径, shape=(M, 2) Returns: result: {"misuse_type": str, "confidence": float} """ shoulder_point = keypoints[5] chest_region = self._get_chest_region(shoulder_point) belt_in_chest = self._check_belt_in_region(belt_visual_path, chest_region) if not belt_in_chest: return {"misuse_type": "lap_only", "confidence": 0.85} return {"misuse_type": "correct", "confidence": 0.9} def _get_chest_region(self, shoulder_point: np.ndarray) -> dict: """获取胸前区域""" x, y, z = shoulder_point return { "x_min": x - 0.1, "x_max": x + 0.1, "y_min": y + 0.1, "y_max": y + 0.3 } def _check_belt_in_region(self, belt_path: np.ndarray, region: dict) -> bool: """检查安全带是否在区域内""" for point in belt_path: x, y = point if region["x_min"] <= x <= region["x_max"] and \ region["y_min"] <= y <= region["y_max"]: return True return False
if __name__ == "__main__": detector = LapOnlyDetector() keypoints = np.array([ [0.0, 0.0, 0.0], [0.3, 0.2, 0.0], [0.7, 0.2, 0.0], [0.5, 0.5, 0.0], ]) normal_path = np.array([ [0.3, 0.2], [0.5, 0.3], [0.7, 0.4], ]) misuse_path = np.array([ [0.3, 0.2], [0.3, 0.4], [0.7, 0.4], ]) result_normal = detector.detect_visual(keypoints, normal_path) result_misuse = detector.detect_visual(keypoints, misuse_path) print(f"正常佩戴: {result_normal}") print(f"仅扣腰带: {result_misuse}")
|