DepthAI
  • DepthAI组件
    • AprilTags
    • 基准测试
    • 相机
    • 校准
    • DetectionNetwork
    • 事件
    • FeatureTracker
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • 杂项
    • 模型库
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • 点云
    • RecordReplay
    • RGBD
    • 脚本
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • 同步
    • VideoEncoder
    • 可视化器
    • VSLAM
    • 扭曲
    • RVC2 特有
  • 高级教程
  • 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}

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。