3

Similiar to OpenCV 2.4 VideoCapture not working on Windows

The result is a window with a grey image for about 2 seconds before closing and displaying "End of video

import cv2
import imutils

vidPath = "filename.mp4"
video = cv2.VideoCapture(vidPath)
index = 0;

while True:

    print (index)
    index += 1
    ret, frame = video.read()

    if ret == False:
         print("End of video")
         break

    frame = imutils.resize(frame, width = 500)

    cv2.imshow("Video", frame)

video.release()
cv2.destroyAllWindows()

'ret' always returns false after the index print gets to about 900, with nothing useful displaying on the window at all. The video in question is roughly 2 minutes long.

Windows 10, Python version 3.5, OpenCV version 3.4.3 (contrib) installed via

pip install opencv-contrib-python

I have checked cv2.getBuildInformation(), FFMpeg is YES (prebuilt Binaries) I have copied opencv_ffmpeg313.dll to the path for python (user/AppData/Local/Programs/Python/Python35-32/Scripts) and (user/AppData/Local/Programs/Python/Python35-32) I also made copies of them called opencv_ffmpeg313_64.dll just in case

I have also tried several different videos, multiple .mp4's, .avi's etc. I have exhausted every fix I have found for this problem and still come up sort.

Alex Ruston
  • 33
  • 1
  • 5
  • could it be the file path? try to use the absolute filepath (don't forget escaping the backslashes), something like `C:\\test\\filename.mp4` . Also try to check video.isOpened() to see if it actually manage to open the file (and that it is able to playback) – api55 Sep 26 '18 at 11:35
  • I have changed it to the absolute file path, and video.isOpened() returns True, no dice – Alex Ruston Sep 26 '18 at 11:39
  • Unfortunately, I don't have a webcam. I'm assuming the fact that the index print out means it reads about 900 frames before printing "End of video", but it just displays as grey nothingness on the window. Also, I got the error ```AttributeError: module 'cv2.cv2' has no attribute 'CV_CAP_PROP_FRAME_COUNT'``` – Alex Ruston Sep 26 '18 at 11:55
  • yeah, i removed the comment because i missed some details in your question, give me a second I am writing the answer – api55 Sep 26 '18 at 11:57
  • Ah, turns out it seems I missed a vital step. I needed to use cv2.waitkey(30) to slow down the playback so it actually becomes visible – Alex Ruston Sep 26 '18 at 12:02
  • yep, that was my answer, but I elaborate it a little bit more to get a more realistic playback :) – api55 Sep 26 '18 at 12:04

1 Answers1

0

Sorry I did't notice all the details of your question...

The problem is that you are missing after the imshow function:

cv2.waitKey(10)

where 10 is the amount of milliseconds to wait. waitKey function does not only checks for keyboard events and returns the key pressed, but also it refreshes the window in which you did imshow (all of the windows if you have more than one). It is similar to the spin function in some GUI libraries...

This explains why the window is grey. At some point it will end reading the video and exits (900 frames later). 900 frames in 30 fps videos is just 30 seconds... if it lasts around 2 minutes it probably is 7.5 fps... or else it may have a faulty frame. To check this you can check the FPS property and the frame count property. with this:

fps = video.get(cv2.CAP_PROP_FPS)
amountOfFrames = video.get(cv2.CAP_PROP_FRAME_COUNT )

and you can even do a more "fancy" playback that takes the fps and uses it as param in the waitKey function like this:

cv2.waitKey(int(1000./float(fps))) # 1000 milliseconds / fps
api55
  • 11,070
  • 4
  • 41
  • 57