此页面由 AI 自动翻译。查看英文原版
DepthAI
软件栈

本页目录

  • 管道
  • 源代码

自定义服务

Supported on:RVC2RVC4
本示例展示了如何创建 dai.RemoteConnection 并注册自定义可视化服务回调。它启动了一个简单的命名服务(myService),该服务在远程调用时返回固定响应。此示例需要 DepthAI v3 API,请参阅 安装说明

管道

源代码

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}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。