UVC & Disparity
cv2.VideoCapture(), native camera apps, and more.How It Works:
Similar samples:
Setup
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.pyCode used for testing
Python
1import cv2
2
3# Initialize the VideoCapture object to use the default camera (camera index 0 is webcam)
4cap = cv2.VideoCapture(1)
5
6# Check if the camera opened successfully
7if not cap.isOpened():
8 print("Error: Could not open camera.")
9 exit()
10
11# Loop to continuously get frames from the camera
12while 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
24cap.release()
25cv2.destroyAllWindows()Source code
Python
Python
PythonGitHub
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 breakPipeline
Need assistance?
Head over to Discussion Forum for technical support or any other questions you might have.