DepthAI Tutorials
DepthAI API References

ON THIS PAGE

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

UVC & Mono Camera

This example demonstrates how to use a mono camera on your OAK device to function as a 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 MonoCamera node outputs image data in the GRAY8 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.

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 - left mono (grayscale) camera
10mono_left = pipeline.createMonoCamera()
11
12mono_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
13mono_left.setBoardSocket(dai.CameraBoardSocket.CAM_B)
14
15# Create an UVC (USB Video Class) output node
16uvc = pipeline.createUVC()
17
18# Manip for frame type conversion
19manip = pipeline.createImageManip()
20manip.initialConfig.setResize(1280, 720)
21manip.initialConfig.setFrameType(dai.RawImgFrame.Type.NV12)
22manip.setMaxOutputFrameSize(int(1280*720*1.5))
23
24# Linking
25manip.out.link(uvc.input)
26mono_left.out.link(manip.inputImage)
27
28# Note: if the pipeline is sent later to device (using startPipeline()),
29# it is important to pass the device config separately when creating the device
30config = dai.Device.Config()
31config.board.uvc = dai.BoardConfig.UVC(1280, 720)
32config.board.uvc.frameType = dai.ImgFrame.Type.NV12
33# config.board.uvc.cameraName = "My Custom Cam"
34pipeline.setBoardConfig(config.board)
35
36
37# Standard UVC load with depthai
38with dai.Device(pipeline) as device:
39    # Dot projector
40    device.setIrLaserDotProjectorBrightness(765)
41    print("\nDevice started, please keep this process running")
42    print("and open an UVC viewer to check the camera stream.")
43    print("\nTo close: Ctrl+C")
44
45    # Doing nothing here, just keeping the host feeding the watchdog
46    while True:
47        try:
48            time.sleep(0.1)
49        except KeyboardInterrupt:
50            break

Need assistance?

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