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

本页目录

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

UVC 与单色相机

本示例演示如何使用 OAK 设备上的单色相机作为网络摄像头。UVC 功能允许您将 OAK 设备用作常规网络摄像头,应用场景包括 OpenCV 的 cv2.VideoCapture()、原生相机应用等。

工作原理:

MonoCamera 节点输出 GRAY8 格式的图像数据。但 UVC 节点需要 NV12 格式的数据。为解决此问题,使用一个中间 ImageManip 节点将 MonoCamera 节点的 GRAY8 输出转换为 NV12 格式,然后传递给 UVC 节点进行串流。

类似示例:

设置

请运行安装脚本以下载所有必需的依赖项。请注意,此脚本必须在 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("Error: Could not open camera.")
9    exit()
10
11# 循环不断从摄像头获取帧
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()

源代码

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

管线

需要帮助?

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