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
| """ Seeing Machines 3D Cabin Perception Mapping 架构
核心能力: 1. 多摄像头标定与融合 2. 实时 3D 重建 3. 全座舱乘员感知 """
import numpy as np import cv2 from typing import List, Dict, Tuple
class CabinPerceptionMapping: """座舱感知映射系统""" def __init__(self, camera_configs: List[dict]): """ 初始化多摄像头系统 Args: camera_configs: 摄像头配置列表 [{ "id": "driver_cam", "position": [0.3, 0.1, 1.2], # 米 "rotation": [0, -15, 0], # 度 "fov": 60, # 度 "resolution": [1920, 1080] }, ...] """ self.cameras = [] for config in camera_configs: cam = Camera3D( camera_id=config["id"], position=np.array(config["position"]), rotation=np.array(config["rotation"]), fov=config["fov"], resolution=config["resolution"] ) self.cameras.append(cam) self.cabin_model = self._load_cabin_model() self.occupant_detector = OccupantDetector3D() def process_frame(self, frames: Dict[str, np.ndarray]) -> dict: """ 处理多摄像头帧 Args: frames: {camera_id: frame} Returns: 座舱感知结果 """ detections_2d = {} for cam_id, frame in frames.items(): detections_2d[cam_id] = self._detect_2d(cam_id, frame) detections_3d = self._reconstruct_3d(detections_2d) cabin_map = self._map_to_cabin(detections_3d) vital_signs = self._extract_vital_signs(frames, detections_3d) return { "cabin_map": cabin_map, "occupants": detections_3d, "vital_signs": vital_signs } def _detect_2d(self, cam_id: str, frame: np.ndarray) -> dict: """单摄像头 2D 检测""" face_landmarks = self._detect_face_landmarks(frame) body_pose = self._detect_body_pose(frame) gaze = self._estimate_gaze(frame, face_landmarks) return { "face": face_landmarks, "body": body_pose, "gaze": gaze } def _reconstruct_3d(self, detections_2d: Dict) -> List[dict]: """ 3D 重建(多视角三角化) 使用多摄像头几何约束重建 3D 坐标 """ occupants_3d = [] for occupant_id in self._match_cross_camera(detections_2d): points_2d = [] cameras = [] for cam_id, det in detections_2d.items(): if occupant_id in det: points_2d.append(det[occupant_id]["face"]["nose_tip"]) cameras.append(self._get_camera(cam_id)) if len(points_2d) >= 2: point_3d = self._triangulate(points_2d, cameras) pose_3d = self._reconstruct_pose_3d( [det[occupant_id] for det in detections_2d.values() if occupant_id in det] ) occupants_3d.append({ "id": occupant_id, "position": point_3d, "pose": pose_3d, "gaze_3d": self._reconstruct_gaze_3d(occupant_id, detections_2d) }) return occupants_3d def _triangulate(self, points_2d: List, cameras: List) -> np.ndarray: """三角化求解 3D 点""" P_matrices = [] for cam, point in zip(cameras, points_2d): P = cam.get_projection_matrix() P_matrices.append(P) A = [] for P, point in zip(P_matrices, points_2d): x, y = point A.append(P[2] * x - P[0]) A.append(P[2] * y - P[1]) A = np.array(A) _, _, Vt = np.linalg.svd(A) X = Vt[-1] X = X[:3] / X[3] return X def _map_to_cabin(self, detections_3d: List) -> dict: """映射到座舱模型""" cabin_map = { "driver": None, "passengers": [], "rear_passengers": [] } for occupant in detections_3d: pos = occupant["position"] if pos[0] < 0.5: cabin_map["driver"] = occupant elif pos[0] < 1.5: cabin_map["passengers"].append(occupant) else: cabin_map["rear_passengers"].append(occupant) return cabin_map def _extract_vital_signs(self, frames: Dict, detections_3d: List) -> Dict: """提取生命体征""" vital_signs = {} for occupant in detections_3d: breathing_rate = self._detect_breathing(frames, occupant["id"]) heart_rate = self._detect_heart_rate(frames, occupant["id"]) vital_signs[occupant["id"]] = { "breathing_rate": breathing_rate, "heart_rate": heart_rate } return vital_signs def _detect_breathing(self, frames: Dict, occupant_id: str) -> float: """呼吸检测""" return 16.0 def _detect_heart_rate(self, frames: Dict, occupant_id: str) -> float: """心率检测(rPPG)""" return 72.0
class Camera3D: """3D 摄像头模型""" def __init__(self, camera_id, position, rotation, fov, resolution): self.id = camera_id self.position = position self.rotation = rotation self.fov = fov self.resolution = resolution self.K = self._compute_intrinsics() self.R, self.t = self._compute_extrinsics() def _compute_intrinsics(self): """计算内参矩阵""" fx = self.resolution[0] / (2 * np.tan(np.radians(self.fov / 2))) fy = fx cx = self.resolution[0] / 2 cy = self.resolution[1] / 2 return np.array([ [fx, 0, cx], [0, fy, cy], [0, 0, 1] ]) def _compute_extrinsics(self): """计算外参矩阵""" rx, ry, rz = np.radians(self.rotation) Rx = np.array([ [1, 0, 0], [0, np.cos(rx), -np.sin(rx)], [0, np.sin(rx), np.cos(rx)] ]) Ry = np.array([ [np.cos(ry), 0, np.sin(ry)], [0, 1, 0], [-np.sin(ry), 0, np.cos(ry)] ]) Rz = np.array([ [np.cos(rz), -np.sin(rz), 0], [np.sin(rz), np.cos(rz), 0], [0, 0, 1] ]) R = Rz @ Ry @ Rx t = self.position return R, t def get_projection_matrix(self): """获取投影矩阵""" extrinsic = np.hstack([self.R, self.t.reshape(-1, 1)]) return self.K @ extrinsic
class OccupantDetector3D: """3D 乘员检测器""" def detect(self, frame): pass
if __name__ == "__main__": cameras = [ { "id": "driver", "position": [0.3, 0.1, 1.2], "rotation": [0, -15, 0], "fov": 60, "resolution": [1920, 1080] }, { "id": "passenger", "position": [1.2, 0.1, 1.2], "rotation": [0, 15, 0], "fov": 60, "resolution": [1920, 1080] }, { "id": "rear", "position": [0.75, -0.5, 1.5], "rotation": [-10, 0, 0], "fov": 90, "resolution": [1920, 1080] } ] system = CabinPerceptionMapping(cameras) frames = { "driver": np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8), "passenger": np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8), "rear": np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8) } result = system.process_frame(frames) print(f"座舱映射完成") print(f"检测到 {len(result['occupants'])} 名乘员")
|