0

The program needs to read a video, do the counting of frames and then show in a window. The error occurs after the video ends. How can i solve this?

Here is the source:

import tkMessageBox
import cv2

banner = cv2.imread('../data/banner.png')
video = cv2.VideoCapture('../data/pintado_real_crop.avi')
contadorDeFrames = True
contador = 0


cv2.imshow('Projeto Pacu', banner)

cv2.moveWindow('Projeto Pacu', 100,50)

while(contadorDeFrames == True):

    contadorDeFrames, frame = video.read()
    contador = contador + 1

    cv2.imshow("Video", frame)
    cv2.moveWindow('Video', 100, 178)

    print"Frame: %d" %contador

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

tkMessageBox.showinfo("Frames contador: %d" %contador)
video.release()
cv2.destroyAllWindows()

Complete Error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp, line 269
Traceback (most recent call last):
  File "/home/vagner/PycharmProjects/TesteVideo/src/TestaVideo.py", line 20, in <module>
    cv2.imshow("Video", frame)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow
Borealis
  • 8,044
  • 17
  • 64
  • 112
Vagner Garcia
  • 35
  • 1
  • 1
  • 5
  • That generally means it failed to load the image, or the image is empty. – Lilith Daemon Aug 11 '15 at 12:50
  • add something like `if not contadorDeFrames: break or do whatever should happen after the video` directly after `video.read()` – Micka Aug 11 '15 at 13:12
  • Micka it works, thanks for your help. – Vagner Garcia Aug 11 '15 at 14:02
  • Possible duplicate of [OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code](http://stackoverflow.com/questions/31341845/opencv-error-assertion-failed-size-width0-size-height0-simple-code) – Dan Mašek Apr 04 '16 at 13:28

2 Answers2

0

In your code, you are trying to print the null frame:

contadorDeFrames, frame = video.read()
contador = contador + 1
cv2.imshow("Video", frame)

when your code reads the frame which is last frame+1, the value will be null and you are trying to print that frame. So check whether the frame is null or not and print.

Arun Sooraj
  • 737
  • 9
  • 20
0

I don´t have an answer for the question but i can specify from where the error came and i have a workaround.

Like you see in my first example i tried to print a quit in the if statement and noticed that it doesnt get called. So i showed how much Frames the video had and let the if statement get called by the framenumbers. So the Error Disapear.

Example 1:

   import numpy as np
   import cv2

    cap = cv2.VideoCapture('richtigcounter.mp4')
    frames =1
    while True:
    ret, frame = cap.read()
    print ("test")
    cv2.imshow('frame',frame)
    frames =frames + 1
    print (frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Quit")
break

cap.release()
cv2.destroyWindow(cap)

Example 2:

    import numpy as np
    import cv2
    import time

    def Reinfall():
        cap = cv2.VideoCapture('reinfallcounter.mp4')
        frames =1

        while True:
        ret, frame = cap.read()




        cv2.imshow('video', frame)
        frames =frames + 1
        print (frames)
        cv2.waitKey(1)
        if frames == 245:
          print("Quit")
          break

cap.release()
cv2.destroyAllWindows()