DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • 管道

Mono 预览 - 在点投影仪和照明 LED 之间切换

此示例将在 IR 照明 LED 和 IR 点投影仪之间进行切换。默认情况下,示例脚本将以 30FPS 运行左右单色摄像头传感器,并每帧在 IR LED 和点投影仪之间切换 - 这意味着您将获得 15FPS 的 LED 照明帧和 15FPS 的点投影仪照明帧。LED 照明帧可用于您的 AI 视觉任务 和 CV 算法(例如 特征跟踪器),适用于弱光环境。点投影仪照明帧用于 主动立体深度

演示

在视频中,我们将投影仪和 LED 都禁用了大约一秒钟,只是为了展示场景在几乎完全黑暗中的样子。

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6if 1:  # PoE config
7    fps = 30
8    res = dai.MonoCameraProperties.SensorResolution.THE_400_P
9    poolSize = 24  # default 3, increased to prevent desync
10else:  # USB
11    fps = 30
12    res = dai.MonoCameraProperties.SensorResolution.THE_720_P
13    poolSize = 8  # default 3, increased to prevent desync
14
15# Create pipeline
16pipeline = dai.Pipeline()
17
18# Define sources and outputs
19monoL = pipeline.create(dai.node.MonoCamera)
20monoR = pipeline.create(dai.node.MonoCamera)
21
22monoL.setCamera("left")
23monoL.setResolution(res)
24monoL.setFps(fps)
25monoL.setNumFramesPool(poolSize)
26monoR.setCamera("right")
27monoR.setResolution(res)
28monoR.setFps(fps)
29monoR.setNumFramesPool(poolSize)
30
31xoutDotL = pipeline.create(dai.node.XLinkOut)
32xoutDotR = pipeline.create(dai.node.XLinkOut)
33xoutFloodL = pipeline.create(dai.node.XLinkOut)
34xoutFloodR = pipeline.create(dai.node.XLinkOut)
35
36xoutDotL.setStreamName('dot-left')
37xoutDotR.setStreamName('dot-right')
38xoutFloodL.setStreamName('flood-left')
39xoutFloodR.setStreamName('flood-right')
40streams = ['dot-left', 'dot-right', 'flood-left', 'flood-right']
41
42# Script node for frame routing and IR dot/flood alternate
43script = pipeline.create(dai.node.Script)
44script.setProcessor(dai.ProcessorType.LEON_CSS)
45script.setScript("""
46    dotBright = 0.8
47    floodBright = 0.1
48    LOGGING = False  # Set `True` for latency/timings debugging
49
50    node.warn(f'IR drivers detected: {str(Device.getIrDrivers())}')
51
52    flagDot = False
53    while True:
54        # Wait first for a frame event, received at MIPI start-of-frame
55        event = node.io['event'].get()
56        if LOGGING: tEvent = Clock.now()
57
58        # Immediately reconfigure the IR driver.
59        # Note the logic is inverted, as it applies for next frame
60        Device.setIrLaserDotProjectorIntensity(0 if flagDot else dotBright)
61        Device.setIrFloodLightIntensity(floodBright if flagDot else 0)
62        if LOGGING: tIrSet = Clock.now()
63
64        # Wait for the actual frames (after MIPI capture and ISP proc is done)
65        frameL = node.io['frameL'].get()
66        if LOGGING: tLeft = Clock.now()
67        frameR = node.io['frameR'].get()
68        if LOGGING: tRight = Clock.now()
69
70        if LOGGING:
71            latIR      = (tIrSet - tEvent               ).total_seconds() * 1000
72            latEv      = (tEvent - event.getTimestamp() ).total_seconds() * 1000
73            latProcL   = (tLeft  - event.getTimestamp() ).total_seconds() * 1000
74            diffRecvRL = (tRight - tLeft                ).total_seconds() * 1000
75            node.warn(f'T[ms] latEv:{latEv:5.3f} latIR:{latIR:5.3f} latProcL:{latProcL:6.3f} '
76                    + f' diffRecvRL:{diffRecvRL:5.3f}')
77
78        # Sync checks
79        diffSeq = frameL.getSequenceNum() - event.getSequenceNum()
80        diffTsEv = (frameL.getTimestamp() - event.getTimestamp()).total_seconds() * 1000
81        diffTsRL = (frameR.getTimestamp() - frameL.getTimestamp()).total_seconds() * 1000
82        if diffSeq or diffTsEv or (abs(diffTsRL) > 0.8):
83            node.error(f'frame/event desync! Fr-Ev: {diffSeq} frames,'
84                    + f' {diffTsEv:.3f} ms; R-L: {diffTsRL:.3f} ms')
85
86        # Route the frames to their respective outputs
87        node.io['dotL' if flagDot else 'floodL'].send(frameL)
88        node.io['dotR' if flagDot else 'floodR'].send(frameR)
89
90        flagDot = not flagDot
91""")
92
93# Linking
94monoL.frameEvent.link(script.inputs['event'])
95monoL.out.link(script.inputs['frameL'])
96monoR.out.link(script.inputs['frameR'])
97
98script.outputs['dotL'].link(xoutDotL.input)
99script.outputs['dotR'].link(xoutDotR.input)
100script.outputs['floodL'].link(xoutFloodL.input)
101script.outputs['floodR'].link(xoutFloodR.input)
102
103# Connect to device and start pipeline
104with dai.Device(pipeline) as device:
105    queues = [device.getOutputQueue(name=s, maxSize=4, blocking=False) for s in streams]
106
107    while True:
108        for q in queues:
109            pkt = q.tryGet()
110            if pkt is not None:
111                name = q.getName()
112                frame = pkt.getCvFrame()
113                cv2.imshow(name, frame)
114
115        if cv2.waitKey(5) == ord('q'):
116            break

C++

开发中

管道

需要帮助?

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