此页面由 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
本示例演示了如何使用 HostCamera 节点在 DepthAI 管道中使用主机(例如笔记本电脑的摄像头)的摄像头。这使您可以将网络摄像头(或连接到主机上的任何其他摄像头)作为 DepthAI 管道的一部分运行,并利用 RVC 硬件进行处理。

工作原理

HostCamera 类是一个自定义主机节点(链接),它使用 OpenCV 从主机摄像头捕获帧。然后,捕获的帧将作为 ImgFrame 消息发送到 DepthAI 管道。然后,管道可以使用其他节点(如神经网络、对象跟踪器等)来处理这些帧。HostCamera 节点是一个线程化主机节点,这意味着它在与主管道不同的线程中运行。这允许摄像头独立于管道的其余部分捕获帧,从而确保平稳运行。此示例需要 DepthAI v3 API,请参阅 安装说明

源代码

Python

Python
GitHub
1import depthai as dai
2import cv2
3import time
4
5
6class HostCamera(dai.node.ThreadedHostNode):
7    def __init__(self):
8        super().__init__()
9        self.output = self.createOutput()
10    def run(self):
11        # Create a VideoCapture object
12        cap = cv2.VideoCapture(0)
13        if not cap.isOpened():
14            p.stop()
15            raise RuntimeError("Error: Couldn't open host camera")
16        while self.mainLoop():
17            # Read the frame from the camera
18            ret, frame = cap.read()
19            if not ret:
20                break
21            # Create an ImgFrame message
22            imgFrame = dai.ImgFrame()
23            imgFrame.setData(frame)
24            imgFrame.setWidth(frame.shape[1])
25            imgFrame.setHeight(frame.shape[0])
26            imgFrame.setType(dai.ImgFrame.Type.BGR888i)
27            # Send the message
28            self.output.send(imgFrame)
29            # Wait for the next frame
30            time.sleep(0.1)
31
32with dai.Pipeline(createImplicitDevice=False) as p:
33    hostCamera = p.create(HostCamera)
34    camQueue = hostCamera.output.createOutputQueue()
35
36    p.start()
37    while p.isRunning():
38        image : dai.ImgFrame = camQueue.get()
39        cv2.imshow("HostCamera", image.getCvFrame())
40        key = cv2.waitKey(1)
41        if key == ord('q'):
42            p.stop()
43            break

C++

1#include <atomic>
2#include <chrono>
3#include <csignal>
4#include <iostream>
5#include <memory>
6#include <opencv2/opencv.hpp>
7#include <thread>
8
9#include "depthai/depthai.hpp"
10
11// Global flag for graceful shutdown
12std::atomic<bool> quitEvent(false);
13
14// Signal handler
15void signalHandler(int signum) {
16    quitEvent = true;
17}
18
19// Custom threaded host node for camera capture
20class HostCamera : public dai::node::CustomThreadedNode<HostCamera> {
21   public:
22    HostCamera() {
23        output = std::shared_ptr<dai::Node::Output>(new dai::Node::Output(*this, {"output", DEFAULT_GROUP, {{{dai::DatatypeEnum::ImgFrame, false}}}}));
24    }
25
26    void run() override {
27        std::cout << "HostCamera running" << std::endl;
28        // Create a VideoCapture object
29        cv::VideoCapture cap(0);
30        if(!cap.isOpened()) {
31            std::cerr << "Error: Couldn't open host camera" << std::endl;
32            stopPipeline();
33            return;
34        }
35
36        while(mainLoop()) {
37            // Read the frame from the camera
38            cv::Mat frame;
39            if(!cap.read(frame)) {
40                break;
41            }
42
43            // Create an ImgFrame message
44            auto imgFrame = std::make_shared<dai::ImgFrame>();
45
46            std::vector<uchar> buffer(frame.data, frame.data + frame.total() * frame.elemSize());
47            imgFrame->setData(buffer);
48            imgFrame->setWidth(frame.cols);
49            imgFrame->setHeight(frame.rows);
50            imgFrame->setType(dai::ImgFrame::Type::BGR888i);
51
52            // Send the message
53            output->send(imgFrame);
54
55            // Wait for the next frame
56            std::this_thread::sleep_for(std::chrono::milliseconds(100));
57        }
58
59        cap.release();
60    }
61
62    std::shared_ptr<dai::Node::Output> output;
63};
64
65int main() {
66    // Set up signal handlers
67    signal(SIGTERM, signalHandler);
68    signal(SIGINT, signalHandler);
69
70    // Create pipeline without implicit device
71    dai::Pipeline pipeline(false);
72
73    // Create host camera node
74    auto hostCamera = pipeline.create<HostCamera>();
75    auto camQueue = hostCamera->output->createOutputQueue();
76
77    // Start pipeline
78    pipeline.start();
79    std::cout << "Host camera started. Press 'q' to quit." << std::endl;
80
81    while(pipeline.isRunning() && !quitEvent) {
82        auto image = camQueue->get<dai::ImgFrame>();
83        if(image == nullptr) continue;
84
85        cv::imshow("HostCamera", image->getCvFrame());
86
87        int key = cv::waitKey(1);
88        if(key == 'q') {
89            pipeline.stop();
90            break;
91        }
92    }
93
94    // Cleanup
95    pipeline.stop();
96    pipeline.wait();
97
98    return 0;
99}

需要帮助?

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