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
| DANGEROUS_BEHAVIORS = { "reaching_back": { "description": "伸手到后排", "key_points": ["shoulder", "elbow", "hand"], "threshold": { "hand_y": "< shoulder_y - 0.3", "rotation_angle": "> 45°" }, "risk_level": "medium" }, "eating_drinking": { "description": "吃东西/喝水", "key_points": ["mouth", "hand"], "threshold": { "hand_mouth_distance": "< 0.15m", "duration": "> 3s" }, "risk_level": "medium" }, "phone_use": { "description": "使用手机", "key_points": ["hand", "ear", "eye"], "threshold": { "hand_ear_distance": "< 0.2m", "hand_eye_distance": "< 0.3m", "duration": "> 2s" }, "risk_level": "high" }, "smoking": { "description": "吸烟", "key_points": ["mouth", "hand"], "threshold": { "hand_mouth_distance": "< 0.1m", "repetitive_motion": True }, "risk_level": "medium" }, "adjusting_controls": { "description": "调整中控", "key_points": ["hand", "shoulder"], "threshold": { "hand_position": "dashboard_area", "duration": "> 5s" }, "risk_level": "low" }, "head_down": { "description": "头部下垂(疲劳)", "key_points": ["head", "neck"], "threshold": { "head_angle": "> 30° downward", "duration": "> 5s" }, "risk_level": "high" } }
class DepthPoseEstimator: """ 深度摄像头3D姿态估计 基于Nature 2026论文实现 """ def __init__(self): self.keypoints = [ 'head', 'neck', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_hand', 'right_hand', 'spine', 'hip' ] def estimate_pose(self, depth_image: np.ndarray) -> dict: """ 从深度图估计3D姿态 Args: depth_image: 深度图 (H, W), 单位mm Returns: pose: { 'keypoints_3d': (N, 3), # 3D坐标 'confidence': (N,), 'behavior': str, 'risk_level': str } """ person_mask = self._segment_person(depth_image) keypoints_2d = self._detect_keypoints_2d(depth_image, person_mask) keypoints_3d = self._lift_to_3d(keypoints_2d, depth_image) behavior, risk_level = self._classify_behavior(keypoints_3d) return { 'keypoints_3d': keypoints_3d, 'behavior': behavior, 'risk_level': risk_level, 'timestamp': time.time() } def _segment_person(self, depth_image: np.ndarray) -> np.ndarray: """人体分割""" seat_depth_range = (0.5, 1.5) person_mask = (depth_image > seat_depth_range[0] * 1000) & \ (depth_image < seat_depth_range[1] * 1000) return person_mask def _detect_keypoints_2d(self, depth_image: np.ndarray, mask: np.ndarray) -> np.ndarray: """2D关键点检测""" pass def _lift_to_3d(self, keypoints_2d: np.ndarray, depth_image: np.ndarray) -> np.ndarray: """提升到3D""" keypoints_3d = np.zeros((len(self.keypoints), 3)) for i, (x, y) in enumerate(keypoints_2d): z = depth_image[int(y), int(x)] / 1000.0 keypoints_3d[i] = [x, y, z] return keypoints_3d def _classify_behavior(self, keypoints_3d: np.ndarray) -> tuple: """姿态分类""" head = keypoints_3d[0] left_hand = keypoints_3d[6] right_hand = keypoints_3d[7] mouth_estimate = keypoints_3d[0] for behavior_name, config in DANGEROUS_BEHAVIORS.items(): if self._check_behavior(keypoints_3d, config): return behavior_name, config['risk_level'] return 'normal', 'none' def _check_behavior(self, keypoints: np.ndarray, config: dict) -> bool: """检查特定行为""" threshold = config['threshold'] if 'hand_mouth_distance' in threshold: hand = keypoints[6] mouth = keypoints[0] distance = np.linalg.norm(hand - mouth) if distance < threshold['hand_mouth_distance']: return True if 'head_angle' in threshold: head = keypoints[0] neck = keypoints[1] angle = self._calculate_angle(head, neck) if angle > threshold['head_angle']: return True return False def _calculate_angle(self, point1: np.ndarray, point2: np.ndarray) -> float: """计算两点间角度""" direction = point1 - point2 angle = np.arctan2(direction[2], direction[1]) * 180 / np.pi return abs(angle)
if __name__ == "__main__": estimator = DepthPoseEstimator() depth_image = np.zeros((480, 640), dtype=np.uint16) depth_image[100:400, 100:500] = 1000 pose = estimator.estimate_pose(depth_image) print(f"检测行为: {pose['behavior']}") print(f"风险等级: {pose['risk_level']}")
|