OpenCV 不会检测 usb 摄像头,只能检测 csi 摄像头

OpenCV won't detect usb camera, only csi camera

我一直在使用 Jetson 运行ning Ubuntu。有两个摄像头连接到它。使用 gstreamer 和 USB uv(紫外光)相机的 CSI 相机。

我可以很好地检测和 运行 CSI 相机。但是每当我尝试连接到 USB 摄像头时,它要么抛出错误,要么尝试连接到 CSI 摄像头。

这是我尝试让 USB 摄像头工作的最新版本的测试代码:

import cv2

# Create the capture objects
usb_cap = cv2.VideoCapture(1)

# If they aren't opened correctly
if not usb_cap.isOpened():
    print("Cannot open usb camera")
    exit()

# Set usb477 height and width
usb_cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
usb_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Window
while True:
    # Capture frame-by-frame
    usb_read, usb_frame = usb_cap.read()

    # if frame isn't read correctly
    if not usb_read:
        print("Can't receive frame from usb (stream end?).\nExiting.")
        break

    # Display the resulting frame
    cv2.imshow("USB frame", usb_frame)

    # Escape the loop by pressing 'q'
    if cv2.waitKey(1) == ord('q'):
        break

# When everything done, release the capture
usb_cap.release()
cv2.destroyAllWindows()

运行 它抛出这一系列错误:

[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1761) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (515) startPipeline OpenCV | GStreamer warning: unable to start pipeline
Can't receive frame from usb (stream end?).
Exiting.
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

如果有人至少能为我指出正确的方向来解决这个问题,我将不胜感激。

您必须指定 V4L 捕获后端才能使用 V4L 控件。 这里似乎没有指定它,使用了gstreamer后端,它用v4l2src实例化了一个管道,但是你不能在创建后更改分辨率。

所以最简单的解决方案就是使用 V4L2 捕获后端:

usb_cap = cv2.VideoCapture(1, cv2.CAP_V4L2)

或构建一个 gstreamer 管道,您可以在其中指定大小(最好同时设置帧速率):

pipeline='v4l2src device=/dev/video1 ! video/x-raw,width=1280,height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
usb_cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)