Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion depthai-core
39 changes: 39 additions & 0 deletions docs/source/samples/queue_add_callback.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 :code:`XLinkOut` node. In the callback function :code:`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
####

.. image:: https://user-images.githubusercontent.com/18037362/120119546-309d5200-c190-11eb-932a-8235be7a4aa1.gif

Setup
#####

.. include:: /includes/install_from_pypi.rst

Source code
###########

.. tabs::

.. tab:: Python

Also `available on GitHub <https://github.com/luxonis/depthai-python/blob/main/examples/queue_add_callback.py>`__

.. literalinclude:: ../../../examples/queue_add_callback.py
:language: python
:linenos:

.. tab:: C++

Also `available on GitHub <https://github.com/luxonis/depthai-core/blob/main/examples/src/queue_add_callback.cpp>`__

.. literalinclude:: ../../../depthai-core/examples/src/queue_add_callback.cpp
:language: cpp
:linenos:

.. include:: /includes/footer-short.rst
5 changes: 3 additions & 2 deletions docs/source/tutorials/code_samples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Code samples are used for automated testing. They are also a great starting poin
- :ref:`RGB video` - Displays high resolution frames of the RGB camera
- :ref:`Mono Preview` - Displays right/left mono cameras
- :ref:`Depth Preview` - Displays colorized stereo disparity
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
- :ref:`RGB Encoding` - Encodes RGB (1080P, 30FPS) into :code:`.h265` and saves it on the host
- :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
- :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
Expand Down Expand Up @@ -56,4 +55,6 @@ Code samples are used for automated testing. They are also a great starting poin
.. rubric:: Mixed

- :ref:`System information` - Displays device system information (memory/cpu usage, temperature)
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
- :ref:`Queue add callback` - Demonstrates how to use queue callbacks
6 changes: 5 additions & 1 deletion docs/source/tutorials/mixed_samples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Mixed

../samples/system_information.rst
../samples/opencv_support.rst
../samples/device_queue_event.rst
../samples/queue_add_callback.rst

- :ref:`System information` - Displays device system information (memory/cpu usage, temperature)
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
- :ref:`Queue add callback` - Demonstrates how to use queue callbacks
2 changes: 0 additions & 2 deletions docs/source/tutorials/simple_samples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Simple
../samples/rgb_video.rst
../samples/mono_preview.rst
../samples/depth_preview.rst
../samples/device_queue_event.rst
../samples/rgb_encoding.rst
../samples/rgb_mono_encoding.rst
../samples/encoding_max_limit.rst
Expand All @@ -27,7 +26,6 @@ These samples are great starting point for the gen2 API.
- :ref:`RGB video` - Displays high resolution frames of the RGB camera
- :ref:`Mono Preview` - Displays right/left mono cameras
- :ref:`Depth Preview` - Displays colorized stereo disparity
- :ref:`Device Queue Event` - Demonstrates how to use device queue events
- :ref:`RGB Encoding` - Encodes RGB (1080P, 30FPS) into :code:`.h265` and saves it on the host
- :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
- :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
Expand Down
54 changes: 54 additions & 0 deletions examples/queue_add_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
import cv2
import depthai as dai
import queue

# Create pipeline
pipeline = dai.Pipeline()

# Add all three cameras
camRgb = pipeline.createColorCamera()
left = pipeline.createMonoCamera()
right = pipeline.createMonoCamera()

# Create XLink output
xout = pipeline.createXLinkOut()
xout.setStreamName("frames")

# Properties
camRgb.setPreviewSize(300, 300)
left.setBoardSocket(dai.CameraBoardSocket.LEFT)
left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
right.setBoardSocket(dai.CameraBoardSocket.RIGHT)
right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)

# Stream all the camera streams through the same XLink node
camRgb.preview.link(xout.input)
left.out.link(xout.input)
right.out.link(xout.input)

q = queue.Queue()

def newFrame(inFrame):
global q
# Get "stream name" from the instance number
num = inFrame.getInstanceNum()
name = "color" if num == 0 else "left" if num == 1 else "right"
frame = inFrame.getCvFrame()
# This is a different thread and you could use it to
# run image processing algorithms here
q.put({"name": name, "frame": frame})

# Connect to device and start pipeline
with dai.Device(pipeline) as device:

# Add callback to the output queue "frames" for all newly arrived frames (color, left, right)
device.getOutputQueue(name="frames", maxSize=4, blocking=False).addCallback(newFrame)

while True:
# You could also get the data as non-blocking (block=False)
data = q.get(block=True)
cv2.imshow(data["name"], data["frame"])

if cv2.waitKey(1) == ord('q'):
break