From fad5c1fe3c6af77e23ab8bf590ac2464acd9a536 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Wed, 28 Apr 2021 23:44:26 +0100 Subject: [PATCH 1/6] added example on how to add a queue callback --- examples/32_queue_add_callback.py | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 examples/32_queue_add_callback.py diff --git a/examples/32_queue_add_callback.py b/examples/32_queue_add_callback.py new file mode 100755 index 000000000..ea31705bb --- /dev/null +++ b/examples/32_queue_add_callback.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +import cv2 +import depthai as dai +import queue + +# Start defining a pipeline +pipeline = dai.Pipeline() + +# Add all three cameras +camRgb = pipeline.createColorCamera() +camRgb.setPreviewSize(300, 300) + +left = pipeline.createMonoCamera() +left.setBoardSocket(dai.CameraBoardSocket.LEFT) +left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) + +right = pipeline.createMonoCamera() +right.setBoardSocket(dai.CameraBoardSocket.RIGHT) +right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) + +# Create XLink output +xout = pipeline.createXLinkOut() +xout.setStreamName("frames") + +# 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) + +# Pipeline is defined, now we can connect to the device +with dai.Device(pipeline) as device: + device.startPipeline() + + # Output queue will be used to get the frames + qFrames = device.getOutputQueue(name="frames", maxSize=4, blocking=False) + + 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}) + + # Add callback for all newly arrived frames (color, left, right) + qFrames.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 From b202f3fdf5651039524a3165f6b8671f87603440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pi=C5=82atowski?= Date: Wed, 12 May 2021 09:27:46 +0200 Subject: [PATCH 2/6] update 32_queue_add_callback --- ..._add_callback.py => queue_add_callback.py} | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) rename examples/{32_queue_add_callback.py => queue_add_callback.py} (63%) diff --git a/examples/32_queue_add_callback.py b/examples/queue_add_callback.py similarity index 63% rename from examples/32_queue_add_callback.py rename to examples/queue_add_callback.py index ea31705bb..0da9211da 100755 --- a/examples/32_queue_add_callback.py +++ b/examples/queue_add_callback.py @@ -27,26 +27,26 @@ 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}) + + # Pipeline is defined, now we can connect to the device with dai.Device(pipeline) as device: device.startPipeline() - # Output queue will be used to get the frames - qFrames = device.getOutputQueue(name="frames", maxSize=4, blocking=False) - - 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}) - - # Add callback for all newly arrived frames (color, left, right) - qFrames.addCallback(newFrame) + # Output queue will be used to get the frames, callback 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) From 104adb5232d8e6f6b9f469435c8c6ed0089d2383 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Fri, 28 May 2021 20:36:18 +0100 Subject: [PATCH 3/6] Updated comment --- examples/queue_add_callback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/queue_add_callback.py b/examples/queue_add_callback.py index 0da9211da..b1190f9ca 100755 --- a/examples/queue_add_callback.py +++ b/examples/queue_add_callback.py @@ -45,7 +45,7 @@ def newFrame(inFrame): with dai.Device(pipeline) as device: device.startPipeline() - # Output queue will be used to get the frames, callback for all newly arrived frames (color, left, right) + # 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: From 532b327cb30f7b7c4f42c34c3294467486d4bb72 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Sun, 30 May 2021 22:33:56 +0100 Subject: [PATCH 4/6] Added docs page. TODO: add cpp example --- docs/source/samples/queue_add_callback.rst | 39 ++++++++++++++++++++++ docs/source/tutorials/mixed_samples.rst | 4 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 docs/source/samples/queue_add_callback.rst diff --git a/docs/source/samples/queue_add_callback.rst b/docs/source/samples/queue_add_callback.rst new file mode 100644 index 000000000..8f22c21ad --- /dev/null +++ b/docs/source/samples/queue_add_callback.rst @@ -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 `__ + + .. literalinclude:: ../../../examples/queue_add_callback.py + :language: python + :linenos: + + .. tab:: C++ + + Also `available on GitHub `__ + + .. literalinclude:: ../../../depthai-core/examples/src/queue_add_callback.cpp + :language: cpp + :linenos: + +.. include:: /includes/footer-short.rst diff --git a/docs/source/tutorials/mixed_samples.rst b/docs/source/tutorials/mixed_samples.rst index 2cdd51b9e..15ccab93b 100644 --- a/docs/source/tutorials/mixed_samples.rst +++ b/docs/source/tutorials/mixed_samples.rst @@ -8,6 +8,8 @@ Mixed ../samples/system_information.rst ../samples/opencv_support.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 \ No newline at end of file +- :ref:`OpenCV support` - Demonstrates how to retrieve an image frame as an OpenCV frame +- :ref:`Queue add callback` - Demonstrates how to use queue callbacks \ No newline at end of file From 615f440b6a2bb6d8f72d519df354bf88989501bc Mon Sep 17 00:00:00 2001 From: CsabaGergely Date: Mon, 31 May 2021 21:56:28 +0300 Subject: [PATCH 5/6] Synchronize with cpp --- depthai-core | 2 +- docs/source/tutorials/code_samples.rst | 5 +++-- docs/source/tutorials/mixed_samples.rst | 2 ++ docs/source/tutorials/simple_samples.rst | 2 -- examples/queue_add_callback.py | 21 +++++++++------------ 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/depthai-core b/depthai-core index cbedca719..09225f7a3 160000 --- a/depthai-core +++ b/depthai-core @@ -1 +1 @@ -Subproject commit cbedca7193ef3b5e59b5c93cb748b51467ae28ce +Subproject commit 09225f7a38d8eb6294bcbea455503900ba9161bc diff --git a/docs/source/tutorials/code_samples.rst b/docs/source/tutorials/code_samples.rst index a472c2e2a..8d17a1b21 100644 --- a/docs/source/tutorials/code_samples.rst +++ b/docs/source/tutorials/code_samples.rst @@ -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 @@ -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 \ No newline at end of file +- :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 \ No newline at end of file diff --git a/docs/source/tutorials/mixed_samples.rst b/docs/source/tutorials/mixed_samples.rst index 15ccab93b..5168fb9ed 100644 --- a/docs/source/tutorials/mixed_samples.rst +++ b/docs/source/tutorials/mixed_samples.rst @@ -8,8 +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:`Device Queue Event` - Demonstrates how to use device queue events - :ref:`Queue add callback` - Demonstrates how to use queue callbacks \ No newline at end of file diff --git a/docs/source/tutorials/simple_samples.rst b/docs/source/tutorials/simple_samples.rst index 9d2cc7c12..3ed78c076 100644 --- a/docs/source/tutorials/simple_samples.rst +++ b/docs/source/tutorials/simple_samples.rst @@ -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 @@ -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 diff --git a/examples/queue_add_callback.py b/examples/queue_add_callback.py index b1190f9ca..949f9e610 100755 --- a/examples/queue_add_callback.py +++ b/examples/queue_add_callback.py @@ -3,25 +3,25 @@ import depthai as dai import queue -# Start defining a pipeline +# Create pipeline pipeline = dai.Pipeline() # Add all three cameras camRgb = pipeline.createColorCamera() -camRgb.setPreviewSize(300, 300) - left = pipeline.createMonoCamera() -left.setBoardSocket(dai.CameraBoardSocket.LEFT) -left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) - right = pipeline.createMonoCamera() -right.setBoardSocket(dai.CameraBoardSocket.RIGHT) -right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P) # 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) @@ -29,7 +29,6 @@ q = queue.Queue() - def newFrame(inFrame): global q # Get "stream name" from the instance number @@ -40,10 +39,8 @@ def newFrame(inFrame): # run image processing algorithms here q.put({"name": name, "frame": frame}) - -# Pipeline is defined, now we can connect to the device +# Connect to device and start pipeline with dai.Device(pipeline) as device: - device.startPipeline() # 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) From 8f93b1088d49a7a81e91bb7d07c3cf1454931895 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Mon, 7 Jun 2021 12:03:52 +0200 Subject: [PATCH 6/6] Updated depthai-core --- depthai-core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/depthai-core b/depthai-core index 09225f7a3..45e011f13 160000 --- a/depthai-core +++ b/depthai-core @@ -1 +1 @@ -Subproject commit 09225f7a38d8eb6294bcbea455503900ba9161bc +Subproject commit 45e011f13f3dcb1f2a7e1f01fabbd0c771379308