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

本页目录

  • Pipeline
  • Source code

相机以最高分辨率拍摄静态图像

Supported on:RVC2RVC4
此示例演示了在向主机流式传输低分辨率帧的同时,如何捕获最高分辨率的 still 图像。默认情况下,相机将以 (1333x1000) 帧流式传输到主机计算机,当用户按下 c 键时,它将以最高分辨率(对于 OAK4 相机为 48MP)捕获 still 图像,并将其保存到当前目录的 .png 文件中。此示例需要 DepthAI v3 API,请参阅 安装说明

Pipeline

Source code

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5import numpy as np
6
7# Create pipeline
8with dai.Pipeline() as pipeline:
9    # Define source and output
10    cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
11    cam_q_in = cam.inputControl.createInputQueue()
12
13    cam_input_q = cam.inputControl.createInputQueue()
14    # In some cases (IMX586), this requires an 8k screen to be able to see the full resolution at once
15    stream_highest_res = cam.requestFullResolutionOutput(useHighestResolution=True)
16
17    script = pipeline.create(dai.node.Script)
18    stream_highest_res.link(script.inputs["in"])
19    # Current workaround for OAK4 cameras, as Camera node doesn't yet support "still" frame capture
20    script.setScript(
21        """
22        while True:
23            message = node.inputs["in"].get()
24            trigger = node.inputs["trigger"].tryGet()
25            if trigger is not None:
26                node.warn("Trigger received!")
27                node.io["highest_res"].send(message)
28        """)
29
30    # If 8k, we can only have 1 output stream, so we need to use ImageManip to downscale
31    imgManip = pipeline.create(dai.node.ImageManip)
32    stream_highest_res.link(imgManip.inputImage)
33    imgManip.initialConfig.setOutputSize(1333, 1000)
34    imgManip.setMaxOutputFrameSize(1333*1000*3)
35    downscaled_res_q = imgManip.out.createOutputQueue()
36
37    highest_res_q = script.outputs["highest_res"].createOutputQueue()
38    q_trigger = script.inputs["trigger"].createInputQueue()
39
40    # Connect to device and start pipeline
41    ctrl = dai.CameraControl()
42    pipeline.start()
43    print("To capture an image, press 'c'")
44    while pipeline.isRunning():
45        img_hd: dai.ImgFrame = downscaled_res_q.get()
46        frame = img_hd.getCvFrame()
47        cv2.imshow("video", frame)
48
49        key = cv2.waitKey(1)
50        if key == ord("q"):
51            break
52        if key == ord('c'):
53            # Send a trigger message to the Script node
54            q_trigger.send(dai.Buffer())
55
56        if highest_res_q.has():
57            highres_img = highest_res_q.get()
58            frame = highres_img.getCvFrame()
59            # Save the full image
60            cv2.imwrite("full_image.png", frame)

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4#include <opencv2/opencv.hpp>
5
6#include "depthai/depthai.hpp"
7
8std::atomic<bool> quitEvent(false);
9
10void signalHandler(int) {
11    quitEvent = true;
12}
13
14int main() {
15    signal(SIGTERM, signalHandler);
16    signal(SIGINT, signalHandler);
17
18    // Create device
19    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
20
21    // Create pipeline
22    dai::Pipeline pipeline(device);
23
24    // Create nodes
25    auto cam = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A);
26    auto camQIn = cam->inputControl.createInputQueue();
27
28    // In some cases (IMX586), this requires an 8k screen to be able to see the full resolution at once
29    auto streamHighestRes = cam->requestFullResolutionOutput();
30
31    // Create script node for still capture
32    auto script = pipeline.create<dai::node::Script>();
33    streamHighestRes->link(script->inputs["in"]);
34
35    // Current workaround for OAK4 cameras, as Camera node doesn't yet support "still" frame capture
36    script->setScript(R"(
37        while True:
38            message = node.inputs["in"].get()
39            trigger = node.inputs["trigger"].tryGet()
40            if trigger is not None:
41                node.warn("Trigger received!")
42                node.io["highest_res"].send(message)
43    )");
44
45    // If 8k, we can only have 1 output stream, so we need to use ImageManip to downscale
46    auto imgManip = pipeline.create<dai::node::ImageManip>();
47    streamHighestRes->link(imgManip->inputImage);
48    imgManip->initialConfig->setOutputSize(1333, 1000);
49    imgManip->setMaxOutputFrameSize(1333 * 1000 * 3);
50    auto downscaledResQ = imgManip->out.createOutputQueue();
51
52    auto highestResQ = script->outputs["highest_res"].createOutputQueue();
53    auto qTrigger = script->inputs["trigger"].createInputQueue();
54
55    // Start pipeline
56    pipeline.start();
57    std::cout << "To capture an image, press 'c'" << std::endl;
58
59    while(pipeline.isRunning() && !quitEvent) {
60        auto imgHd = downscaledResQ->get<dai::ImgFrame>();
61        if(imgHd == nullptr) continue;
62
63        cv::Mat frame = imgHd->getCvFrame();
64        cv::imshow("video", frame);
65
66        int key = cv::waitKey(1);
67        if(key == 'q') {
68            break;
69        }
70        if(key == 'c') {
71            // Send a trigger message to the Script node
72            qTrigger->send(std::make_shared<dai::Buffer>());
73        }
74
75        if(highestResQ->has()) {
76            auto highresImg = highestResQ->get<dai::ImgFrame>();
77            if(highresImg != nullptr) {
78                cv::Mat frame = highresImg->getCvFrame();
79                // Save the full image
80                cv::imwrite("full_image.png", frame);
81            }
82        }
83    }
84
85    pipeline.stop();
86    pipeline.wait();
87
88    return 0;
89}

需要帮助?

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