DepthAI
Software Stack

ON THIS PAGE

  • Pipeline
  • Source code

Custom Services

Supported on:RVC2RVC4
This example shows how to create a dai.RemoteConnection and register a custom Visualizer service callback. It starts a simple named service (myService) that returns a fixed response when called remotely.This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2import depthai as dai
3import signal
4import sys
5import time
6
7isRunning = True
8def stop():
9    global isRunning
10    isRunning = False
11
12def signal_handler(sig, frame):
13    print('You pressed Ctrl+C!')
14    stop()
15    sys.exit(0)
16
17signal.signal(signal.SIGINT, signal_handler)
18
19remoteConnection = dai.RemoteConnection()
20
21def testService(input):
22    print("Test service called with input:", input)
23    return {"result": "testService result"}
24
25remoteConnection.registerService("myService", testService)
26while isRunning:
27    time.sleep(1)

C++

1#include <atomic>
2#include <chrono>
3#include <csignal>
4#include <iostream>
5#include <memory>
6#include <thread>
7
8#include "depthai/depthai.hpp"
9#include "depthai/remote_connection/RemoteConnection.hpp"
10
11// Global flag for graceful shutdown
12std::atomic<bool> quitEvent(false);
13
14// Signal handler
15void signalHandler(int signum) {
16    std::cout << "You pressed Ctrl+C!" << std::endl;
17    quitEvent = true;
18}
19
20// Test service function
21nlohmann::json testService(const nlohmann::json& input) {
22    std::cout << "Test service called with input: " << input.dump() << std::endl;
23    return {{"result", "testService result"}};
24}
25
26int main() {
27    // Set up signal handlers
28    signal(SIGTERM, signalHandler);
29    signal(SIGINT, signalHandler);
30
31    try {
32        // Create remote connection
33        dai::RemoteConnection remoteConnection;
34
35        // Register service
36        remoteConnection.registerService("myService", testService);
37
38        // Main loop
39        while(!quitEvent) {
40            std::this_thread::sleep_for(std::chrono::seconds(1));
41        }
42
43    } catch(const std::exception& e) {
44        std::cerr << "Error: " << e.what() << std::endl;
45        return 1;
46    }
47
48    return 0;
49}

Need assistance?

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