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
| import numpy as np
class PressureMat: """ 压力垫数据采集 论文使用: - 座椅垫压力矩阵 - 靠背压力矩阵 分辨率:16x16 或 32x32 压力点 """ def __init__(self, resolution: tuple = (16, 16), pressure_range: tuple = (0, 100)): self.resolution = resolution self.pressure_range = pressure_range def simulate_pressure_distribution(self, body_pose: np.ndarray, seat_type: str = "soft") -> np.ndarray: """ 模拟压力分布 Args: body_pose: 身体关键点, shape=(17, 3) (x, y, z) seat_type: 座椅类型 "hard" or "soft" Returns: pressure_map: 压力分布矩阵, shape=(H, W) """ H, W = self.resolution pressure_map = np.zeros((H, W)) hip_joints = body_pose[[0, 1, 2, 3]] back_joints = body_pose[[4, 5, 6]] hip_pressure = self._project_to_seat(hip_joints, H, W) back_pressure = self._project_to_seat(back_joints, H, W) if seat_type == "soft": hip_pressure = self._gaussian_blur(hip_pressure, sigma=2.0) back_pressure = self._gaussian_blur(back_pressure, sigma=2.0) else: hip_pressure = self._gaussian_blur(hip_pressure, sigma=0.5) back_pressure = self._gaussian_blur(back_pressure, sigma=0.5) pressure_map[:H//2, :] = hip_pressure pressure_map[H//2:, :] = back_pressure return pressure_map def _project_to_seat(self, joints: np.ndarray, H: int, W: int) -> np.ndarray: """投影关键点到座椅平面""" pressure = np.zeros((H, W)) for joint in joints: x, y, z = joint grid_x = int(np.clip(x * W, 0, W - 1)) grid_y = int(np.clip(y * H, 0, H - 1)) pressure_value = max(0, 100 - z * 50) pressure[grid_y, grid_x] = pressure_value return pressure def _gaussian_blur(self, pressure: np.ndarray, sigma: float = 1.0) -> np.ndarray: """高斯模糊(模拟压力分布)""" from scipy.ndimage import gaussian_filter return gaussian_filter(pressure, sigma=sigma)
if __name__ == "__main__": mat = PressureMat(resolution=(32, 32)) body_pose = np.array([ [0.5, 0.8, 0.2], [0.6, 0.8, 0.2], [0.55, 0.7, 0.3], [0.55, 0.85, 0.2], [0.5, 0.4, 0.1], [0.55, 0.3, 0.1], [0.55, 0.2, 0.1], ]) pressure_soft = mat.simulate_pressure_distribution(body_pose, "soft") pressure_hard = mat.simulate_pressure_distribution(body_pose, "hard") print(f"软座椅压力分布形状: {pressure_soft.shape}") print(f"硬座椅压力分布形状: {pressure_hard.shape}") print(f"软座椅最大压力: {np.max(pressure_soft):.1f} kPa") print(f"硬座椅最大压力: {np.max(pressure_hard):.1f} kPa")
|