#!/usr/bin/env python3
import cv2
import depthai as dai
# 创建 Pipeline(流水线)
pipeline = dai.Pipeline()
# 定义输入并输出
camRgb = pipeline.create(dai.node.ColorCamera)
xoutRgb = pipeline.create(dai.node.XLinkOut)
xoutRgb.setStreamName("rgb")
# 设置属性
camRgb.setPreviewSize(300, 300)
camRgb.setInterleaved(False)
camRgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.RGB)
# 连接
camRgb.preview.link(xoutRgb.input)
# 连接到设备并启动 Pipeline(流水线)
with dai.Device(pipeline) as device:
print("已连接摄像头:", device.getConnectedCameraFeatures())
# 打印 USB 速度
print("USB 速度:", device.getUsbSpeed().name)
# Bootloader 版本
if device.getBootloaderVersion() is not None:
print("Bootloader 版本:", device.getBootloaderVersion())
# 设备名称
print("设备名称:", device.getDeviceName(), " 产品名称:", device.getProductName())
# 输出队列将用于从上述输出获取 RGB 帧
qRgb = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
print("按下 'q' 退出,'p' 保存为 png, 'j' 保存为 jpg, 'b' 保存为 bmp")
while True:
inRgb = qRgb.get() # 阻塞调用,等待直到有新数据到达
frame = inRgb.getCvFrame()
# 获取'BGR'(OpenCV 格式)帧
cv2.imshow("rgb", frame)
key = cv2.waitKey(1)
if key == ord("q") or key == ord("Q"):
break
if key == ord("p") or key == ord("P"):
cv2.imwrite("rgb.png", frame)
elif key == ord("j") or key == ord("J"):
cv2.imwrite("rgb.jpg", frame)
elif key == ord("b") or key == ord("B"):
cv2.imwrite("rgb.bmp", frame)