opencv runtime_error ai_generated partial

VIDEOIO ERROR: V4L2: Pixel format of stream 0 is not supported by OpenCV

ID: opencv/videoio-camera-format-unsupported

Also available as: JSON · Markdown · 中文
82%Fix Rate
83%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.1 active
4.9.0 active

Root Cause

The camera provides a pixel format (e.g., MJPG, YUYV, NV12) that OpenCV's V4L2 backend cannot decode directly without conversion.

generic

中文

摄像头提供的像素格式(例如 MJPG、YUYV、NV12)OpenCV 的 V4L2 后端无法直接解码,需要转换。

Official Documentation

https://docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html

Workarounds

  1. 85% success Before opening the camera, set the desired pixel format using CAP_PROP_FOURCC: cap = cv2.VideoCapture(0); cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
    Before opening the camera, set the desired pixel format using CAP_PROP_FOURCC: cap = cv2.VideoCapture(0); cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
  2. 78% success Use the GStreamer backend instead: cap = cv2.VideoCapture('v4l2src ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    Use the GStreamer backend instead: cap = cv2.VideoCapture('v4l2src ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

中文步骤

  1. Before opening the camera, set the desired pixel format using CAP_PROP_FOURCC: cap = cv2.VideoCapture(0); cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
  2. Use the GStreamer backend instead: cap = cv2.VideoCapture('v4l2src ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

Dead Ends

Common approaches that don't work:

  1. Installing v4l-utils and running v4l2-ctl to change format without restarting the app 70% fail

    The format change is not picked up by an already-opened VideoCapture; need to reopen.

  2. Setting CAP_PROP_FOURCC to 'MJPG' after opening camera 85% fail

    The property must be set before opening the camera or on a closed capture.

  3. Recompiling OpenCV with different V4L2 flags but not rebuilding the app 90% fail

    The app links against the old library; a full rebuild is needed.