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
| import numpy as np from typing import Dict, Tuple, List from dataclasses import dataclass from enum import Enum
class ModalityType(Enum): RADAR = 'radar' THERMAL = 'thermal' RGB = 'rgb'
@dataclass class ModalityResult: """单模态检测结果""" child_detected: bool confidence: float location: Tuple[float, float] vital_signs: Dict = None
class MultiModalCPDFusion: """多模态CPD融合检测器""" def __init__(self): self.weights = { ModalityType.RADAR: 0.4, ModalityType.THERMAL: 0.35, ModalityType.RGB: 0.25 } self.detection_threshold = 0.65 def detect(self, radar_result: ModalityResult, thermal_result: ModalityResult, rgb_result: ModalityResult) -> Dict: """ 多模态融合检测 Args: radar_result: 雷达检测结果 thermal_result: 热成像检测结果 rgb_result: RGB摄像头检测结果 Returns: final_result: 融合检测结果 """ results = { ModalityType.RADAR: radar_result, ModalityType.THERMAL: thermal_result, ModalityType.RGB: rgb_result } weighted_confidence = 0.0 for modality, result in results.items(): weight = self.weights[modality] if result.child_detected: weighted_confidence += weight * result.confidence else: weighted_confidence -= weight * 0.3 weighted_confidence = np.clip(weighted_confidence, 0, 1) locations = [] for result in results.values(): if result.child_detected: locations.append(result.location) if locations: final_location = np.mean(locations, axis=0) else: final_location = (0, 0) child_detected = weighted_confidence > self.detection_threshold vital_signs_valid = self._validate_vital_signs(radar_result) return { 'child_detected': child_detected and vital_signs_valid, 'confidence': weighted_confidence, 'location': final_location, 'vital_signs': radar_result.vital_signs if radar_result.vital_signs else {}, 'modality_contributions': { 'radar': radar_result.confidence * self.weights[ModalityType.RADAR], 'thermal': thermal_result.confidence * self.weights[ModalityType.THERMAL], 'rgb': rgb_result.confidence * self.weights[ModalityType.RGB] } } def _validate_vital_signs(self, radar_result: ModalityResult) -> bool: """验证生命体征""" if not radar_result.vital_signs: return True heart_rate = radar_result.vital_signs.get('heart_rate', 0) respiration_rate = radar_result.vital_signs.get('respiration_rate', 0) is_valid_heart = 60 < heart_rate < 200 is_valid_resp = 10 < respiration_rate < 100 return is_valid_heart and is_valid_resp
class RadarCPDDetector: """60GHz雷达CPD检测器""" def __init__(self): self.config = { 'frequency': 60e9, 'bandwidth': 4e9, 'range_resolution': 0.05, 'max_range': 3.0, 'update_rate': 10 } def detect(self, radar_data: np.ndarray) -> ModalityResult: """ 雷达检测 Args: radar_data: 雷达数据(距离-多普勒图) Returns: result: 检测结果 """ vital_signs = self._extract_vital_signs(radar_data) micro_movement = self._detect_micro_movement(radar_data) child_detected = micro_movement and vital_signs['valid'] location = self._estimate_location(radar_data) return ModalityResult( child_detected=child_detected, confidence=0.85 if child_detected else 0.2, location=location, vital_signs=vital_signs ) def _extract_vital_signs(self, radar_data: np.ndarray) -> Dict: """提取生命体征""" return { 'heart_rate': 120, 'respiration_rate': 35, 'valid': True } def _detect_micro_movement(self, radar_data: np.ndarray) -> bool: """检测微动""" return True def _estimate_location(self, radar_data: np.ndarray) -> Tuple[float, float]: """估计位置""" return (1.5, 0.3)
class ThermalCPDDetector: """热成像CPD检测器""" def __init__(self): self.config = { 'resolution': (160, 120), 'temperature_range': (20, 40), 'threshold': 32 } def detect(self, thermal_image: np.ndarray) -> ModalityResult: """ 热成像检测 Args: thermal_image: (H, W) 温度图像 Returns: result: 检测结果 """ human_mask = thermal_image > self.config['threshold'] num_objects, labels, stats, centroids = self._analyze_connected_components(human_mask) child_detected = num_objects > 0 if child_detected: location = centroids[0] else: location = (0, 0) return ModalityResult( child_detected=child_detected, confidence=0.75 if child_detected else 0.1, location=location ) def _analyze_connected_components(self, mask: np.ndarray): """连通域分析""" from scipy import ndimage labeled, num_objects = ndimage.label(mask) centroids = ndimage.center_of_mass(mask, labeled, range(1, num_objects + 1)) return num_objects, labeled, None, centroids
class RGBCPDDetector: """RGB摄像头CPD检测器""" def __init__(self): self.model = None def detect(self, rgb_image: np.ndarray) -> ModalityResult: """ RGB检测 Args: rgb_image: (H, W, 3) RGB图像 Returns: result: 检测结果 """ detections = self._detect_objects(rgb_image) child_detected = len(detections) > 0 if child_detected: location = detections[0]['center'] else: location = (0, 0) return ModalityResult( child_detected=child_detected, confidence=0.9 if child_detected else 0.1, location=location ) def _detect_objects(self, image: np.ndarray) -> List[Dict]: """检测物体""" return [{'class': 'child_seat', 'center': (200, 150), 'confidence': 0.92}]
if __name__ == "__main__": radar_detector = RadarCPDDetector() thermal_detector = ThermalCPDDetector() rgb_detector = RGBCPDDetector() fusion = MultiModalCPDFusion() radar_data = np.random.rand(256, 256) thermal_image = np.random.rand(120, 160) * 20 + 20 rgb_image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) radar_result = radar_detector.detect(radar_data) thermal_result = thermal_detector.detect(thermal_image) rgb_result = rgb_detector.detect(rgb_image) final_result = fusion.detect(radar_result, thermal_result, rgb_result) print(f"儿童存在检测: {final_result['child_detected']}") print(f"融合置信度: {final_result['confidence']:.2f}") print(f"位置: {final_result['location']}") print(f"各模态贡献: {final_result['modality_contributions']}")
|