I am running the following code on Raspberry Pi with pi camera, I have the broadcom drivers for it and all, but I am getting an error. Perhaps something to do with the dimensions of the video feed, but I do not know how to set it on Linux.
Code:
import cv2 import numpy as np cap = cv2.VideoCapture() while True: ret, img = cap.read() cv2.imshow('img', img) if cv2.waitKey(0) & 0xFF == ord('q): break
Error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp, line 325 Traceback (most recent call last): File "check_picam_with_opencv.py", line 10, in <module> cv2.imshow('img', img) cv2.error: /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function imshow
Advertisement
Answer
Provide an id to VideoCapture
.
cap = cv2.VideoCapture(0)
Also check the value of ret
, see if it’s TRUE
or FALSE
print (ret)
Edit:
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera.
cap = cv2.VideoCapture(0)
To check whether the
cap
has been initialized or not, you can usecap.isOpened()
function, which returnsTrue
for successful initialization andFalse
for failure.
if cap.isOpened() == False: print ("VideoCapture failed")
cap.read()
returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
ret, frame = cap.read() if ret == False: print("Frame is empty")
Further reading here.