DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • OpenCV support
  • Demo
  • Setup
  • Source code
  • Pipeline

OpenCV support

This example shows API which exposes both numpy and OpenCV compatible image types for easier 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 install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import cv2
4import depthai as dai
5
6# Create pipeline
7pipeline = dai.Pipeline()
8
9# Define source and outputs
10camRgb = pipeline.create(dai.node.ColorCamera)
11xoutVideo = pipeline.create(dai.node.XLinkOut)
12xoutPreview = pipeline.create(dai.node.XLinkOut)
13
14xoutVideo.setStreamName("video")
15xoutPreview.setStreamName("preview")
16
17# Properties
18camRgb.setPreviewSize(300, 300)
19camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
20camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
21camRgb.setInterleaved(True)
22camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
23
24# Linking
25camRgb.video.link(xoutVideo.input)
26camRgb.preview.link(xoutPreview.input)
27
28# Connect to device and start pipeline
29with dai.Device(pipeline) as device:
30
31    video = device.getOutputQueue('video')
32    preview = device.getOutputQueue('preview')
33
34    while True:
35        videoFrame = video.get()
36        previewFrame = preview.get()
37
38        # Get BGR frame from NV12 encoded video frame to show with opencv
39        cv2.imshow("video", videoFrame.getCvFrame())
40        # Show 'preview' frame as is (already in correct format, no copy is made)
41        cv2.imshow("preview", previewFrame.getFrame())
42
43        if cv2.waitKey(1) == ord('q'):
44            break

Pipeline

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.