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
| import numpy as np from typing import Tuple, Optional
class OccupantPostureMonitor: """ 乘员姿态监控系统 基于 3D 深度摄像头检测危险坐姿 """ def __init__(self): self.danger_distance = 20.0 self.dashboard_plane = np.array([0, 0, 1, -30]) def analyze_posture(self, depth_map: np.ndarray, body_keypoints: dict) -> dict: """ 分析乘员姿态 Args: depth_map: 深度图 (H, W), 单位 cm body_keypoints: 身体关键点 - head: (x, y, z) 头部位置 - torso: [(x,y,z), ...] 躯干关键点 - feet: [(x,y,z), ...] 脚部位置 Returns: 姿态分析结果 """ result = { 'is_safe': True, 'warnings': [], 'min_distance': float('inf'), 'feet_on_dash': False, 'lean_forward': False } if 'feet' in body_keypoints: result['feet_on_dash'] = self._check_feet_on_dashboard( body_keypoints['feet'], depth_map ) if result['feet_on_dash']: result['warnings'].append('FEET_ON_DASHBOARD') result['is_safe'] = False if 'head' in body_keypoints and 'torso' in body_keypoints: result['lean_forward'], min_dist = self._check_lean_forward( body_keypoints['head'], body_keypoints['torso'], depth_map ) result['min_distance'] = min_dist if result['lean_forward']: result['warnings'].append('LEAN_FORWARD') result['is_safe'] False return result def _check_feet_on_dashboard(self, feet_positions: list, depth_map: np.ndarray) -> bool: """ 检测脚是否放在仪表板上 仪表板区域定义为: - z < 40cm (距摄像头较近) - y > 车辆中线 """ for foot in feet_positions: x, y, z = foot if z < 40: if 0 <= int(y) < depth_map.shape[0] and 0 <= int(x) < depth_map.shape[1]: depth_value = depth_map[int(y), int(x)] if depth_value < 40: return True return False def _check_lean_forward(self, head_pos: tuple, torso_points: list, depth_map: np.ndarray) -> Tuple[bool, float]: """ 检测身体前倾 Returns: (是否前倾, 最小距离) """ hx, hy, hz = head_pos dashboard_z = 30 head_distance = hz - dashboard_z if torso_points: torso_distances = [p[2] - dashboard_z for p in torso_points] min_torso_distance = min(torso_distances) else: min_torso_distance = head_distance min_distance = min(head_distance, min_torso_distance) is_dangerous = min_distance < self.danger_distance return is_dangerous, min_distance def get_airbag_strategy(self, posture_result: dict, occupant_size: str) -> dict: """ 根据姿态和乘员体型确定安全气囊策略 Args: posture_result: 姿态分析结果 occupant_size: 乘员体型 ('small', 'medium', 'large') Returns: 安全气囊策略 """ strategy = { 'airbag_enabled': True, 'deployment_force': 'standard', 'pretensioner_timing': 'normal', 'suppress_warning': False } if not posture_result['is_safe']: if posture_result['feet_on_dash']: strategy['airbag_enabled'] = False strategy['suppress_warning'] = True elif posture_result['lean_forward']: strategy['deployment_force'] = 'low' if occupant_size == 'small': if strategy['deployment_force'] == 'standard': strategy['deployment_force'] = 'low' strategy['pretensioner_timing'] = 'early' elif occupant_size == 'large': strategy['deployment_force'] = 'high' return strategy
if __name__ == "__main__": monitor = OccupantPostureMonitor() depth_map = np.random.uniform(30, 100, (480, 640)) body_keypoints = { 'head': (320, 240, 35), 'torso': [ (320, 300, 40), (320, 350, 45), (320, 400, 50) ], 'feet': [] } result = monitor.analyze_posture(depth_map, body_keypoints) print(f"姿态安全: {result['is_safe']}") print(f"警告: {result['warnings']}") print(f"最小距离: {result['min_distance']:.1f} cm") strategy = monitor.get_airbag_strategy(result, 'medium') print(f"安全气囊策略: {strategy}")
|