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
| """ Deep-In-Sight风格的摄像头-雷达融合CPD框架
硬件: - OMS摄像头 (RGB-IR, 5MP, 全局快门) - Murata 60GHz mmWave radar (4Tx/4Rx) - 边缘处理器 (QCS8255 Hexagon NPU)
依赖: numpy>=1.24, opencv-python>=4.8 """
import numpy as np import cv2 from typing import Dict, Tuple, Optional
class CameraRadarFusionCPD: """ 摄像头-雷达融合儿童存在检测 融合策略: 1. 摄像头检测可见乘员(人脸/人体检测+分类) 2. 雷达检测微动(呼吸/心跳) 3. 决策融合: OR逻辑 + 置信度加权 """ def __init__(self, config: dict): self.camera_confidence_threshold = config.get('cam_thresh', 0.7) self.radar_confidence_threshold = config.get('radar_thresh', 0.6) self.radar_breathing_threshold = config.get('breath_thresh', 0.3) def camera_detect(self, image: np.ndarray) -> Dict: """ 摄像头检测模块 Returns: { 'occupant_detected': bool, 'classification': 'adult'|'child'|'empty'|'unknown', 'confidence': float, 'bbox': (x, y, w, h) } """ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image _, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if not contours: return { 'occupant_detected': False, 'classification': 'empty', 'confidence': 0.9, 'bbox': None } c = max(contours, key=cv2.contourArea) if cv2.contourArea(c) < 500: return { 'occupant_detected': False, 'classification': 'empty', 'confidence': 0.85, 'bbox': None } x, y, w, h = cv2.boundingRect(c) area_ratio = (w * h) / (image.shape[0] * image.shape[1]) if area_ratio > 0.15: cls = 'adult' elif area_ratio > 0.05: cls = 'child' else: cls = 'unknown' return { 'occupant_detected': True, 'classification': cls, 'confidence': min(0.95, 0.5 + area_ratio * 3), 'bbox': (x, y, w, h) } def radar_detect(self, radar_cube: np.ndarray) -> Dict: """ 60GHz雷达检测模块 Args: radar_cube: range-Doppler数据 (N_range x N_doppler) Returns: { 'presence_detected': bool, 'breathing_detected': bool, 'breathing_rate': float, # breaths/min 'confidence': float } """ low_doppler = radar_cube[:, :5] energy = np.sum(np.abs(low_doppler), axis=1) total_energy = np.sum(energy) presence = total_energy > self.radar_breathing_threshold * 1e6 if presence: slow_time_signal = np.sum(radar_cube, axis=0) fft = np.fft.rfft(slow_time_signal) freqs = np.fft.rfftfreq(len(slow_time_signal), d=0.05) breath_mask = (freqs >= 0.2) & (freqs <= 0.5) if np.any(breath_mask): peak_idx = np.argmax(np.abs(fft[breath_mask])) breath_freq = freqs[breath_mask][peak_idx] breathing_rate = breath_freq * 60 breathing = True else: breathing_rate = 0 breathing = False else: breathing = False breathing_rate = 0 confidence = min(1.0, total_energy / 1e7) return { 'presence_detected': presence, 'breathing_detected': breathing, 'breathing_rate': breathing_rate, 'confidence': confidence } def fuse( self, camera_result: Dict, radar_result: Dict ) -> Dict: """ 决策融合 策略: - 摄像头可见 → 用摄像头分类 - 摄像头不可见,雷达有微动 → 判定有乘员(可能被遮挡) - 两者都不可见 → 判定空座 - 两者都可见 → 加权融合 """ cam_det = camera_result['occupant_detected'] rad_det = radar_result['presence_detected'] if cam_det and rad_det: return { 'occupant_present': True, 'classification': camera_result['classification'], 'source': 'camera+radar', 'confidence': 0.95, 'breathing': radar_result['breathing_detected'], 'breathing_rate': radar_result.get('breathing_rate', 0) } elif not cam_det and rad_det: return { 'occupant_present': True, 'classification': 'occluded_occupant', 'source': 'radar_only', 'confidence': radar_result['confidence'], 'breathing': radar_result['breathing_detected'], 'breathing_rate': radar_result.get('breathing_rate', 0) } elif cam_det and not rad_det: return { 'occupant_present': True, 'classification': camera_result['classification'], 'source': 'camera_only', 'confidence': camera_result['confidence'] * 0.8, 'breathing': False, 'breathing_rate': 0 } else: return { 'occupant_present': False, 'classification': 'empty', 'source': 'both_negative', 'confidence': 0.9, 'breathing': False, 'breathing_rate': 0 }
if __name__ == "__main__": fusion = CameraRadarFusionCPD({ 'cam_thresh': 0.7, 'radar_thresh': 0.6, 'breath_thresh': 0.3 }) img1 = np.random.randint(40, 60, (1080, 1920, 3), dtype=np.uint8) img1[400:700, 800:1000] = 120 radar1 = np.random.randn(64, 32) * 0.01 + 0.5 cam1 = fusion.camera_detect(img1) rad1 = fusion.radar_detect(radar1) result1 = fusion.fuse(cam1, rad1) print(f"场景1 (可见乘员): {result1['classification']}, 置信度={result1['confidence']:.2f}") img2 = np.random.randint(40, 45, (1080, 1920, 3), dtype=np.uint8) radar2 = np.random.randn(64, 32) * 0.01 + 2.0 cam2 = fusion.camera_detect(img2) rad2 = fusion.radar_detect(radar2) result2 = fusion.fuse(cam2, rad2) print(f"场景2 (遮挡乘员): {result2['classification']}, 来源={result2['source']}") print(f" 呼吸检测: {result2['breathing']}, 呼吸率: {result2.get('breathing_rate', 0):.1f} bpm")
|