1+ import copy
12import logging
2- import queue
3+ import threading
34import typing
45from dataclasses import dataclass
56
@@ -59,7 +60,8 @@ def __init__(
5960 self .resolution_width = sample_camera_config .resolution_width
6061 self .resolution_height = sample_camera_config .resolution_height
6162 self .frame_rate = sample_camera_config .frame_rate
62- self ._frame_buffer : dict [str , queue .Queue ] = {}
63+ self ._frame_buffer_lock : dict [str , threading .Lock ] = {}
64+ self ._frame_buffer : dict [str , list ] = {}
6365
6466 self .D400_config = rs .config ()
6567 self .D400_config .enable_stream (
@@ -181,17 +183,21 @@ def enable_device(self, camera_name: str, device_info: RealSenseDeviceInfo, enab
181183 color_vp = pipeline_profile .get_stream (rs .stream .color ).as_video_stream_profile ()
182184
183185 rs_color_intrinsics = color_vp .get_intrinsics ()
184- color_intrinsics = np .array ([
185- [rs_color_intrinsics .fx , 0 , (rs_color_intrinsics .width - 1 )/ 2 , 0 ],
186- [0 , rs_color_intrinsics .fy , (rs_color_intrinsics .height - 1 )/ 2 , 0 ],
187- [0 , 0 , 1 , 0 ],
188- ])
186+ color_intrinsics = np .array (
187+ [
188+ [rs_color_intrinsics .fx , 0 , (rs_color_intrinsics .width - 1 ) / 2 , 0 ],
189+ [0 , rs_color_intrinsics .fy , (rs_color_intrinsics .height - 1 ) / 2 , 0 ],
190+ [0 , 0 , 1 , 0 ],
191+ ]
192+ )
189193 rs_depth_intrinsics = depth_vp .get_intrinsics ()
190- depth_intrinsics = np .array ([
191- [rs_depth_intrinsics .fx , 0 , (rs_depth_intrinsics .width - 1 )/ 2 , 0 ],
192- [0 , rs_depth_intrinsics .fy , (rs_depth_intrinsics .height - 1 )/ 2 , 0 ],
193- [0 , 0 , 1 , 0 ],
194- ])
194+ depth_intrinsics = np .array (
195+ [
196+ [rs_depth_intrinsics .fx , 0 , (rs_depth_intrinsics .width - 1 ) / 2 , 0 ],
197+ [0 , rs_depth_intrinsics .fy , (rs_depth_intrinsics .height - 1 ) / 2 , 0 ],
198+ [0 , 0 , 1 , 0 ],
199+ ]
200+ )
195201
196202 depth_to_color = depth_vp .get_extrinsics_to (color_vp )
197203
@@ -202,10 +208,13 @@ def enable_device(self, camera_name: str, device_info: RealSenseDeviceInfo, enab
202208 depth_scale = sensor .get_depth_scale (),
203209 color_intrinsics = color_intrinsics ,
204210 depth_intrinsics = depth_intrinsics ,
205- depth_to_color = common .Pose (translation = depth_to_color .translation , rotation = np .array (depth_to_color .rotation ).reshape (3 , 3 )),
211+ depth_to_color = common .Pose (
212+ translation = depth_to_color .translation , rotation = np .array (depth_to_color .rotation ).reshape (3 , 3 )
213+ ),
206214 )
207215
208- self ._frame_buffer [camera_name ] = queue .Queue (maxsize = self .CALIBRATION_FRAME_SIZE )
216+ self ._frame_buffer [camera_name ] = []
217+ self ._frame_buffer_lock [camera_name ] = threading .Lock ()
209218 self ._logger .debug ("Enabled device %s (%s)" , device_info .serial , device_info .product_line )
210219
211220 @staticmethod
@@ -257,7 +266,9 @@ def to_ts(frame: rs.frame) -> float:
257266
258267 color_extrinsics = self .calibration_strategy [camera_name ].get_extrinsics ()
259268 depth_to_color = device .depth_to_color
260- depth_extrinsics = color_extrinsics @ depth_to_color .inverse ().pose_matrix () if color_extrinsics is not None else None
269+ depth_extrinsics = (
270+ color_extrinsics @ depth_to_color .inverse ().pose_matrix () if color_extrinsics is not None else None
271+ )
261272
262273 timestamps = []
263274 for stream in streams :
@@ -275,7 +286,9 @@ def to_ts(frame: rs.frame) -> float:
275286 elif rs .stream .depth == stream .stream_type ():
276287 frame = frameset .get_depth_frame ()
277288 depth = DataFrame (
278- data = (to_numpy (frame ).astype (np .float64 ) * device .depth_scale / BaseCameraSet .DEPTH_SCALE ).astype (np .int16 ),
289+ data = (to_numpy (frame ).astype (np .float64 ) * device .depth_scale / BaseCameraSet .DEPTH_SCALE ).astype (
290+ np .int16
291+ ),
279292 timestamp = to_ts (frame ),
280293 intrinsics = device .depth_intrinsics ,
281294 extrinsics = depth_extrinsics ,
@@ -302,7 +315,10 @@ def to_ts(frame: rs.frame) -> float:
302315 )
303316 imu = IMUFrame (accel = accel , gyro = gyro )
304317 f = Frame (camera = cf , imu = imu , avg_timestamp = float (np .mean (timestamps )) if len (timestamps ) > 0 else None )
305- self ._frame_buffer [camera_name ].put (f )
318+ with self ._frame_buffer_lock [camera_name ]:
319+ if len (self ._frame_buffer [camera_name ]) >= self .CALIBRATION_FRAME_SIZE :
320+ self ._frame_buffer [camera_name ].pop (0 )
321+ self ._frame_buffer [camera_name ].append (copy .deepcopy (f ))
306322 return f
307323
308324 def disable_streams (self ):
@@ -329,6 +345,12 @@ def enable_emitter(self, enable_ir_emitter=True):
329345
330346 def calibrate (self ) -> bool :
331347 for camera_name in self .cameras :
332- self .calibration_strategy [camera_name ].calibrate (
333- intrinsics = self ._enabled_devices [camera_name ].color_intrinsics , samples = self ._frame_buffer
334- )
348+ if not self .calibration_strategy [camera_name ].calibrate (
349+ intrinsics = self ._enabled_devices [camera_name ].color_intrinsics ,
350+ samples = self ._frame_buffer [camera_name ],
351+ lock = self ._frame_buffer_lock [camera_name ],
352+ ):
353+ self ._logger .warning (f"Calibration of camera { camera_name } failed." )
354+ return False
355+ self ._logger .info ("Calibration successful." )
356+ return True
0 commit comments