I have a video file that I am trying to process one frame at a time. I tried using the VideoCapture class to read using the following code type. The problem is that if a video file is recorded at a speed of 25 frames per second, reading occurs at the same pace. How to get frames as fast as my computer can decode them?
I plan to process the video stream and then save it to a file.
import cv2 import sys import time cap = cv2.VideoCapture(sys.argv[1]) start = time.time() counter = 0 while True: counter += 1; image = cap.read()[1] if counter %25 == 0: print "time", time.time() - start
Output: It prints a timestamp every 25 frames. Note how timestamps change almost exactly 1 second on each line => the program processes about 25 frames per second. This is with a video file that is 25 frames per second.
time 1.25219297409 time 2.25236606598 time 3.25211691856 time 4.25237703323 time 5.25236296654 time 6.25234603882 time 7.252161026 time 8.25258207321 time 9.25195503235 time 10.2523479462
VideoCapture may not be the right API for this kind of work, but what to use instead?
Using Linux, Fedora 20, opencv-python 2.4.7 and python 2.7.5.
source share