中国 · 南京 · 栖霞区紫东路2号紫东国际创意园B3-2幢5F
+86-18994094214 (仅工作日:8:30~17:30)
contact@oakchina.cn

【152】OAK相机如何保存左右目摄像头的图片帧?

View Categories

【152】OAK相机如何保存左右目摄像头的图片帧?

4 min read

同时保存左右目的图片帧:

#!/usr/bin/env python3

from pathlib import Path

import cv2
import depthai as dai

root = Path(__file__).resolve().parent

# 创建 Pipeline(流水线)
pipeline = dai.Pipeline()

# 定义输入并输出
mono_left = pipeline.create(dai.node.MonoCamera)
mono_right = pipeline.create(dai.node.MonoCamera)
xout_left = pipeline.create(dai.node.XLinkOut)
xout_right = pipeline.create(dai.node.XLinkOut)

xout_left.setStreamName('left')
xout_right.setStreamName('right')

# 设置属性
mono_left.setBoardSocket(dai.CameraBoardSocket.LEFT)
mono_right.setBoardSocket(dai.CameraBoardSocket.RIGHT)

# 设置分辨率
mono_left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
mono_right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)


# 连接
mono_right.out.link(xout_right.input)
mono_left.out.link(xout_left.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())

    # 输出队列将用于从上述输出获取 Mono 帧
    q_left = device.getOutputQueue(name="left", maxSize=4, blocking=False)
    q_right = device.getOutputQueue(name="right", maxSize=4, blocking=False)

    print("按下 'q' 退出,'p' 保存为 png, 'j' 保存为 jpg, 'b' 保存为 bmp")
    print("请不要在中文目录下保存图像!!")

    filename = root.joinpath("Mono").as_posix()
    while True:
        in_left = q_left.get()  # 阻塞调用,等待直到有新数据到达
        in_right = q_right.get()  # 阻塞调用,等待直到有新数据到达

        frame_left = in_left.getCvFrame()
        frame_right = in_right.getCvFrame()
        # 获取'BGR'(OpenCV 格式)帧
        cv2.imshow("MonoLeft", frame_left)
        cv2.imshow("MonoRight", frame_right)

        key = cv2.waitKey(1)

        if key == ord("q") or key == ord("Q"):
            break
        if key == ord("p") or key == ord("P"):
            filepath_left = filename + "Left.png"
            filepath_right = filename + "Right.png"
            cv2.imwrite(filepath_left, frame_left)
            cv2.imwrite(filepath_right, frame_right)
            print("已保存:", filepath_left)
            print("已保存:", filepath_right)
        elif key == ord("j") or key == ord("J"):
            filepath_left = filename + "Left.jpg"
            filepath_right = filename + "Right.jpg"
            cv2.imwrite(filepath_left, frame_left)
            cv2.imwrite(filepath_right, frame_right)
            print("已保存:", filepath_left)
            print("已保存:", filepath_right)
        elif key == ord("b") or key == ord("B"):
            filepath_left = filename + "Left.bmp"
            filepath_right = filename + "Right.bmp"
            cv2.imwrite(filepath_left, frame_left)
            cv2.imwrite(filepath_right, frame_right)
            print("已保存:", filepath_left)
            print("已保存:", filepath_right)

只保存左或右目的图片帧:

#!/usr/bin/env python3

from pathlib import Path

import cv2
import depthai as dai

root = Path(__file__).resolve().parent
# 是否保存左相机图像,True 为保存左相机图像,False 为保存右相机
LEFT = True

# 创建 Pipeline(流水线)
pipeline = dai.Pipeline()

# 定义输入并输出
camMono = pipeline.create(dai.node.MonoCamera)
xoutMono = pipeline.create(dai.node.XLinkOut)

xoutMono.setStreamName("Mono")

# 设置属性
if LEFT:
    camMono.setBoardSocket(dai.CameraBoardSocket.LEFT)
else:
    camMono.setBoardSocket(dai.CameraBoardSocket.RIGHT)

# 设置分辨率
camMono.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)


# 连接
camMono.out.link(xoutMono.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())

    # 输出队列将用于从上述输出获取 Mono 帧
    qMono = device.getOutputQueue(name="Mono", maxSize=4, blocking=False)

    print("按下 'q' 退出,'p' 保存为 png, 'j' 保存为 jpg, 'b' 保存为 bmp")
    print("请不要在中文目录下保存图像!!")

    filename = root.joinpath("Mono").as_posix()
    while True:
        inMono = qMono.get()  # 阻塞调用,等待直到有新数据到达
        frame = inMono.getCvFrame()
        # 获取'BGR'(OpenCV 格式)帧
        cv2.imshow("Mono", frame)

        key = cv2.waitKey(1)

        if key == ord("q") or key == ord("Q"):
            break
        if key == ord("p") or key == ord("P"):
            filepath = filename + ".png"
            cv2.imwrite(filepath, frame)
            print("已保存:", filepath)
        elif key == ord("j") or key == ord("J"):
            filepath = filename + ".jpg"
            cv2.imwrite(filepath, frame)
            print("已保存:", filepath)
        elif key == ord("b") or key == ord("B"):
            filepath = filename + ".bmp"
            cv2.imwrite(filepath, frame)
            print("已保存:", filepath)