队列添加回调
队列添加回调
XLinkOut 节点将单色帧和彩色帧都从设备发送到主机。在回调函数 newFrame() 中,我们解码出帧来自哪个摄像头,以便稍后能以正确的标题向用户显示该帧。演示

设置
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py源代码
Python
PythonGitHub
1#!/usr/bin/env python3
2import cv2
3import depthai as dai
4import queue
5
6# Create pipeline
7pipeline = dai.Pipeline()
8
9# Add all three cameras
10camRgb = pipeline.create(dai.node.ColorCamera)
11left = pipeline.create(dai.node.MonoCamera)
12right = pipeline.create(dai.node.MonoCamera)
13
14# Create XLink output
15xout = pipeline.create(dai.node.XLinkOut)
16xout.setStreamName("frames")
17
18# Properties
19camRgb.setPreviewSize(300, 300)
20left.setCamera("left")
21left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
22right.setCamera("right")
23right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
24
25# Stream all the camera streams through the same XLink node
26camRgb.preview.link(xout.input)
27left.out.link(xout.input)
28right.out.link(xout.input)
29
30q = queue.Queue()
31
32def newFrame(inFrame):
33 global q
34 # Get "stream name" from the instance number
35 num = inFrame.getInstanceNum()
36 name = "color" if num == 0 else "left" if num == 1 else "right"
37 frame = inFrame.getCvFrame()
38 # This is a different thread and you could use it to
39 # run image processing algorithms here
40 q.put({"name": name, "frame": frame})
41
42# Connect to device and start pipeline
43with dai.Device(pipeline) as device:
44
45 # Add callback to the output queue "frames" for all newly arrived frames (color, left, right)
46 device.getOutputQueue(name="frames", maxSize=4, blocking=False).addCallback(newFrame)
47
48 while True:
49 # You could also get the data as non-blocking (block=False)
50 data = q.get(block=True)
51 cv2.imshow(data["name"], data["frame"])
52
53 if cv2.waitKey(1) == ord('q'):
54 break管道
需要帮助?
请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。