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
| from dataclasses import dataclass from typing import List, Tuple, Optional import numpy as np
@dataclass class RadarPoint: """雷达点云数据""" x: float y: float z: float velocity: float snr: float timestamp: float
@dataclass class ToFDepthFrame: """ToF 深度帧""" depth_map: np.ndarray amplitude: np.ndarray timestamp: float
@dataclass class RGBFrame: """RGB/IR 图像帧""" image: np.ndarray is_infrared: bool timestamp: float
@dataclass class FusedOccupantData: """融合后的乘员数据""" position_3d: Tuple[float, float, float] velocity: Optional[float] depth_map_roi: np.ndarray visual_features: Optional[np.ndarray] detection_confidence: float timestamp: float
class SensorFusionPipeline: """ 传感器融合管线 整合雷达、ToF、视觉数据 """ def __init__(self, config: dict): self.config = config self.radar_to_camera_extrinsics = config.get('radar_to_camera_extrinsics', np.eye(4)) self.tof_to_camera_extrinsics = config.get('tof_to_camera_extrinsics', np.eye(4)) self.radar_buffer = [] self.tof_buffer = [] self.rgb_buffer = [] self.sync_threshold_ms = 33 def process_frame( self, radar_data: List[RadarPoint], tof_frame: ToFDepthFrame, rgb_frame: RGBFrame ) -> List[FusedOccupantData]: """ 处理一帧数据 Args: radar_data: 雷达点云 tof_frame: ToF 深度帧 rgb_frame: RGB 图像 Returns: occupants: 检测到的乘员列表 """ if not self._check_temporal_sync(radar_data, tof_frame, rgb_frame): return [] radar_clusters = self._cluster_radar_points(radar_data) tof_occupants = self._detect_from_tof(tof_frame) visual_detections = self._detect_from_rgb(rgb_frame) fused_occupants = self._fuse_detections( radar_clusters, tof_occupants, visual_detections ) return fused_occupants def _check_temporal_sync( self, radar_data: List[RadarPoint], tof_frame: ToFDepthFrame, rgb_frame: RGBFrame ) -> bool: """检查时间同步""" if not radar_data or tof_frame is None or rgb_frame is None: return False radar_time = radar_data[0].timestamp tof_time = tof_frame.timestamp rgb_time = rgb_frame.timestamp max_diff = max(abs(radar_time - tof_time), abs(radar_time - rgb_time), abs(tof_time - rgb_time)) return max_diff < self.sync_threshold_ms / 1000.0 def _cluster_radar_points( self, points: List[RadarPoint] ) -> List[dict]: """雷达点云聚类""" if not points: return [] from sklearn.cluster import DBSCAN positions = np.array([[p.x, p.y, p.z] for p in points]) clustering = DBSCAN(eps=0.3, min_samples=5).fit(positions) labels = clustering.labels_ clusters = [] for label in set(labels): if label == -1: continue cluster_points = [p for i, p in enumerate(points) if labels[i] == label] center = np.mean([[p.x, p.y, p.z] for p in cluster_points], axis=0) avg_velocity = np.mean([p.velocity for p in cluster_points]) is_breathing = self._detect_breathing(cluster_points) clusters.append({ 'center': center, 'point_count': len(cluster_points), 'avg_velocity': avg_velocity, 'is_breathing': is_breathing, 'points': cluster_points }) return clusters def _detect_breathing(self, points: List[RadarPoint]) -> bool: """检测呼吸运动""" velocities = [abs(p.velocity) for p in points] avg_velocity = np.mean(velocities) return 0.005 < avg_velocity < 0.1 def _detect_from_tof(self, frame: ToFDepthFrame) -> List[dict]: """ToF 深度检测""" depth_map = frame.depth_map valid_mask = (depth_map > 0.3) & (depth_map < 3.0) from scipy.ndimage import label labeled, num_features = label(valid_mask) detections = [] for i in range(1, num_features + 1): mask = labeled == i ys, xs = np.where(mask) depths = depth_map[mask] center_x = np.mean(xs) center_y = np.mean(ys) center_z = np.mean(depths) bbox = [min(xs), min(ys), max(xs), max(ys)] detections.append({ 'position': (center_x, center_y, center_z), 'bbox': bbox, 'pixel_count': len(xs), 'depth_map_roi': depth_map[bbox[1]:bbox[3], bbox[0]:bbox[2]] }) return detections def _detect_from_rgb(self, frame: RGBFrame) -> List[dict]: """视觉检测""" return [] def _fuse_detections( self, radar_clusters: List[dict], tof_detections: List[dict], visual_detections: List[dict] ) -> List[FusedOccupantData]: """融合检测结果""" fused = [] for tof_det in tof_detections: tof_pos = tof_det['position'] matched_radar = None min_dist = float('inf') for radar_cluster in radar_clusters: radar_pos = radar_cluster['center'] dist = np.linalg.norm(np.array(tof_pos) - np.array(radar_pos)) if dist < min_dist and dist < 0.5: min_dist = dist matched_radar = radar_cluster occupant = FusedOccupantData( position_3d=tof_pos, velocity=matched_radar['avg_velocity'] if matched_radar else None, depth_map_roi=tof_det['depth_map_roi'], visual_features=None, detection_confidence=0.9 if matched_radar else 0.7, timestamp=0.0 ) fused.append(occupant) return fused
|