Opencv Videocapture and Error: (-215:Assertion Failed) !_Src.Empty() in Function 'Cv::Cvtcolor'

OpenCV VideoCapture and error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

The error you're seeing is one that happens if None gets passed on cv2.cvtColor.

After

ret, img = cap.read()

it's a good idea to check that img is not None before proceeding. Depending on your input source, cap.read() can fail. On one my laptops, it fails at least once before starting to return valid images.

cv2.error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Most of the time when you read !_src.empty() in your stacktrace it means that your frame, that should be a numpy tensor with numbers within it, is empty.

I suggest you to edit your code in the following way:

video = cv2.VideoCapture(0)

while True:
if video.isOpened():
ret, frame = video.read()
if ret:
cv2.imshow("WindowFrame", frame)

if cv2.waitKey(1) == ord('q'):
break

video.release()
cv2.destroyAllWindows()

So first you create the VideoCapture() object specifying the index of the camera

VideoCapture (int index) --> Open a camera for video capturing

Then, inside the while True you check if the camera object has already been initialized with isOpened()

isOpened () const --> Returns true if video capturing has been initialized already.

and if this is true then you can start reading frames from the camera. You do this with video.read() which returns a frame (in particular it reads the next frame) and a boolean value (we called it ret) that tells you if the frame is available or not. Thus, if ret == True, we show the frame with cv2.imshow("WindowFrame", frame).

Furthermore, inside your code there's an error. Since release() method closes the camera, you can not put it inside the while True loop

virtual void release () --> Closes video file or capturing device

Also cv2.destroyAllWindows() must stay outside the loop because otherwise you keep opening and closing the window which shows you the frame.

https://docs.opencv.org/3.4/d8/dfe/classcv_1_1VideoCapture.html



Related Topics



Leave a reply



Submit