DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Queue add callback
  • Demo
  • Setup
  • Source code

Queue add callback

This example shows how to use queue callbacks. It sends both mono frames and color frames from the device to the host via one XLinkOut node. In the callback function newFrame() we decode from which camera did the frame come from so we can later show the frame with correct title to the user.

Demo

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++
Python
GitHub
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

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.