55
66import numpy as np
77import pyrealsense2 as rs
8+ from rcs .camera .interface import Calibration
89from rcs .camera .hw import HardwareCamera
910from rcs .camera .interface import CameraFrame , DataFrame , Frame , IMUFrame
1011
@@ -22,10 +23,12 @@ class RealSenseDevicePipeline:
2223 pipeline : rs .pipeline
2324 pipeline_profile : rs .pipeline_profile
2425 camera : RealSenseDeviceInfo
26+ depth_scale : float | None = None
2527
2628
2729class RealSenseCameraSet (HardwareCamera ):
2830 TIMESTAMP_FACTOR = 1e-3
31+ DEPTH_SCALE = 10000
2932
3033 def __init__ (
3134 self ,
@@ -41,6 +44,7 @@ def __init__(
4144 self .enable_imu = enable_imu
4245 self .cameras = cameras
4346 self ._logger = logging .getLogger (__name__ )
47+ # TODO: safe the last 30 recorded frames
4448 self ._last_frameset : dict [rs .stream , rs .frame ] | None = None
4549 self ._frameset_lock = threading .Lock ()
4650 assert (
@@ -168,7 +172,9 @@ def enable_device(self, device_info: RealSenseDeviceInfo, enable_ir_emitter: boo
168172 if sensor .supports (rs .option .emitter_enabled ):
169173 sensor .set_option (rs .option .emitter_enabled , 1 if enable_ir_emitter else 0 )
170174 sensor .set_option (rs .option .laser_power , self .laser_power )
171- self ._enabled_devices [device_info .serial ] = RealSenseDevicePipeline (pipeline , pipeline_profile , device_info )
175+ self ._enabled_devices [device_info .serial ] = RealSenseDevicePipeline (
176+ pipeline , pipeline_profile , device_info , depth_scale = sensor .get_depth_scale ()
177+ )
172178 self ._logger .debug ("Enabled device %s (%s)" , device_info .serial , device_info .product_line )
173179
174180 @staticmethod
@@ -255,7 +261,12 @@ def to_ts(frame: rs.frame) -> float:
255261 timestamps .append (to_ts (frame ))
256262
257263 assert color is not None , "Color frame not found"
258- cf = CameraFrame (color = color , ir = ir , depth = depth )
264+ # TODO: check depth type (should be int16)
265+ cf = CameraFrame (
266+ color = color ,
267+ ir = ir ,
268+ depth = (depth .astype (np .float64 ) * device .depth_scale / self .DEPTH_SCALE ).astype (np .int16 ),
269+ )
259270 imu = IMUFrame (accel = accel , gyro = gyro )
260271 return Frame (camera = cf , imu = imu , avg_timestamp = float (np .mean (timestamps )) if len (timestamps ) > 0 else None )
261272
@@ -303,7 +314,7 @@ def get_device_intrinsics(
303314 device_intrinsics [device_name ][key ] = value .get_profile ().as_video_stream_profile ().get_intrinsics ()
304315 return device_intrinsics
305316
306- def get_depth_to_color_extrinsics (self , frames ):
317+ def get_depth_to_color_extrinsics (self , frames : dict [ rs . stream , rs . frame ] ):
307318 """
308319 Get the extrinsics between the depth imager 1 and the color imager using its frame delivered by the realsense device
309320
@@ -353,7 +364,25 @@ def enable_emitter(self, enable_ir_emitter=True):
353364 if enable_ir_emitter :
354365 sensor .set_option (rs .option .laser_power , self .laser_power )
355366
356-
357- def intrinsics (self , camera_name : str ) -> np .ndarray [tuple [typing .Literal [3 , 4 ]], np .dtype [np .float64 ]]:
358- with self ._frameset_lock :
359- pass
367+ def calibrate (self ) -> dict [str , Calibration ] | None :
368+
369+ intrinsics = {}
370+ for camera_name , camera_config in self .cameras :
371+ serial = camera_config .identifier
372+ device = self ._enabled_devices [serial ]
373+ vp = device .pipeline_profile .get_stream (rs .stream .color ).as_video_stream_profile ()
374+ intrinsics [camera_name ] = vp .get_intrinsics ()
375+
376+ extrinsics = {}
377+ for camera_name , camera_config in self .cameras :
378+ serial = camera_config .identifier
379+ device = self ._enabled_devices [serial ]
380+ depth_vp = device .pipeline_profile .get_stream (rs .stream .depth ).as_video_stream_profile ()
381+ color_vp = device .pipeline_profile .get_stream (rs .stream .color ).as_video_stream_profile ()
382+ depth_to_color = depth_vp .get_extrinsics_to (color_vp )
383+
384+ # calibrate strategy: save the last 30 frames and send it
385+ # it can
386+ # - ask for more new frames
387+ # - calibration fail
388+ # - calibration successful
0 commit comments