DepthAI
Software Stack

ON THIS PAGE

  • Pipeline
  • Source code

Auto Reconnect

Supported on:RVC2RVC4
Demonstrates setting a reconnection callback and limited retry attempts on the device. The example opens a simple camera preview; unplug the device briefly to see reconnection attempts reported via the callback.This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6
7
8def callback(x: dai.Device.ReconnectionStatus):
9    print(f"Reconnecting state {x}")
10
11# Create pipeline
12device = dai.Device()
13device.setMaxReconnectionAttempts(3, callback)
14
15# You can try unplugging the camera to see the reconnection in action
16with dai.Pipeline(device) as pipeline:
17    # Define source and output
18    cam = pipeline.create(dai.node.Camera).build()
19    videoQueue = cam.requestOutput((640,400)).createOutputQueue()
20
21    # Connect to device and start pipeline
22    pipeline.start()
23    while pipeline.isRunning():
24        videoIn = videoQueue.get()
25        assert isinstance(videoIn, dai.ImgFrame)
26        cv2.imshow("video", videoIn.getCvFrame())
27
28        if cv2.waitKey(1) == ord("q"):
29            break

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4#include <memory>
5#include <opencv2/opencv.hpp>
6
7#include "depthai/depthai.hpp"
8
9// Global flag for graceful shutdown
10std::atomic<bool> quitEvent(false);
11
12// Signal handler
13void signalHandler(int signum) {
14    quitEvent = true;
15}
16
17// Reconnection callback function
18void reconnectionCallback(dai::Device::ReconnectionStatus status) {
19    std::cout << "Reconnecting state " << static_cast<int>(status) << std::endl;
20}
21
22int main() {
23    // Set up signal handlers
24    signal(SIGTERM, signalHandler);
25    signal(SIGINT, signalHandler);
26
27    try {
28        // Create device with reconnection settings
29        auto device = std::make_shared<dai::Device>();
30        device->setMaxReconnectionAttempts(3, reconnectionCallback);
31
32        // Create pipeline
33        dai::Pipeline pipeline(device);
34
35        // Define source and output
36        auto cam = pipeline.create<dai::node::Camera>()->build();
37        auto output = cam->requestOutput(std::make_pair(640, 400));
38        auto videoQueue = output->createOutputQueue();
39
40        // Start pipeline
41        pipeline.start();
42        std::cout << "Pipeline started. Try unplugging the camera to see reconnection in action." << std::endl;
43        std::cout << "Press 'q' to quit." << std::endl;
44
45        while(pipeline.isRunning() && !quitEvent) {
46            auto videoIn = videoQueue->get<dai::ImgFrame>();
47            if(videoIn == nullptr) continue;
48
49            cv::imshow("video", videoIn->getCvFrame());
50
51            int key = cv::waitKey(1);
52            if(key == 'q') {
53                break;
54            }
55        }
56
57        // Cleanup
58        pipeline.stop();
59        pipeline.wait();
60
61    } catch(const std::exception& e) {
62        std::cerr << "Error: " << e.what() << std::endl;
63        return 1;
64    }
65
66    return 0;
67}

Need assistance?

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