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
| """ CPD 雷达-摄像头融合检测系统 Euro NCAP 2026 合规实现 """
import numpy as np from typing import Dict, Tuple, List from dataclasses import dataclass
@dataclass class CPDAlert: """CPD 警告""" level: int trigger_time: float location: Tuple[int, int] child_age_estimate: str vital_signs: Dict confidence: float
class RadarCPDDetector: """ 60GHz 雷达 CPD 检测器 使用 TI IWR6843AOP 或类似芯片 """ def __init__(self, config: Dict): self.frequency = 60e9 self.range_resolution = config.get('range_resolution', 0.04) self.velocity_resolution = config.get('velocity_resolution', 0.1) self.breathing_threshold = config.get('breathing_threshold', 0.01) self.heart_rate_range = config.get('heart_rate_range', (60, 180)) self.seat_zones = { 'rear_left': {'x': (-0.5, 0.0), 'y': (0.5, 1.0), 'z': (0.0, 0.5)}, 'rear_center': {'x': (-0.25, 0.25), 'y': (0.5, 1.0), 'z': (0.0, 0.5)}, 'rear_right': {'x': (0.0, 0.5), 'y': (0.5, 1.0), 'z': (0.0, 0.5)} } def process_frame(self, radar_data: np.ndarray) -> Dict: """ 处理单帧雷达数据 Args: radar_data: 雷达点云 (N, 4) [x, y, z, velocity] Returns: detection: 检测结果 """ clusters = self._cluster_points(radar_data) vital_signs = self._detect_vital_signs(clusters) occupancy = self._check_occupancy(clusters) return { 'clusters': clusters, 'vital_signs': vital_signs, 'occupancy': occupancy } def _cluster_points(self, points: np.ndarray) -> List[Dict]: """点云聚类""" if len(points) == 0: return [] from sklearn.cluster import DBSCAN clustering = DBSCAN(eps=0.2, min_samples=5).fit(points[:, :3]) labels = clustering.labels_ clusters = [] for label in set(labels): if label == -1: continue cluster_points = points[labels == label] center = cluster_points[:, :3].mean(axis=0) clusters.append({ 'center': center, 'points': cluster_points, 'size': len(cluster_points) }) return clusters def _detect_vital_signs(self, clusters: List[Dict]) -> List[Dict]: """检测生命体征""" vital_signs = [] for cluster in clusters: velocities = cluster['points'][:, 3] velocity_variance = np.var(velocities) if velocity_variance > self.breathing_threshold: vital_signs.append({ 'location': cluster['center'], 'confidence': min(velocity_variance / self.breathing_threshold, 1.0) }) return vital_signs def _check_occupancy(self, clusters: List[Dict]) -> Dict: """检查座椅占用""" occupancy = {seat: False for seat in self.seat_zones} for cluster in clusters: center = cluster['center'] for seat, zone in self.seat_zones.items(): if (zone['x'][0] <= center[0] <= zone['x'][1] and zone['y'][0] <= center[1] <= zone['y'][1] and zone['z'][0] <= center[2] <= zone['z'][1]): occupancy[seat] = True return occupancy
class CameraCPDDetector: """摄像头 CPD 检测器""" def __init__(self, config: Dict): self.child_detector = ChildDetector(config) self.pose_estimator = PoseEstimator(config) def process_frame(self, frame: np.ndarray) -> Dict: """ 处理单帧图像 Args: frame: 车内图像 (H, W, 3) Returns: detection: 检测结果 """ children = self.child_detector.detect(frame) poses = [] for child in children: pose = self.pose_estimator.estimate(frame, child['bbox']) poses.append(pose) return { 'children': children, 'poses': poses }
class ChildDetector: """儿童检测器""" def __init__(self, config: Dict): pass def detect(self, frame: np.ndarray) -> List[Dict]: """检测儿童""" return []
class PoseEstimator: """姿态估计器""" def __init__(self, config: Dict): pass def estimate(self, frame: np.ndarray, bbox: Tuple) -> Dict: """估计姿态""" return {}
class CPDFusionSystem: """ CPD 融合系统 雷达 + 摄像头融合 """ def __init__(self, config: Dict): self.radar_detector = RadarCPDDetector(config) self.camera_detector = CameraCPDDetector(config) self.detection_history = [] self.alert_state = None self.engine_off_time = None def update(self, radar_data: np.ndarray, camera_frame: np.ndarray, vehicle_state: Dict, current_time: float) -> CPDAlert: """ 更新系统状态 Args: radar_data: 雷达点云 camera_frame: 车内图像 vehicle_state: 车辆状态 - 'engine_on': bool - 'doors_closed': bool - 'speed': float current_time: 当前时间 Returns: alert: CPD 警告(如果触发) """ if vehicle_state['engine_on']: self.engine_off_time = None self.alert_state = None return None if self.engine_off_time is None: self.engine_off_time = current_time radar_result = self.radar_detector.process_frame(radar_data) camera_result = self.camera_detector.process_frame(camera_frame) child_detected = self._fuse_detections(radar_result, camera_result) if child_detected: elapsed_time = current_time - self.engine_off_time if elapsed_time > 120: level = 3 elif elapsed_time > 90: level = 2 elif elapsed_time > 60: level = 1 else: return None return CPDAlert( level=level, trigger_time=current_time, location=child_detected['location'], child_age_estimate=child_detected.get('age_estimate', 'unknown'), vital_signs=radar_result['vital_signs'], confidence=child_detected['confidence'] ) return None def _fuse_detections(self, radar_result: Dict, camera_result: Dict) -> Dict: """ 融合雷达和摄像头检测结果 Returns: fused: 融合结果(如果检测到儿童) """ radar_vital = len(radar_result['vital_signs']) > 0 camera_child = len(camera_result['children']) > 0 if radar_vital and camera_child: return { 'location': camera_result['children'][0].get('location', (0, 0)), 'confidence': 0.95, 'age_estimate': camera_result['children'][0].get('age', 'unknown') } elif radar_vital: return { 'location': radar_result['vital_signs'][0]['location'], 'confidence': 0.7, 'age_estimate': 'unknown' } elif camera_child: return { 'location': camera_result['children'][0].get('location', (0, 0)), 'confidence': 0.6, 'age_estimate': camera_result['children'][0].get('age', 'unknown') } return None
class EuroNCAP_CPDTest: """Euro NCAP CPD 测试""" def __init__(self): self.system = CPDFusionSystem({}) self.test_scenarios = [ { 'id': 'CPD-01', 'description': '6个月婴儿独自留在后座', 'expected_detection': True, 'expected_alert_level': 1, 'time_to_alert': 60 }, { 'id': 'CPD-02', 'description': '3岁儿童独自留在后座', 'expected_detection': True, 'expected_alert_level': 1, 'time_to_alert': 60 }, { 'id': 'CPD-03', 'description': '宠物留在车内', 'expected_detection': True, 'expected_alert_level': 1, 'time_to_alert': 120 }, { 'id': 'CPD-04', 'description': '空车', 'expected_detection': False, 'expected_alert_level': 0, 'time_to_alert': None } ] def run_test(self, scenario_id: str) -> Dict: """运行测试场景""" scenario = next(s for s in self.test_scenarios if s['id'] == scenario_id) radar_data = np.random.randn(100, 4) * 0.5 camera_frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) vehicle_state = {'engine_on': False, 'doors_closed': True, 'speed': 0} alert = self.system.update(radar_data, camera_frame, vehicle_state, time.time()) return { 'scenario_id': scenario_id, 'expected_detection': scenario['expected_detection'], 'detected': alert is not None, 'passed': (alert is not None) == scenario['expected_detection'] }
|