0
votes

I'm using openCv and python 2.7.

I'm trying to read a video but the last frame has no type and I can't show it.

My video has 16 frames and frame rate is 4fps.

Printing the types of the frames I get:

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
...
<type 'numpy.ndarray'>
<type 'NoneType'>
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, fi
le C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\highgui\src\
window.cpp, line 271
Traceback (most recent call last):
  File "3_1.py", line 113, in <module>
    cv2.imshow('frame',frame)
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\high
gui\src\window.cpp:271: error: (-215) size.width>0 && size.height>0 in function
cv::imshow

My code is:

cap_2 = cv2.VideoCapture('video.avi')
while(cap_2.isOpened()):
    ret, frame = cap_2.read()

    print type(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap_2.release()
cv2.destroyAllWindows()
1
That's not the last frame, at that point you've already gone past the end. You don't test ret to see whether the read succeeded, nor do you test frame for not being empty, before you pass it to imshow. Furthermore, VideoCapture::isOpened() returns True as long as the video is open. Reading all the frames will not affect that. - Dan Mašek

1 Answers

0
votes

Thank you @DanMašek.

The problem is that I did not test ret to see where the read succeeded. So the file was open but there was no new frame to read and as a result I got NoneType Error.

Thus, my final correct code is:

cap_2 = cv2.VideoCapture('video.avi')
while(cap_2.isOpened()):
    ret, frame = cap_2.read()

    if ret == true:
        print type(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
             break

cap_2.release()
cv2.destroyAllWindows()