I have a raspberry pi camera module 3 connected to raspberry pi. It is connected properly, since libcamera-still -o test.jpg
works as intended.
But when I try to capture a stream of frames, I get this error message error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
.
My code looks like this:
cap = cv2.VideoCpture(0)
while True:
_, image = cap.read()
cv2,imshow("video stream", image)
if(cv2.waitkey(1) == ord("q")):
break
cap.release()
cv2.destroyAllWindows()
I have tried what this post says: opencv - unable to capture frame from camera on raspberry pi but it did not help.
So, the solution that worked for me looks kinda like this(I am using picamera2 to capture the camera frames and cv2 to show those frames in a window):
import cv2
from picamera2 import Picamera2
picam2 = Picamera2()
picam2.start()
while True:
image = picam2.capture_array()
cv2.imshow("Frame", image)
if(cv2.waitKey(1) == ord("q")):
cv2.imwrite("test_frame.png", image)
break
cv2.destroyAllWindows()
Install Picamera2 using:
python3 -m pip install Picamera2