Running the following code on my raspberry pi 4b with the camera v2 and OpenCV installed via pip install opencv-python I have no issues:

import cv2
cam = cv2.VideoCapture(0)
cam.isOpened() # True

However, when I installed OpenCV with the manual build described here, with the following variables:


Running the same code gives this error:

[ WARN:0] global /tmp/pip-req-build-4nusm4mm/opencv/modules/videoio/src/cap_gstreamer.cpp (1825) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0] global /tmp/pip-req-build-4nusm4mm/opencv/modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /tmp/pip-req-build-4nusm4mm/opencv/modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0] global /tmp/pip-req-build-4nusm4mm/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

I believe I have all the required dependencies and gstreamer libs. I have tried multiple different things including changing /dev/video0 permissions and sudo modprobe bcm2835-v4l2 all to no avail.

OpenCV version 4.5.1, GStreamer version 1.14.4


I got the same problem when using raspberrypi camera the problem was that it outputs NV12 format not RGB you can use this code to test your camera https://github.com/JetsonHacksNano/CSI-Camera

or just add "format=(string)NV12" as a parameter of cv2.VideoCapture

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .