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
| import numpy as np from typing import Tuple
class VoxelHumanModel: """ 体素人体模型 论文方法:将人体表示为 3D 体素网格 优势: - 可表示完整 3D 结构 - 对遮挡鲁棒 - 可用于异常姿态识别 应用:座舱乘员 3D 建模 """ def __init__(self, voxel_size: float = 0.05, grid_dims: Tuple[int, int, int] = (64, 64, 48)): self.voxel_size = voxel_size self.grid_dims = grid_dims self.body_dims = { "height": 1.75, "width": 0.5, "depth": 0.3 } def estimate_voxel_occupancy(self, pose_3d: np.ndarray, body_model: str = "ellipsoid") -> np.ndarray: """ 估计体素占用 Args: pose_3d: 3D 关键点坐标, shape=(J, 3), 单位:米 body_model: 人体模型类型 Returns: occupancy_grid: 体素占用网格, shape=(grid_dims) """ occupancy = np.zeros(self.grid_dims) for joint_idx in range(len(pose_3d)): joint_pos = pose_3d[joint_idx] voxel_idx = self._world_to_voxel(joint_pos) self._fill_joint_region(occupancy, voxel_idx, joint_idx) self._connect_limbs(occupancy, pose_3d) return occupancy def _world_to_voxel(self, pos: np.ndarray) -> Tuple[int, int, int]: """世界坐标转体素索引""" voxel_idx = ( int((pos[0] + 1) / (2 * self.voxel_size)), int((pos[1] + 1) / (2 * self.voxel_size)), int((pos[2] + 0) / (self.grid_dims[2] * self.voxel_size)) ) voxel_idx = ( max(0, min(self.grid_dims[0] - 1, voxel_idx[0])), max(0, min(self.grid_dims[1] - 1, voxel_idx[1])), max(0, min(self.grid_dims[2] - 1, voxel_idx[2])) ) return voxel_idx def _fill_joint_region(self, occupancy: np.ndarray, voxel_idx: Tuple[int, int, int], joint_idx: int): """在关节点周围填充体素""" joint_radii = { "head": 3, "shoulder": 2, "elbow": 2, "wrist": 1, "hip": 2, "knee": 2, "ankle": 1, "spine": 2 } radius = joint_radii.get(list(joint_radii.keys())[joint_idx], 2) for dx in range(-radius, radius + 1): for dy in range(-radius, radius + 1): for dz in range(-radius, radius + 1): if dx*dx + dy*dy + dz*dz <= radius*radius: x, y, z = voxel_idx[0] + dx, voxel_idx[1] + dy, voxel_idx[2] + dz if 0 <= x < self.grid_dims[0] and \ 0 <= y < self.grid_dims[1] and \ 0 <= z < self.grid_dims[2]: occupancy[x, y, z] = 1.0 def _connect_limbs(self, occupancy: np.ndarray, pose_3d: np.ndarray): """连接相邻关节形成肢体""" limb_connections = [ (0, 1), (1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), ] for start_idx, end_idx in limb_connections: start_pos = pose_3d[start_idx] end_pos = pose_3d[end_idx] num_steps = int(np.linalg.norm(end_pos - start_pos) / self.voxel_size) for i in range(num_steps): interp_pos = start_pos + (end_pos - start_pos) * i / num_steps voxel_idx = self._world_to_voxel(interp_pos) occupancy[voxel_idx] = 1.0 def detect_anomalous_posture(self, occupancy: np.ndarray, seat_model: np.ndarray) -> dict: """ 检测异常姿态 Args: occupancy: 人体体素占用 seat_model: 座椅体素模型 Returns: dict: { "is_anomalous": bool, "posture_type": str, # "lying", "standing", "climbing", "normal" "danger_zones": List[Tuple] } """ intersection = occupancy * seat_model in_seat_ratio = np.sum(intersection) / np.sum(occupancy) if in_seat_ratio < 0.5: return { "is_anomalous": True, "posture_type": "outside_seat", "danger_zones": self._find_outside_regions(occupancy, seat_model) } height_extent = np.max(np.where(np.sum(occupancy, axis=(0, 1)) > 0)[0]) - \ np.min(np.where(np.sum(occupancy, axis=(0, 1)) > 0)[0]) if height_extent > self.grid_dims[2] * 0.8: return { "is_anomalous": True, "posture_type": "lying_down", "danger_zones": [] } head_z = np.max(np.where(np.sum(occupancy, axis=(0, 1)) > 0)) if head_z > self.grid_dims[2] * 0.9: return { "is_anomalous": True, "posture_type": "standing_up", "danger_zones": [] } return { "is_anomalous": False, "posture_type": "normal_seated", "danger_zones": [] } def _find_outside_regions(self, occupancy: np.ndarray, seat_model: np.ndarray) -> list: """找出人体在座椅外的危险区域""" outside = occupancy * (1 - seat_model) danger_zones = [] for z in range(self.grid_dims[2]): outside_slice = outside[:, :, z] if np.sum(outside_slice) > 0: coords = np.where(outside_slice > 0) danger_zones.append((coords[0][0], coords[1][0], z)) return danger_zones[:5]
if __name__ == "__main__": voxel_model = VoxelHumanModel() normal_pose = np.array([ [0.0, 0.5, 0.8], [0.0, 0.3, 0.6], [0.0, 0.1, 0.5], [0.2, 0.1, 0.4], [0.3, 0.1, 0.3], [0.0, 0.0, 0.3], [0.1, 0.0, 0.2], [0.1, 0.0, 0.1], ]) seat_model = np.zeros((64, 64, 48)) seat_model[10:40, 10:50, 10:30] = 1.0 normal_occupancy = voxel_model.estimate_voxel_occupancy(normal_pose) normal_result = voxel_model.detect_anomalous_posture(normal_occupancy, seat_model) print("=== 正常坐姿 ===") print(f"异常姿态: {normal_result['is_anomalous']}") print(f"姿态类型: {normal_result['posture_type']}") lying_pose = np.array([ [0.0, 0.1, 0.8], [0.0, 0.1, 0.6], [0.0, 0.1, 0.5], [0.2, 0.1, 0.4], [0.3, 0.1, 0.3], [0.0, 0.1, 0.3], [0.1, 0.1, 0.2], [0.1, 0.1, 0.1], ]) lying_occupancy = voxel_model.estimate_voxel_occupancy(lying_pose) lying_result = voxel_model.detect_anomalous_posture(lying_occupancy, seat_model) print("\n=== 躺倒姿态 ===") print(f"异常姿态: {lying_result['is_anomalous']}") print(f"姿态类型: {lying_result['posture_type']}")
|