librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

Multi-camera synchronization for rosbag files

Open MatsumotoJ opened this issue 8 years ago • 5 comments

Required Info
Camera Model D400
Firmware Version 05.08.15.00
Operating System & Version Win 10
Platform PC
SDK Version 2.10.2

Hello.

I am trying to record a 360 deg 3D video using multiple D400 cameras. I want to use rosbag files for saving the data because of its low computational load.

Here is the code that I tried for the recording (saving one file for each camera):

rs2::context rs_ctx;
const std::string platform_camera_name = "Platform Camera";
i = 0;
for (auto&& dev : rs_ctx.query_devices()) 
{
    if (dev.get_info(RS2_CAMERA_INFO_NAME) == platform_camera_name) continue;

    rs2::pipeline pipe;
    rs2::config cfg;

    cfg.enable_device(dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));
    cfg.enable_stream(RS2_STREAM_COLOR, 848, 480, RS2_FORMAT_BGR8, 30);
    cfg.enable_stream(RS2_STREAM_DEPTH, 848, 480, RS2_FORMAT_Z16, 30);
    cfg.enable_record_to_file("camera." + std::to_string(i+1) + ".bag");

    rs2::pipeline_profile prof = pipe.start(cfg);

    i++;
}

However the recorded files were not synchronized: some cameras start earlier than the others; there also could be frame drops at the different timings for different cameras.

I have following two idea for the synchronization:

  • synchronizing the recorded files in offline by using frame timestamp data and rs2::playback::seek.
  • synchronizing recording/playback pipelines by using rs2::syncer.

But I could not find enough documentations for either function, so I have not successfully implemented the ideas yet.

Could you kindly provide an example of a code for synchronous recording/playback rosbag of multi cameras?

MatsumotoJ avatar May 31 '18 23:05 MatsumotoJ

Hi @MatsumotoJ Your analysis is 100% correct. We are planning to address these points in the future. In addition, we have recently added rs-convert util to extract frames from BAG files as a post-processing step.

dorodnic avatar Jun 03 '18 08:06 dorodnic

Hi dorodnic,

Thank you very much for the info. The code of rs-convert looks a good example to start implementing the first idea. I would share the implementation here, after I success.

Thank you again.

MatsumotoJ avatar Jun 03 '18 13:06 MatsumotoJ

Hi again,

Here is the implementation: https://gist.github.com/MatsumotoJ/e23a80f540b58db12ee92d8a96de1745 (There could be ± 1.0/fps lag in the present implementation; more precise synchronization for ± 0.5/fps lag, would be possible)

Thank you very much for your help.

MatsumotoJ avatar Jun 04 '18 07:06 MatsumotoJ

Hi, @MatsumotoJ ,

How do you make sure that multiple cameras have the same clock? Since the timestamp is based on camera's own hardware clock, I find that timestamps cannot be directly compared between two cameras.

hflsdupont avatar Jul 18 '18 21:07 hflsdupont

Hi @hflsdupont,

I have not checked extensively but so far the code above works for my environment (Firmware difference?). I checked it by capturing a moving object in front of the cameras and then reconstructing a full 3D video with the synchronized frames.

Do you have any idea, @dorodnic ?

MatsumotoJ avatar Jul 19 '18 03:07 MatsumotoJ