此页面由 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 参考
  • 工具
软件栈

本页目录

  • Pipeline
  • Source code

Gate

Supported on:RVC2RVC4
在运行时轻松打开和关闭消息流。此示例需要 DepthAI v3 API,请参阅 安装说明

Pipeline

Source code

Python

Python
GitHub
1import time
2import cv2 
3import depthai as dai
4
5device = dai.Device()
6
7withGate = True 
8
9with dai.Pipeline(device) as pipeline:
10    camera = pipeline.create(dai.node.Camera).build()
11    cameraOut = camera.requestOutput((640, 400), fps=30)
12
13    gate  = pipeline.create(dai.node.Gate)
14    cameraOut.link(gate.input)
15    cameraQueue = gate.output.createOutputQueue()
16    gateControlQueue = gate.inputControl.createInputQueue()
17
18    gateControl = dai.GateControl()
19
20    pipeline.start()
21
22    ellapsed = 0
23    startTime = time.monotonic()
24    gateOpen = True
25
26    while pipeline.isRunning():
27        frame = cameraQueue.tryGet()
28        if frame is not None:
29            cv2.imshow("camera", frame.getCvFrame())
30
31        currentTime = time.monotonic()
32        elapsed = currentTime - startTime
33
34        if elapsed > 5.0:  # 5 seconds
35            # Toggle the value
36            gateOpen = not gateOpen
37            # Create and send the control message
38            if (gateOpen):
39                gateControlQueue.send(dai.GateControl.openGate())
40            else:
41                gateControlQueue.send(dai.GateControl.closeGate())
42            
43            print(f"Gate toggled to: {gateOpen}")
44            
45            # Reset the timer
46            startTime = currentTime
47
48        key = cv2.waitKey(1)
49
50        if key == ord('q'):
51            pipeline.stop()
52            break

C++

1#include <atomic>
2#include <chrono>
3#include <csignal>
4#include <iostream>
5
6// DepthAI and OpenCV headers
7#include <opencv2/opencv.hpp>
8
9#include "depthai/depthai.hpp"
10#include "depthai/pipeline/node/Gate.hpp"
11
12std::atomic<bool> quitEvent(false);
13
14void signalHandler(int) {
15    quitEvent = true;
16}
17
18int main() {
19    signal(SIGTERM, signalHandler);
20    signal(SIGINT, signalHandler);
21
22    dai::Pipeline pipeline;
23
24    auto camera = pipeline.create<dai::node::Camera>()->build();
25    auto cameraOut = camera->requestOutput(std::make_pair(640, 400), std::nullopt, dai::ImgResizeMode::CROP, 30);
26
27    auto gate = pipeline.create<dai::node::Gate>();
28
29    cameraOut->link(gate->input);
30
31    auto cameraQueue = gate->output.createOutputQueue();
32
33    auto gateControlQueue = gate->inputControl.createInputQueue();
34
35    pipeline.start();
36
37    auto startTime = std::chrono::steady_clock::now();
38    bool openGate = true;
39
40    while(pipeline.isRunning() && !quitEvent) {
41        auto frame = cameraQueue->tryGet<dai::ImgFrame>();
42        if(frame != nullptr) {
43            cv::imshow("camera", frame->getCvFrame());
44        }
45
46        auto currentTime = std::chrono::steady_clock::now();
47        std::chrono::duration<double> elapsed = currentTime - startTime;
48
49        if(elapsed.count() > 5.0) {
50            openGate = !openGate;
51
52            if(openGate) {
53                gateControlQueue->send(dai::GateControl::openGate());
54            } else {
55                gateControlQueue->send(dai::GateControl::closeGate());
56            }
57
58            std::cout << "Gate toggled to: " << (openGate ? "True" : "False") << std::endl;
59
60            startTime = currentTime;
61        }
62
63        int key = cv::waitKey(1);
64        if(key == 'q') {
65            break;
66        }
67    }
68
69    pipeline.stop();
70    pipeline.wait();
71
72    return 0;
73}

需要帮助?

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