Raspberry Pi 5+Module 3 Camera Unable to Read Frames in Python

I ran the following code on the Raspberry Pi 5+Module 3 camera platform.

import cv2

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("open camera failed")
    exit()

while True:
    
    ret, frame = cap.read()

    if ret:
        cv2.imshow('frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        print("read failed")
        break

cap.release()
cv2.destroyAllWindows()

Unable to read correct information in the line “ret, frame=cap. read()”.
Prompt after running the code:

[ WARN:0@0.118] global ./modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0@0.118] global ./modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@0.118] global ./modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

The opencv version I am using is 4.9.0.80.
In fact, I can correctly read frames and display images using Libcamera in C++and Picamera2 in Python.
How can OpenCV solve this problem? Is it a problem with OpenCV adaptation? If so, can the next version solve this problem.

1 Like