OpenCV crashes after playing a video... help.. please!
I am going thru the tutorials to learn OpenCV. And I have a problem. When I run this code:
import cv2
cap = cv2.VideoCapture('C:\Users\wg\174037210.avi')
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('Video', frame)
if cv2.waitKey(75) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
which is plain vanilla code I get this error after the video finishes: Traceback (most recent call last): File "C:/Users/wg/python/video-test.py", line 15, in <module> cv2.imshow('Video', frame) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
The environment is as follows: Windows 7 Professional Python 3.6.5 OpenCV 3.4.3
Any help is greatly appreciated. Thanks!
@wgb-2019, You need to check that your video is finished.
ret
is a flag for it.python users NEVER check their input, or return values.
@berak, As for me, if a python variable isn't used, it's better to name it correspondingly. In example,
_, frame = cap.read()
.... but they HAVE to check the
ret
value !if the movie comes to an end, the last frame will be empty / invalid, and the NEXT line of code will crash.