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
| """ FSR压力传感器乘员分类系统
部署位置:座椅坐垫下方 传感器阵列:8x8 或 16x16 """
import numpy as np from typing import Tuple, Dict from scipy import ndimage
class FSROccupantClassifier: """FSR压力传感器乘员分类器""" def __init__( self, grid_size: Tuple[int, int] = (8, 8), adult_threshold: float = 30.0, child_threshold: float = 15.0 ): self.grid_size = grid_size self.adult_threshold = adult_threshold self.child_threshold = child_threshold self.calibration_matrix = np.ones(grid_size) self.zero_offset = np.zeros(grid_size) def calibrate(self, empty_readings: np.ndarray): """ 校准传感器(空座椅状态) Args: empty_readings: [N, H, W] 多次空座椅读数 """ self.zero_offset = np.mean(empty_readings, axis=0) center = self.grid_size[0] // 2, self.grid_size[1] // 2 reference = np.mean(empty_readings[:, center[0], center[1]]) self.calibration_matrix = reference / (self.zero_offset + 1e-6) def read_pressure(self, raw_data: np.ndarray) -> np.ndarray: """ 读取并处理压力数据 Args: raw_data: [H, W] 原始传感器读数 Returns: pressure: [H, W] 处理后的压力值 """ pressure = raw_data - self.zero_offset pressure = pressure * self.calibration_matrix pressure = np.maximum(pressure, 0) return pressure def estimate_weight(self, pressure: np.ndarray) -> float: """ 估计乘员体重 Args: pressure: [H, W] 压力分布 Returns: weight: 估计体重(kg) """ total_pressure = np.sum(pressure) calibration_factor = 0.5 weight = total_pressure * calibration_factor return weight def classify_occupant(self, weight: float) -> str: """ 分类乘员类型 Args: weight: 估计体重 Returns: occupant_type: 成人/儿童/空座椅 """ if weight < 5.0: return 'EMPTY' elif weight < self.child_threshold: return 'CHILD' elif weight < self.adult_threshold: return 'SMALL_ADULT' else: return 'ADULT' def detect_child_seat(self, pressure: np.ndarray) -> Tuple[bool, str]: """ 检测儿童座椅 Args: pressure: [H, W] 压力分布 Returns: is_child_seat: 是否检测到儿童座椅 seat_type: 座椅类型(后向/前向) """ pressure_std = np.std(pressure) pressure_max = np.max(pressure) pressure_mean = np.mean(pressure[pressure > 0]) threshold = pressure_mean * 2 high_pressure_points = pressure > threshold num_high_points = np.sum(high_pressure_points) is_child_seat = ( pressure_std > 10 and num_high_points <= 4 and 5 < self.estimate_weight(pressure) < 20 ) seat_type = 'FORWARD_FACING' if is_child_seat else 'NONE' return is_child_seat, seat_type def analyze_position(self, pressure: np.ndarray) -> Dict: """ 分析乘员位置 Args: pressure: [H, W] 压力分布 Returns: position: 位置信息 """ total = np.sum(pressure) if total < 1e-6: return {'x': 0, 'y': 0, 'is_centered': True} y_coords, x_coords = np.meshgrid( np.arange(self.grid_size[0]), np.arange(self.grid_size[1]), indexing='ij' ) center_x = np.sum(x_coords * pressure) / total center_y = np.sum(y_coords * pressure) / total expected_x = self.grid_size[1] / 2 expected_y = self.grid_size[0] / 2 offset = np.sqrt((center_x - expected_x)**2 + (center_y - expected_y)**2) is_centered = offset < 2.0 return { 'x': center_x, 'y': center_y, 'is_centered': is_centered, 'offset': offset } def process(self, raw_data: np.ndarray) -> Dict: """ 完整处理流程 Args: raw_data: [H, W] 原始传感器数据 Returns: result: 处理结果 """ pressure = self.read_pressure(raw_data) weight = self.estimate_weight(pressure) occupant_type = self.classify_occupant(weight) is_child_seat, seat_type = self.detect_child_seat(pressure) position = self.analyze_position(pressure) return { 'weight': weight, 'occupant_type': occupant_type, 'is_child_seat': is_child_seat, 'seat_type': seat_type, 'position': position, 'pressure_map': pressure }
if __name__ == "__main__": classifier = FSROccupantClassifier(grid_size=(8, 8)) empty_readings = np.random.rand(10, 8, 8) * 0.5 classifier.calibrate(empty_readings) print("=" * 60) print("乘员分类测试") print("=" * 60) empty_seat = np.random.rand(8, 8) * 0.3 result = classifier.process(empty_seat) print(f"\n空座椅: 体重={result['weight']:.1f}kg, " f"类型={result['occupant_type']}") adult_pressure = np.random.rand(8, 8) * 10 + 20 result = classifier.process(adult_pressure) print(f"成人: 体重={result['weight']:.1f}kg, " f"类型={result['occupant_type']}") child_pressure = np.random.rand(8, 8) * 5 + 10 result = classifier.process(child_pressure) print(f"儿童: 体重={result['weight']:.1f}kg, " f"类型={result['occupant_type']}")
|