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
| import numpy as np import cv2
class OOP_Detector: """ OOP异常姿态检测器 基于深度摄像头检测乘员姿态异常 """ def __init__(self, depth_camera_params): """ Args: depth_camera_params: 深度摄像头参数 """ self.fx = depth_camera_params['fx'] self.fy = depth_camera_params['fy'] self.cx = depth_camera_params['cx'] self.cy = depth_camera_params['cy'] self.keypoints = [ 'nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle' ] self.danger_threshold = 0.20 def detect_keypoints_3d(self, depth_image, keypoints_2d): """ 从深度图像提取3D关键点 Args: depth_image: 深度图像 (H, W), 单位mm keypoints_2d: 2D关键点坐标 (N, 2) Returns: keypoints_3d: 3D关键点坐标 (N, 3) """ keypoints_3d = [] for (u, v) in keypoints_2d: if u < 0 or u >= depth_image.shape[1] or v < 0 or v >= depth_image.shape[0]: keypoints_3d.append([0, 0, 0]) continue z = depth_image[int(v), int(u)] x = (u - self.cx) * z / self.fx y = (v - self.cy) * z / self.fy keypoints_3d.append([x, y, z]) return np.array(keypoints_3d) def check_feet_on_dashboard(self, keypoints_3d): """ 检测脚是否放在仪表板上 Args: keypoints_3d: 3D关键点 Returns: is_oop: 是否OOP foot_position: 脚部位置 (left/right/center) """ left_ankle_idx = self.keypoints.index('left_ankle') right_ankle_idx = self.keypoints.index('right_ankle') left_ankle = keypoints_3d[left_ankle_idx] right_ankle = keypoints_3d[right_ankle_idx] dashboard_height_min = 0.30 dashboard_height_max = 0.50 left_foot_high = dashboard_height_min < -left_ankle[1] < dashboard_height_max right_foot_high = dashboard_height_min < -right_ankle[1] < dashboard_height_max if left_foot_high or right_foot_high: if left_foot_high and right_foot_high: foot_position = 'center' elif left_foot_high: foot_position = 'left' else: foot_position = 'right' return True, foot_position return False, None def check_upper_body_too_close(self, keypoints_3d): """ 检测上身是否过度前倾(距仪表板<20cm) Args: keypoints_3d: 3D关键点 Returns: is_oop: 是否OOP distance: 距离仪表板距离 (m) """ left_shoulder_idx = self.keypoints.index('left_shoulder') right_shoulder_idx = self.keypoints.index('right_shoulder') nose_idx = self.keypoints.index('nose') left_shoulder = keypoints_3d[left_shoulder_idx] right_shoulder = keypoints_3d[right_shoulder_idx] nose = keypoints_3d[nose_idx] upper_body_center = (left_shoulder + right_shoulder + nose) / 3 distance_to_dashboard = upper_body_center[0] if distance_to_dashboard < self.danger_threshold: return True, distance_to_dashboard return False, distance_to_dashboard def analyze_posture(self, depth_image, keypoints_2d): """ 综合姿态分析 Args: depth_image: 深度图像 keypoints_2d: 2D关键点 Returns: result: 检测结果 """ keypoints_3d = self.detect_keypoints_3d(depth_image, keypoints_2d) feet_on_dashboard, foot_position = self.check_feet_on_dashboard(keypoints_3d) upper_body_too_close, distance = self.check_upper_body_too_close(keypoints_3d) is_oop = feet_on_dashboard or upper_body_too_close result = { 'is_oop': is_oop, 'oop_type': [], 'details': {} } if feet_on_dashboard: result['oop_type'].append('feet_on_dashboard') result['details']['foot_position'] = foot_position if upper_body_too_close: result['oop_type'].append('upper_body_too_close') result['details']['distance_to_dashboard'] = distance return result
camera_params = { 'fx': 525.0, 'fy': 525.0, 'cx': 319.5, 'cy': 239.5 }
detector = OOP_Detector(camera_params)
depth_image = np.random.randint(500, 2000, size=(480, 640)).astype(np.uint16)
keypoints_2d = np.random.rand(17, 2) * [640, 480]
result = detector.analyze_posture(depth_image, keypoints_2d) print(f"OOP检测结果: {result}")
|