DepthAI
  • DepthAI Components
    • AprilTags
    • Benchmark
    • Camera
    • Calibration
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • Misc
    • Model Zoo
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • PointCloud
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • VSLAM
    • Warp
    • RVC2-specific
  • Advanced Tutorials
  • API Reference
  • Tools
Software Stack

ON THIS PAGE

  • Pipeline
  • Source code

Sync

Supported on:RVC2RVC4
This example shows how to synchronize two video streams, each coming from a different camera.This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python

Python
GitHub
1import depthai as dai
2
3pipeline = dai.Pipeline()
4left = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
5right = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
6
7
8sync = pipeline.create(dai.node.Sync)
9sync.setRunOnHost(True) # Can also run on device
10left.requestFullResolutionOutput().link(sync.inputs["left"])
11right.requestFullResolutionOutput().link(sync.inputs["right"])
12
13outQueue = sync.out.createOutputQueue()
14pipeline.start()
15
16
17while pipeline.isRunning():
18    messageGroup : dai.MessageGroup = outQueue.get()
19    left = messageGroup["left"]
20    right = messageGroup["right"]
21    print(f"Timestamps, message group {messageGroup.getTimestamp()}, left {left.getTimestamp()}, right {right.getTimestamp()}")

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4
5#include "depthai/depthai.hpp"
6
7std::atomic<bool> quitEvent(false);
8
9void signalHandler(int) {
10    quitEvent = true;
11}
12
13int main() {
14    signal(SIGTERM, signalHandler);
15    signal(SIGINT, signalHandler);
16
17    // Create pipeline
18    dai::Pipeline pipeline;
19
20    // Create and configure camera nodes
21    auto left = pipeline.create<dai::node::Camera>();
22    left->build(dai::CameraBoardSocket::CAM_B);
23
24    auto right = pipeline.create<dai::node::Camera>();
25    right->build(dai::CameraBoardSocket::CAM_C);
26
27    // Create and configure sync node
28    auto sync = pipeline.create<dai::node::Sync>();
29    sync->setRunOnHost(true);  // Can also run on device
30
31    // Link cameras to sync inputs
32    left->requestFullResolutionOutput()->link(sync->inputs["left"]);
33    right->requestFullResolutionOutput()->link(sync->inputs["right"]);
34
35    // Create output queue
36    auto outQueue = sync->out.createOutputQueue();
37
38    pipeline.start();
39
40    while(pipeline.isRunning() && !quitEvent) {
41        auto messageGroup = outQueue->get<dai::MessageGroup>();
42        if(messageGroup == nullptr) continue;
43
44        auto leftMsg = messageGroup->get<dai::ImgFrame>("left");
45        auto rightMsg = messageGroup->get<dai::ImgFrame>("right");
46
47        std::cout << "Timestamps, message group " << messageGroup->getTimestamp().time_since_epoch().count() << std::endl;
48        std::cout << "left " << leftMsg->getTimestamp().time_since_epoch().count() << std::endl;
49        std::cout << "right " << rightMsg->getTimestamp().time_since_epoch().count() << std::endl;
50
51        if(cv::waitKey(1) == 'q') {
52            break;
53        }
54    }
55
56    pipeline.stop();
57    pipeline.wait();
58
59    return 0;
60}

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.