16 - Device Queue Event

This example shows how to use getQueueEvent function in order to be notified when one of the packets from selected streams arrive

Demo

Setup

Please run the following command to install the required dependencies

 python3 -m pip install -U pip
 python3 -m pip install opencv-python
 python3 -m pip install -U --force-reinstall depthai

For additional information, please follow installation guide

Source code

Also available on GitHub

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

"""
 This example demonstrates use of queue events to block a thread until a message
 arrives to any (of the specified) queue
"""

import cv2
import depthai as dai

# Start defining a pipeline
pipeline = dai.Pipeline()

# Create Color and Mono cameras
camRgb = pipeline.createColorCamera()
camMono = pipeline.createMonoCamera()
# Create separate streams for them
xoutRgb = pipeline.createXLinkOut()
xoutMono = pipeline.createXLinkOut()

# Set properties
xoutRgb.setStreamName("rgb")
xoutMono.setStreamName("mono")
# Cap color camera to 5 fps
camRgb.setFps(5)
camRgb.setInterleaved(True)
camRgb.setPreviewSize(300, 300)

# Connect
camRgb.preview.link(xoutRgb.input)
camMono.out.link(xoutMono.input)


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

    # Clear queue events
    device.getQueueEvents()

    while True:
        # Block until a message arrives to any of the specified queues
        queueName = device.getQueueEvent(("rgb", "mono"))

        # Getting that message from queue with name specified by the event
        # Note: number of events doesn't necessarily match number of messages in queues
        # because queues can be set to non-blocking (overwriting) behavior
        message = device.getOutputQueue(queueName).get()

        # Display arrived frames
        if type(message) == dai.ImgFrame:
            cv2.imshow(queueName, message.getCvFrame())

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

Got questions?

We’re always happy to help with code or other questions you might have.