此页面由 AI 自动翻译。查看英文原版
DepthAI
  • DepthAI 组件
    • AprilTags
    • 基准测试
    • Camera
    • 校准
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • 杂项
    • 模型动物园
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • 点云
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
  • 高级教程
  • API 参考
  • 工具
软件栈

本页目录

  • 管道
  • 源代码

主机显示

Supported on:RVC2RVC4
本示例演示了如何在 DepthAI 管道中使用 HostDisplay 节点,通过 OpenCV 在设备上显示捕获的帧。HostDisplay 节点是一个自定义主机节点,它从 DepthAI 管道接收图像帧,并使用 OpenCV 的 imshow 函数在主机上显示它们。该示例是手动调用管道队列的 .get() 方法以检索帧并使用 OpenCV 显示它们的替代方案。相反,HostDisplay 节点处理帧检索和显示过程,从而更轻松地可视化管道的输出。它是自定义主机节点最基本的示例。此示例需要 DepthAI v3 API,请参阅 安装说明

管道

源代码

Python

Python
GitHub
1import depthai as dai
2import cv2
3
4
5class HostDisplay(dai.node.HostNode):
6    def build(self, frameOutput: dai.Node.Output):
7        self.link_args(frameOutput) # Has to match the inputs to the `process` method
8
9        # This sends all the processing to the pipeline where it's executed by the `pipeline.runTasks()` or implicitly by `pipeline.run()` method.
10        # It's needed as the GUI window needs to be updated in the main thread, and the `process` method is by default called in a separate thread.
11        self.sendProcessingToPipeline(True)
12        return self
13
14    def process(self, message: dai.ImgFrame):
15        cv2.imshow("HostDisplay", message.getCvFrame())
16        key = cv2.waitKey(1)
17        if key == ord('q'):
18            print("Detected 'q' - stopping the pipeline...")
19            self.stopPipeline()
20
21# with dai.Pipeline() as p:
22p = dai.Pipeline()
23with p:
24    camera = p.create(dai.node.Camera).build()
25    hostDisplay = p.create(HostDisplay).build(camera.requestOutput((300, 300)))
26
27    p.run() # Will block until the pipeline is stopped by someone else (in this case it's the display node)

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
9std::atomic<bool> quitEvent(false);
10
11void signalHandler(int) {
12    quitEvent = true;
13}
14
15// Custom host node for display
16class HostDisplay : public dai::node::CustomNode<HostDisplay> {
17   public:
18    HostDisplay() {
19        sendProcessingToPipeline(true);
20    }
21
22    std::shared_ptr<dai::Buffer> processGroup(std::shared_ptr<dai::MessageGroup> message) override {
23        if(quitEvent) {
24            stopPipeline();
25            return nullptr;
26        }
27        if(message == nullptr) return nullptr;
28
29        auto frame = message->get<dai::ImgFrame>("frame");
30        if(frame == nullptr) return nullptr;
31
32        cv::imshow("HostDisplay", frame->getCvFrame());
33        int key = cv::waitKey(1);
34        if(key == 'q') {
35            std::cout << "Detected 'q' - stopping the pipeline..." << std::endl;
36            stopPipeline();
37        }
38
39        return nullptr;
40    }
41};
42
43int main() {
44    signal(SIGTERM, signalHandler);
45    signal(SIGINT, signalHandler);
46
47    // Create device
48    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
49
50    // Create pipeline
51    dai::Pipeline pipeline(device);
52
53    // Create nodes
54    auto camera = pipeline.create<dai::node::Camera>()->build();
55    auto output = camera->requestOutput(std::make_pair(300, 300));
56
57    // Create display node
58    auto display = pipeline.create<HostDisplay>();
59    output->link(display->inputs["frame"]);
60
61    // Start pipeline
62    pipeline.run();
63
64    return 0;
65}

需要帮助?

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