Implementation snapshot supporting the published lesson. Only cited line ranges are included; this is not a runnable source file. Configuration and recorded examples describe this project snapshot.
… uncited lines omitted …17CLOSE_RANGE_MODE = "Close Range Default"18# SDK v2.9.3 Property.h: OB_PROP_DISP_SEARCH_RANGE_MODE_INT 2 means 256.19DISPARITY_256 = 220CLOSE_RANGE_DEPTH_SIZES = ((640, 480), (848, 480), (848, 530))212223def configure_closest_range(device, sdk):24 """Require the advertised close-range preset and verify its 256 search range.2526 This must run before creating the pipeline or enumerating stream profiles:27 Default mode advertises only disparity ranges up to 128 on firmware 1.0.70.28 """29 modes = device.get_depth_work_mode_list()30 advertised = [modes.get_depth_work_mode_by_index(index)31 for index in range(modes.get_count())]32 mode = next((mode for mode in advertised if mode.name == CLOSE_RANGE_MODE), None)33 if mode is None:34 raise RuntimeError(35 f"Gemini 305 does not advertise {CLOSE_RANGE_MODE!r}; closest-range capture "36 f"requires compatible firmware. Advertised modes: {[mode.name for mode in advertised]}")37 if device.get_depth_work_mode().name != CLOSE_RANGE_MODE:38 status = device.set_depth_work_mode(mode)39 if status != sdk.OBStatus.STATUS_OK:40 raise RuntimeError(f"Gemini 305 could not select {CLOSE_RANGE_MODE!r}: {status}")41 if device.get_depth_work_mode().name != CLOSE_RANGE_MODE:42 raise RuntimeError("Gemini 305 close-range depth mode readback differs from the requested preset.")43 properties = ((sdk.OBPropertyID.OB_PROP_DISP_SEARCH_RANGE_MODE_INT, DISPARITY_256),44 (sdk.OBPropertyID.OB_PROP_DISP_SEARCH_OFFSET_INT, 0))45 for prop, requested in properties:46 if not device.is_property_supported(prop, sdk.OBPermissionType.PERMISSION_READ_WRITE):47 raise RuntimeError(f"Gemini 305 cannot configure/read back closest-range property {prop}.")48 bounds = device.get_int_property_range(prop)49 if not bounds.min <= requested <= bounds.max:50 raise RuntimeError(f"Gemini 305 closest-range property {prop} does not support {requested}.")51 if device.get_int_property(prop) != requested:52 device.set_int_property(prop, requested)53 if device.get_int_property(prop) != requested:54 raise RuntimeError(f"Gemini 305 closest-range property {prop} readback differs from {requested}.")55 logging.getLogger("object_scanner").info(56 "Gemini 305 verified depth mode=%s, disparity search=256, offset=0", CLOSE_RANGE_MODE)57 return dict(depth_work_mode=CLOSE_RANGE_MODE, disparity_search_range=256,58 disparity_search_offset=0, specified_minimum_depth_m=0.04)… uncited lines omitted …298 def _check_pair_timestamps(self, color, depth):299 """Host aggregation does not prove simultaneous exposure; bound device skew."""300 skew_us = abs(int(color.get_timestamp_us()) - int(depth.get_timestamp_us()))301 self.info["last_frame_skew_us"] = skew_us302 if self._fps is not None and skew_us > 500_000 / self._fps:303 rejected = self.info.get("rejected_frame_pairs", 0) + 1304 self.info["rejected_frame_pairs"] = rejected305 if rejected == 1 or rejected % 100 == 0:306 logging.getLogger("object_scanner").warning(307 "Dropping Gemini 305 RGB-D pair with %d us skew (>half-period at %s FPS); rejected=%d",308 skew_us, self._fps, rejected)309 return False310 return True311312 def _geometry(self, color, depth):313 color_profile = color.get_stream_profile().as_video_stream_profile()314 depth_profile = depth.get_stream_profile().as_video_stream_profile()315 color_k, depth_k = _intrinsic(color_profile), _intrinsic(depth_profile)316 size = (color.get_width(), color.get_height())317 if size != (depth.get_width(), depth.get_height()):318 raise RuntimeError("Orbbec aligned depth dimensions differ from color dimensions.")319 if not np.allclose(color_k, depth_k, rtol=1e-6, atol=1e-5):320 raise RuntimeError("Orbbec aligned depth intrinsics differ from rectified color intrinsics.")321 depth_coeffs = _coefficients(depth_profile.get_distortion())322 if not np.isfinite(depth_coeffs).all() or np.any(np.abs(depth_coeffs) > 1e-8):323 raise RuntimeError("Orbbec aligned depth is not rectified; refusing inconsistent RGB-D geometry.")324 extrinsic = depth_profile.get_extrinsic_to(color_profile)325 if not np.allclose(np.asarray(extrinsic.rot).reshape(3, 3), np.eye(3), atol=1e-5) \326 or not np.allclose(extrinsic.transform, 0, atol=1e-4):327 raise RuntimeError("Orbbec aligned depth is not in the color optical frame.")328 distortion = color_profile.get_distortion()329 coeffs = _coefficients(distortion)330 if not np.isfinite(coeffs).all():331 raise RuntimeError("Gemini 305 returned invalid color distortion coefficients.")332 key = (size, tuple(color_k.ravel()), tuple(coeffs), distortion.model)333 if key == self._geometry_key:334 return335 self.maps = None336 if np.any(np.abs(coeffs) > 1e-8):337 if distortion.model not in (self.sdk.OBCameraDistortionModel.BROWN_CONRADY,338 self.sdk.OBCameraDistortionModel.BROWN_CONRADY_K6):339 raise RuntimeError(f"Unsupported Gemini 305 color distortion model: {distortion.model}")340 self.maps = cv2.initUndistortRectifyMap(color_k, coeffs, None, color_k, size, cv2.CV_32FC1)341 self.intrinsic = color_k342 self._geometry_key = key343344 def read(self, near, far, fov_keep, downscale):345 frames = self.pipeline.wait_for_frames(5000)346 timestamp_ns = time.time_ns()347 if frames is None:348 return None349 if frames.get_color_frame() is None or frames.get_depth_frame() is None:350 return None351 if not self._check_pair_timestamps(frames.get_color_frame(), frames.get_depth_frame()):352 return None353 aligned = self.align.process(frames)354 if aligned is None:355 return None356 aligned = aligned.as_frame_set()357 color, depth = aligned.get_color_frame(), aligned.get_depth_frame()358 if color is None or depth is None:359 return None360 self._geometry(color, depth)361 rgb, raw = color_rgb(color, self.sdk), depth_raw(depth, self.sdk)362 scale = float(depth.get_depth_scale()) * 0.001 # SDK scale is millimetres per count.363 if not np.isfinite(scale) or scale <= 0:364 raise RuntimeError("Gemini 305 returned an invalid metric depth scale.")365 if self.maps is not None:366 rgb = cv2.remap(rgb, *self.maps, cv2.INTER_LINEAR)367 height, width = raw.shape368 intrinsic = self.intrinsic.copy()369 if downscale > 1:370 size = (width // downscale, height // downscale)371 if min(size) < 1:372 raise ValueError("Camera downscale exceeds the frame dimensions.")373 rgb = cv2.resize(rgb, size, interpolation=cv2.INTER_AREA)374 raw = cv2.resize(raw, size, interpolation=cv2.INTER_NEAREST)375 sx, sy = size[0] / width, size[1] / height376 intrinsic[0, 0] *= sx377 intrinsic[1, 1] *= sy378 intrinsic[0, 2] = (intrinsic[0, 2] + 0.5) * sx - 0.5379 intrinsic[1, 2] = (intrinsic[1, 2] + 0.5) * sy - 0.5380 return RGBD(np.ascontiguousarray(rgb), prepare_depth(raw, scale, near, far, fov_keep),381 intrinsic, timestamp_ns)