Skip to content

Commit 500f8e6

Browse files
authored
Merge pull request #246 from luxonis/queue_add_callback_example
added example on how to add a queue callback
2 parents 706aa2f + a4aa31b commit 500f8e6

File tree

6 files changed

+102
-6
lines changed

6 files changed

+102
-6
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Queue add callback
2+
==================
3+
4+
This example shows how to use queue callbacks. It sends both mono frames and color frames from the device to the
5+
host via one :code:`XLinkOut` node. In the callback function :code:`newFrame()` we decode from which camera did
6+
the frame come from so we can later show the frame with correct title to the user.
7+
8+
Demo
9+
####
10+
11+
.. image:: https://user-images.githubusercontent.com/18037362/120119546-309d5200-c190-11eb-932a-8235be7a4aa1.gif
12+
13+
Setup
14+
#####
15+
16+
.. include:: /includes/install_from_pypi.rst
17+
18+
Source code
19+
###########
20+
21+
.. tabs::
22+
23+
.. tab:: Python
24+
25+
Also `available on GitHub <https://github.com/luxonis/depthai-python/blob/main/examples/queue_add_callback.py>`__
26+
27+
.. literalinclude:: ../../../examples/queue_add_callback.py
28+
:language: python
29+
:linenos:
30+
31+
.. tab:: C++
32+
33+
Also `available on GitHub <https://github.com/luxonis/depthai-core/blob/main/examples/src/queue_add_callback.cpp>`__
34+
35+
.. literalinclude:: ../../../depthai-core/examples/src/queue_add_callback.cpp
36+
:language: cpp
37+
:linenos:
38+
39+
.. include:: /includes/footer-short.rst

docs/source/tutorials/code_samples.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Code samples are used for automated testing. They are also a great starting poin
1818
- :ref:`RGB video` - Displays high resolution frames of the RGB camera
1919
- :ref:`Mono Preview` - Displays right/left mono cameras
2020
- :ref:`Depth Preview` - Displays colorized stereo disparity
21-
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
2221
- :ref:`RGB Encoding` - Encodes RGB (1080P, 30FPS) into :code:`.h265` and saves it on the host
2322
- :ref:`RGB & Mono Encoding`- Encodes RGB (1080P, 30FPS) and both mono streams (720P, 30FPS) into :code:`.h265`/:code:`.h264` and saves them on the host
2423
- :ref:`Encoding Max Limit` - Encodes RGB (4k 25FPS) and both mono streams (720P, 25FPS) into :code:`.h265`/:code:`.h264` and saves them on the host
@@ -56,4 +55,6 @@ Code samples are used for automated testing. They are also a great starting poin
5655
.. rubric:: Mixed
5756

5857
- :ref:`System information` - Displays device system information (memory/cpu usage, temperature)
59-
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
58+
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
59+
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
60+
- :ref:`Queue add callback` - Demonstrates how to use queue callbacks

docs/source/tutorials/mixed_samples.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Mixed
88

99
../samples/system_information.rst
1010
../samples/opencv_support.rst
11+
../samples/device_queue_event.rst
12+
../samples/queue_add_callback.rst
1113

1214
- :ref:`System information` - Displays device system information (memory/cpu usage, temperature)
13-
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
15+
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
16+
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
17+
- :ref:`Queue add callback` - Demonstrates how to use queue callbacks

docs/source/tutorials/simple_samples.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Simple
1010
../samples/rgb_video.rst
1111
../samples/mono_preview.rst
1212
../samples/depth_preview.rst
13-
../samples/device_queue_event.rst
1413
../samples/rgb_encoding.rst
1514
../samples/rgb_mono_encoding.rst
1615
../samples/encoding_max_limit.rst
@@ -27,7 +26,6 @@ These samples are great starting point for the gen2 API.
2726
- :ref:`RGB video` - Displays high resolution frames of the RGB camera
2827
- :ref:`Mono Preview` - Displays right/left mono cameras
2928
- :ref:`Depth Preview` - Displays colorized stereo disparity
30-
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
3129
- :ref:`RGB Encoding` - Encodes RGB (1080P, 30FPS) into :code:`.h265` and saves it on the host
3230
- :ref:`RGB & Mono Encoding`- Encodes RGB (1080P, 30FPS) and both mono streams (720P, 30FPS) into :code:`.h265`/:code:`.h264` and saves them on the host
3331
- :ref:`Encoding Max Limit` - Encodes RGB (4k 25FPS) and both mono streams (720P, 25FPS) into :code:`.h265`/:code:`.h264` and saves them on the host

examples/queue_add_callback.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
import cv2
3+
import depthai as dai
4+
import queue
5+
6+
# Create pipeline
7+
pipeline = dai.Pipeline()
8+
9+
# Add all three cameras
10+
camRgb = pipeline.createColorCamera()
11+
left = pipeline.createMonoCamera()
12+
right = pipeline.createMonoCamera()
13+
14+
# Create XLink output
15+
xout = pipeline.createXLinkOut()
16+
xout.setStreamName("frames")
17+
18+
# Properties
19+
camRgb.setPreviewSize(300, 300)
20+
left.setBoardSocket(dai.CameraBoardSocket.LEFT)
21+
left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
22+
right.setBoardSocket(dai.CameraBoardSocket.RIGHT)
23+
right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
24+
25+
# Stream all the camera streams through the same XLink node
26+
camRgb.preview.link(xout.input)
27+
left.out.link(xout.input)
28+
right.out.link(xout.input)
29+
30+
q = queue.Queue()
31+
32+
def 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
43+
with 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

0 commit comments

Comments
 (0)