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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
| """ mmWave雷达CPD检测实现 检测原理:分析呼吸运动产生的微多普勒信号
硬件要求: - 60GHz mmWave雷达(如TI IWR6843AOP) - 4发4收天线阵列 - 能检测0.2-0.7Hz呼吸频率 """
import numpy as np from typing import List, Tuple, Optional from dataclasses import dataclass from enum import Enum
class OccupantType(Enum): """乘员类型""" EMPTY = "empty" ADULT = "adult" CHILD = "child" INFANT = "infant" OBJECT = "object"
@dataclass class RadarFrame: """雷达帧数据""" timestamp: float range_profile: np.ndarray doppler_profile: np.ndarray point_cloud: List[Tuple[float, float, float, float, float]]
@dataclass class VitalSigns: """生命体征""" breathing_rate: float breathing_confidence: float movement_detected: bool
class CPDRadarDetector: """ Euro NCAP CPD雷达检测器 符合2026协议要求: - 检测呼吸/运动 - 识别儿童(≤6岁) - 15秒内触发警告 """ CHILD_BREATHING_RANGE = (20, 40) ADULT_BREATHING_RANGE = (12, 20) def __init__( self, radar_frame_rate: int = 10, fft_size: int = 512, breathing_threshold: float = 0.3, movement_threshold: float = 0.5 ): """ 初始化CPD检测器 Args: radar_frame_rate: 雷达帧率 fft_size: FFT点数 breathing_threshold: 呼吸检测阈值 movement_threshold: 运动检测阈值 """ self.frame_rate = radar_frame_rate self.fft_size = fft_size self.breathing_threshold = breathing_threshold self.movement_threshold = movement_threshold self.doppler_buffer = [] self.range_buffer = [] self.buffer_duration = 30 self.last_detection_time = None self.warning_state = "idle" def process_frame(self, frame: RadarFrame) -> Tuple[Optional[VitalSigns], OccupantType]: """ 处理单帧雷达数据 Args: frame: 雷达帧 Returns: vital_signs: 生命体征 occupant_type: 乘员类型 """ self.doppler_buffer.append(frame.doppler_profile) self.range_buffer.append(frame.range_profile) max_frames = self.buffer_duration * self.frame_rate if len(self.doppler_buffer) > max_frames: self.doppler_buffer = self.doppler_buffer[-max_frames:] self.range_buffer = self.range_buffer[-max_frames:] if len(self.doppler_buffer) < 10 * self.frame_rate: return None, OccupantType.EMPTY vital_signs = self._detect_breathing() movement = self._detect_movement() if vital_signs: vital_signs.movement_detected = movement occupant_type = self._classify_occupant(vital_signs) return vital_signs, occupant_type def _detect_breathing(self) -> Optional[VitalSigns]: """ 检测呼吸信号 使用FFT分析多普勒信号中的呼吸频率 """ doppler_series = np.array(self.doppler_buffer) mean_range = np.mean(self.range_buffer, axis=0) target_range_bin = np.argmax(mean_range) if doppler_series.ndim == 2: doppler_time_series = doppler_series[:, target_range_bin] else: doppler_time_series = doppler_series doppler_time_series = doppler_time_series - np.mean(doppler_time_series) fft_result = np.fft.rfft(doppler_time_series, n=self.fft_size) fft_magnitude = np.abs(fft_result) freq_resolution = self.frame_rate / self.fft_size frequencies = np.arange(len(fft_magnitude)) * freq_resolution breathing_mask = (frequencies >= 0.2) & (frequencies <= 0.7) breathing_spectrum = fft_magnitude * breathing_mask if np.max(breathing_spectrum) > 0: peak_idx = np.argmax(breathing_spectrum) breathing_freq = frequencies[peak_idx] breathing_rate = breathing_freq * 60 total_power = np.sum(fft_magnitude) breathing_power = np.sum(breathing_spectrum) confidence = breathing_power / total_power if total_power > 0 else 0 if confidence > self.breathing_threshold: return VitalSigns( breathing_rate=breathing_rate, breathing_confidence=confidence, movement_detected=False ) return None def _detect_movement(self) -> bool: """ 检测大动作 通过多普勒信号的变化检测 """ if len(self.doppler_buffer) < 10: return False recent_doppler = self.doppler_buffer[-10:] variance = np.var(recent_doppler) return variance > self.movement_threshold def _classify_occupant(self, vital_signs: Optional[VitalSigns]) -> OccupantType: """ 分类乘员类型 基于呼吸频率区分儿童/成人 """ if vital_signs is None: return OccupantType.EMPTY breathing_rate = vital_signs.breathing_rate if self.CHILD_BREATHING_RANGE[0] <= breathing_rate <= self.CHILD_BREATHING_RANGE[1]: return OccupantType.CHILD if self.ADULT_BREATHING_RANGE[0] <= breathing_rate <= self.ADULT_BREATHING_RANGE[1]: return OccupantType.ADULT if breathing_rate > self.CHILD_BREATHING_RANGE[1]: return OccupantType.INFANT return OccupantType.OBJECT
class CPDWarningController: """ CPD警告控制器 符合Euro NCAP 2026时序要求 """ def __init__(self): self.state = "idle" self.detection_time = None self.warning_count = 0 self.last_warning_time = None def update( self, occupant_type: OccupantType, vehicle_locked: bool, doors_closed: bool, current_time: float ) -> dict: """ 更新警告状态 Args: occupant_type: 乘员类型 vehicle_locked: 车辆是否锁闭 doors_closed: 车门是否关闭 current_time: 当前时间(秒) Returns: action: 需要执行的动作 """ if occupant_type not in [OccupantType.CHILD, OccupantType.INFANT]: self.state = "idle" self.detection_time = None return {"action": "none"} if self.detection_time is None: self.detection_time = current_time elapsed = current_time - self.detection_time if vehicle_locked: if self.state == "idle" and elapsed >= 15: self.state = "initial_warning" self.last_warning_time = current_time return { "action": "initial_warning", "duration": 3, "type": "visual_audible" } else: if self.state == "idle" and elapsed >= 600: self.state = "initial_warning" self.last_warning_time = current_time return { "action": "initial_warning", "duration": 3, "type": "visual_audible" } if self.state == "initial_warning": if self.last_warning_time and (current_time - self.last_warning_time) >= 90: self.state = "escalation" self.warning_count += 1 self.last_warning_time = current_time return { "action": "escalation", "duration": 15, "type": "intensive", "message": "Check the seats", "count": self.warning_count } if self.state == "escalation": if self.last_warning_time and (current_time - self.last_warning_time) >= 60: self.warning_count += 1 self.last_warning_time = current_time if elapsed <= 1200: return { "action": "escalation", "duration": 15, "type": "intensive", "message": "Check the seats", "count": self.warning_count } if elapsed >= 600: return { "action": "intervention", "measures": ["climate_control", "unlock_doors", "remote_notification"] } return {"action": "none"}
if __name__ == "__main__": detector = CPDRadarDetector(radar_frame_rate=10) controller = CPDWarningController() print("=== Euro NCAP CPD测试 ===") print(f"儿童呼吸频率范围: {detector.CHILD_BREATHING_RANGE}") print() breathing_rate = 30 breathing_freq = breathing_rate / 60 frame_rate = 10 duration = 30 for i in range(duration * frame_rate): t = i / frame_rate breathing_motion = 0.02 * np.sin(2 * np.pi * breathing_freq * t) noise = np.random.normal(0, 0.005, 10) frame = RadarFrame( timestamp=t * 1000, range_profile=np.random.randn(10), doppler_profile=breathing_motion + noise, point_cloud=[] ) vital_signs, occupant_type = detector.process_frame(frame) if t >= 5 and i % 20 == 0: action = controller.update( occupant_type=occupant_type, vehicle_locked=True, doors_closed=True, current_time=t ) if action["action"] != "none": print(f"[{t:.1f}s] 检测结果: {occupant_type.value}") if vital_signs: print(f" 呼吸频率: {vital_signs.breathing_rate:.1f} 次/分") print(f" 置信度: {vital_signs.breathing_confidence:.2f}") print(f" 警告动作: {action}") print() print("=== 测试完成 ===")
|