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

本页目录

  • Pipeline
  • Source code

显示所有摄像头

Supported on:RVC2RVC4
使用 OpenCV 在主机上显示所有摄像头流。它使用 Camera 节点以可用最高分辨率获取摄像头流(使用 cam.requestFullResolutionOutput())。此示例需要 DepthAI v3 API,请参阅 安装说明

Pipeline

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6# Create pipeline
7device = dai.Device()
8with dai.Pipeline(device) as pipeline:
9    outputQueues = {}
10    sockets = device.getConnectedCameras()
11    for socket in sockets:
12        cam = pipeline.create(dai.node.Camera).build(socket)
13        outputQueues[str(socket)] = cam.requestFullResolutionOutput().createOutputQueue()
14
15    pipeline.start()
16    while pipeline.isRunning():
17        for name in outputQueues.keys():
18            queue = outputQueues[name]
19            videoIn = queue.get()
20            assert isinstance(videoIn, dai.ImgFrame)
21            # Visualizing the frame on slower hosts might have overhead
22            cv2.imshow(name, videoIn.getCvFrame())
23
24        if cv2.waitKey(1) == ord("q"):
25            break

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4#include <map>
5#include <memory>
6#include <opencv2/opencv.hpp>
7#include <string>
8
9#include "depthai/depthai.hpp"
10
11std::atomic<bool> quitEvent(false);
12
13void signalHandler(int) {
14    quitEvent = true;
15}
16
17int main() {
18    signal(SIGTERM, signalHandler);
19    signal(SIGINT, signalHandler);
20
21    // Create device
22    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
23
24    // Create pipeline
25    dai::Pipeline pipeline(device);
26
27    // Map to store output queues
28    std::map<std::string, std::shared_ptr<dai::MessageQueue>> outputQueues;
29
30    // Get connected cameras
31    auto sockets = device->getConnectedCameras();
32    for(const auto& socket : sockets) {
33        auto cam = pipeline.create<dai::node::Camera>();
34        cam->build(socket);
35        auto output = cam->requestFullResolutionOutput();
36        outputQueues[dai::toString(socket)] = output->createOutputQueue();
37    }
38
39    pipeline.start();
40    while(pipeline.isRunning() && !quitEvent) {
41        for(const auto& [name, queue] : outputQueues) {
42            auto videoIn = queue->get<dai::ImgFrame>();
43            if(videoIn != nullptr) {
44                // Visualizing the frame on slower hosts might have overhead
45                cv::imshow(name, videoIn->getCvFrame());
46            }
47        }
48
49        if(cv::waitKey(1) == 'q') {
50            break;
51        }
52    }
53
54    pipeline.stop();
55    pipeline.wait();
56
57    return 0;
58}

需要帮助?

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