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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
| import numpy as np from scipy import signal
class WiFiDensePoseProcessor: """ WiFi DensePose信号处理器 处理流程: 1. CSI采集 2. 预处理(去噪、插值) 3. 特征提取 4. 姿态估计 """ def __init__(self, config: dict): self.config = config self.csi_buffer = [] self.buffer_size = 100 self.denoise_window = 5 self.interpolation_factor = 4 def process_csi(self, csi_data: np.ndarray) -> dict: """ 处理CSI数据 Args: csi_data: CSI数据 (packets, subcarriers, rx_antennas) Returns: result: 姿态估计结果 """ csi_clean = self.preprocess(csi_data) features = self.extract_features(csi_clean) keypoints = self.estimate_pose(features) densepose = self.generate_densepose(features) return { 'keypoints': keypoints, 'densepose': densepose, 'confidence': self.compute_confidence(features) } def preprocess(self, csi: np.ndarray) -> np.ndarray: """ CSI预处理 Args: csi: 原始CSI数据 Returns: csi_clean: 清洗后的CSI """ csi_median = np.median(csi, axis=0, keepdims=True) csi = np.where(np.abs(csi - csi_median) > 3 * np.std(csi), csi_median, csi) fs = self.config.get('sampling_rate', 1000) cutoff = 100 b, a = signal.butter(4, cutoff / (fs / 2), btype='low') csi_filtered = signal.filtfilt(b, a, csi, axis=0) amplitude = np.abs(csi_filtered) phase = np.angle(csi_filtered) phase_unwrapped = np.unwrap(phase, axis=0) amplitude_norm = (amplitude - np.mean(amplitude)) / (np.std(amplitude) + 1e-10) phase_norm = (phase_unwrapped - np.mean(phase_unwrapped)) / (np.std(phase_unwrapped) + 1e-10) csi_clean = np.concatenate([ amplitude_norm[..., np.newaxis], phase_norm[..., np.newaxis] ], axis=-1) return csi_clean def extract_features(self, csi: np.ndarray) -> np.ndarray: """ 提取CSI特征 Args: csi: 预处理后的CSI Returns: features: 特征向量 """ features = [] features.append(np.mean(csi, axis=0)) features.append(np.std(csi, axis=0)) features.append(np.max(csi, axis=0)) features.append(np.min(csi, axis=0)) fft_result = np.fft.fft(csi, axis=0) fft_magnitude = np.abs(fft_result) features.append(np.mean(fft_magnitude[:10], axis=0)) if csi.shape[2] >= 2: features.append(csi[:, :, 0] - csi[:, :, 1]) features = np.concatenate([f.reshape(-1) for f in features]) return features def estimate_pose(self, features: np.ndarray) -> np.ndarray: """ 估计人体关键点 Args: features: CSI特征 Returns: keypoints: 关键点坐标 (17, 3) [x, y, confidence] """ keypoints = np.zeros((17, 3)) keypoints[0] = [0.5, 0.3, 0.8] keypoints[5] = [0.4, 0.5, 0.9] keypoints[6] = [0.6, 0.5, 0.9] keypoints[11] = [0.4, 0.7, 0.9] keypoints[12] = [0.6, 0.7, 0.9] return keypoints def generate_densepose(self, features: np.ndarray) -> np.ndarray: """ 生成密集姿态图 Args: features: CSI特征 Returns: densepose: 密集姿态图 (H, W) """ densepose = np.zeros((56, 56)) return densepose def compute_confidence(self, features: np.ndarray) -> float: """计算检测置信度""" return 0.85
class OMSWiFiSystem: """ 基于WiFi的乘员监测系统 """ def __init__(self, wifi_config: dict): self.processor = WiFiDensePoseProcessor(wifi_config) self.occupant_state = { 'driver': {'present': False, 'pose': None}, 'passenger_front': {'present': False, 'pose': None}, 'passenger_rear_left': {'present': False, 'pose': None}, 'passenger_rear_right': {'present': False, 'pose': None} } def update(self, csi_data: np.ndarray) -> dict: """ 更新乘员状态 Args: csi_data: CSI数据 Returns: state: 乘员状态 """ result = self.processor.process_csi(csi_data) if result['keypoints'] is not None and result['confidence'] > 0.7: self.occupant_state['driver']['present'] = True self.occupant_state['driver']['pose'] = result['keypoints'] return self.occupant_state def detect_oop(self) -> list: """ 检测异常姿态(OOP) Returns: oop_alerts: OOP警报列表 """ alerts = [] for seat, state in self.occupant_state.items(): if not state['present'] or state['pose'] is None: continue keypoints = state['pose'] shoulder_y = (keypoints[5, 1] + keypoints[6, 1]) / 2 elbow_y = (keypoints[7, 1] + keypoints[8, 1]) / 2 if elbow_y < shoulder_y - 0.2: alerts.append({ 'seat': seat, 'type': 'arms_raised', 'severity': 'medium', 'message': f'{seat}手臂异常抬高' }) hip_center = (keypoints[11, 0] + keypoints[12, 0]) / 2 shoulder_center = (keypoints[5, 0] + keypoints[6, 0]) / 2 if abs(hip_center - shoulder_center) > 0.15: alerts.append({ 'seat': seat, 'type': 'body_twisted', 'severity': 'high', 'message': f'{seat}身体异常扭曲' }) return alerts
if __name__ == "__main__": wifi_config = { 'sampling_rate': 1000, 'antennas': 3, 'subcarriers': 30 } oms = OMSWiFiSystem(wifi_config) csi_data = np.random.randn(100, 30, 3) + 1.0 state = oms.update(csi_data) alerts = oms.detect_oop() print("乘员状态:") for seat, info in state.items(): if info['present']: print(f" {seat}: 在位") if alerts: print("\nOOP警报:") for alert in alerts: print(f" [{alert['severity']}] {alert['message']}")
|