24 - OpenCV support

This example shows API which exposes both numpy and OpenCV compatible image types for eaiser usage. It uses ColorCamera node to retrieve both BGR interleaved ‘preview’ and NV12 encoded ‘video’ frames. Both are displayed using functions getFrame and getCvFrame.

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
#!/usr/bin/env python3

import cv2
import depthai as dai

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

# Define a source - color camera
camRgb = pipeline.createColorCamera()
camRgb.setPreviewSize(300, 300)
camRgb.setBoardSocket(dai.CameraBoardSocket.RGB)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
camRgb.setInterleaved(True)
camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)

# Create output
xoutVideo = pipeline.createXLinkOut()
xoutVideo.setStreamName("video")
xoutPreview = pipeline.createXLinkOut()
xoutPreview.setStreamName("preview")

camRgb.preview.link(xoutPreview.input)
camRgb.video.link(xoutVideo.input)

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

    while True:
        # Get preview and video frames
        preview = device.getOutputQueue('preview').get()
        video = device.getOutputQueue('video').get()

        # Show 'preview' frame as is (already in correct format, no copy is made)
        cv2.imshow("preview", preview.getFrame())
        # Get BGR frame from NV12 encoded video frame to show with opencv
        cv2.imshow("video", video.getCvFrame())

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

Got questions?

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