DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI 教程
DepthAI API 参考

本页目录

  • 设置
  • 用于测试的代码
  • 源代码
  • 管道

UVC 与视差

本示例演示如何将您的OAK设备用作UVC网络摄像头。UVC功能允许您在诸如OpenCV的cv2.VideoCapture()、原生相机应用等程序中将OAK设备作为常规网络摄像头使用。

工作原理:

StereoDepth节点以UINT8格式输出图像数据。然而,UVC节点期望接收NV12格式的数据。为了桥接这一差异,使用一个中间节点ImageManip将MonoCamera节点的GRAY8输出转换为NV12格式,然后传递给UVC节点进行流式传输。 这不适用于深度输出,因为深度是UINT16格式,无法转换为NV12。如果启用亚像素视差功能,此示例将无法工作,因为该功能同样输出UINT16格式。

类似示例:

设置

请运行安装脚本以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您需要先下载 depthai-python 仓库,然后运行该脚本。
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
更多信息,请参考安装指南

用于测试的代码

Python
1import cv2
2
3# 初始化VideoCapture对象,使用默认相机(相机索引0为网络摄像头)
4cap = cv2.VideoCapture(1)
5
6# 检查相机是否成功打开
7if not cap.isOpened():
8    print("错误:无法打开相机。")
9    exit()
10
11# 循环持续从相机获取帧
12while True:
13    ret, frame = cap.read()
14
15    if not ret:
16        print("错误:无法读取帧。")
17        break
18
19    cv2.imshow('视频流', frame)
20
21    if cv2.waitKey(1) & 0xFF == ord('q'):
22        break
23
24cap.release()
25cv2.destroyAllWindows()

源代码

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

管道

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。