DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • UVC & Disparity
  • How It Works:
  • Similar samples:
  • Setup
  • Code used for testing
  • Source code

UVC & Disparity

This example demonstrates how to use your OAK device as a UVC webcam. The UVC feature allows you to use your OAK device as a regular webcam in applications like OpenCV's cv2.VideoCapture(), native camera apps, and more.

How It Works:

The StereoDepth node outputs image data in the UINT8 format. However, the UVC node expects the data in NV12 format. To bridge this gap, an intermediary ImageManip node is used to convert the GRAY8 output from the MonoCamera node to NV12 format, which is then passed to the UVC node for streaming. This doesn't work with stereo depth output, since depth is UINT16 which we cannot convert to NV12.This example won't work if we enable the subpixel disparity feature, since that outputs UINT16 as well.

Similar samples:

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.

Code used for testing

Command Line
1import cv2
2
3    # Initialize the VideoCapture object to use the default camera (camera index 0 is webcam)
4    cap = cv2.VideoCapture(1)
5
6    # Check if the camera opened successfully
7    if not cap.isOpened():
8        print("Error: Could not open camera.")
9        exit()
10
11    # Loop to continuously get frames from the camera
12    while True:
13        ret, frame = cap.read()
14
15        if not ret:
16            print("Error: Could not read frame.")
17            break
18
19        cv2.imshow('Video Feed', frame)
20
21        if cv2.waitKey(1) & 0xFF == ord('q'):
22            break
23
24    cap.release()
25    cv2.destroyAllWindows()

Source code

Python
Python
GitHub
1#!/usr/bin/env python3
2
3import time
4
5import depthai as dai
6
7pipeline = dai.Pipeline()
8
9# Define a source - two mono (grayscale) cameras
10mono_left = pipeline.createMonoCamera()
11mono_right = pipeline.createMonoCamera()
12
13mono_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
14mono_left.setBoardSocket(dai.CameraBoardSocket.CAM_B)
15
16mono_right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
17mono_right.setBoardSocket(dai.CameraBoardSocket.CAM_C)
18
19# Create stereo depth
20stereo = pipeline.createStereoDepth()
21stereo.initialConfig.setConfidenceThreshold(255)
22stereo.setLeftRightCheck(True)
23# 0.190 values, better for visualization
24stereo.setExtendedDisparity(True)
25
26# Create an UVC (USB Video Class) output node
27uvc = pipeline.createUVC()
28
29# Manip for frame type conversion
30manip = pipeline.createImageManip()
31manip.initialConfig.setResize(1280, 720)
32manip.initialConfig.setFrameType(dai.RawImgFrame.Type.NV12)
33manip.initialConfig.setColormap(dai.Colormap.STEREO_TURBO, stereo.initialConfig.getMaxDisparity())
34manip.setMaxOutputFrameSize(int(1280*720*1.5))
35
36# Linking
37mono_left.out.link(stereo.left)
38mono_right.out.link(stereo.right)
39stereo.disparity.link(manip.inputImage)
40manip.out.link(uvc.input)
41
42# Note: if the pipeline is sent later to device (using startPipeline()),
43# it is important to pass the device config separately when creating the device
44config = dai.Device.Config()
45config.board.uvc = dai.BoardConfig.UVC(1280, 720)
46config.board.uvc.frameType = dai.ImgFrame.Type.NV12
47# config.board.uvc.cameraName = "My Custom Cam"
48pipeline.setBoardConfig(config.board)
49
50
51# Standard UVC load with depthai
52with dai.Device(pipeline) as device:
53    # Dot projector
54    device.setIrLaserDotProjectorBrightness(765)
55    print("\nDevice started, please keep this process running")
56    print("and open an UVC viewer to check the camera stream.")
57    print("\nTo close: Ctrl+C")
58
59    # Doing nothing here, just keeping the host feeding the watchdog
60    while True:
61        try:
62            time.sleep(0.1)
63        except KeyboardInterrupt:
64            break

Need assistance?

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