Ask Your Question
2

OpenCV crashes after playing a video... help.. please!

asked 2019-01-09 00:00:04 -0600

wgb-2019 gravatar image

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!

edit retag flag offensive close merge delete

Comments

@wgb-2019, You need to check that your video is finished. ret is a flag for it.

dkurt gravatar imagedkurt ( 2019-01-09 00:57:57 -0600 )edit

python users NEVER check their input, or return values.

berak gravatar imageberak ( 2019-01-09 02:04:33 -0600 )edit

@berak, As for me, if a python variable isn't used, it's better to name it correspondingly. In example,

_, frame = cap.read().

dkurt gravatar imagedkurt ( 2019-01-09 02:11:47 -0600 )edit

... 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.

berak gravatar imageberak ( 2019-01-09 02:14:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2019-01-09 02:16:07 -0600

khaalidi gravatar image

updated 2019-01-09 02:16:52 -0600

You need to check that your video is finished. ret from cap.read() is always True whenever there is a frame, False when we reach to the end of the video and there is no frame to read.

import cv2
cap = cv2.VideoCapture('C:\Users\wg\174037210.avi')
while(cap.isOpened()):
    ret, frame = cap.read()

    if not ret:
        break

    cv2.imshow('Video', frame)
    if cv2.waitKey(75) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-08 23:45:42 -0600

Seen: 4,449 times

Last updated: Jan 09 '19