DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考
最后更新

本页目录

  • 设置
  • 用于测试的代码
  • 源代码
  • 管道
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包

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

管道

需要帮助?

请访问讨论论坛以获取技术支持或您可能有的任何其他问题。