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

本页目录

  • 演示
  • 源代码
  • 管道

脚本相机控制

此示例演示了如何使用 Script 节点。它控制 ColorCamera 每秒捕获一张静态图像。

演示

相机控制
此示例需要 DepthAI v3 API,请参阅 安装说明

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2import cv2
3import depthai as dai
4
5# Start defining a pipeline
6pipeline = dai.Pipeline()
7
8# Define a source - color camera
9cam = pipeline.create(dai.node.ColorCamera)
10
11# Script node
12script = pipeline.create(dai.node.Script)
13script.setScript("""
14    import time
15    ctrl = CameraControl()
16    ctrl.setCaptureStill(True)
17    while True:
18        time.sleep(1)
19        node.io['out'].send(ctrl)
20""")
21
22# XLinkOut
23xout = pipeline.create(dai.node.XLinkOut)
24xout.setStreamName('still')
25
26# Connections
27script.outputs['out'].link(cam.inputControl)
28cam.still.link(xout.input)
29
30# Connect to device with pipeline
31with dai.Device(pipeline) as device:
32    while True:
33        img = device.getOutputQueue("still").get()
34        cv2.imshow('still', img.getCvFrame())
35        if cv2.waitKey(1) == ord('q'):
36            break

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6int main() {
7    using namespace std;
8
9    // Start defining a pipeline
10    dai::Pipeline pipeline;
11
12    // Define a source - color camera
13    auto colorCam = pipeline.create<dai::node::ColorCamera>();
14
15    // Script node
16    auto script = pipeline.create<dai::node::Script>();
17    script->setScript(R"(
18        import time
19        ctrl = CameraControl()
20        ctrl.setCaptureStill(True)
21        while True:
22            time.sleep(1)
23            node.io['out'].send(ctrl)
24    )");
25
26    // XLinkOut
27    auto xout = pipeline.create<dai::node::XLinkOut>();
28    xout->setStreamName("still");
29
30    // Connections
31    script->outputs["out"].link(colorCam->inputControl);
32    colorCam->still.link(xout->input);
33
34    // Connect to device with pipeline
35    dai::Device device(pipeline);
36    while(true) {
37        auto img = device.getOutputQueue("still")->get<dai::ImgFrame>();
38        cv::imshow("still", img->getCvFrame());
39        if(cv::waitKey(1) == 'q') {
40            break;
41        }
42    }
43
44    return 0;
45}

管道

需要帮助?

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