Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process video files with python OpenCV faster than file frame rate?

I have video file that I am trying to process one frame at a time,. I tried use VideoCapture class to do reading with following type of code. The problem is that if video file is recorded at 25 frames / second, the reading happens at same pace. How to get frames as fast as my computer can decode them?

I plan to process the video stream and then store 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 once every 25 frames. Notice how timestamps change almost exactly by 1 second on every line => program processes about 25 frames per second. This with video file that is 25 frames/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

Probably VideoCapture is the wrong 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.

like image 269
Juha Syrjälä Avatar asked Jan 29 '15 20:01

Juha Syrjälä


People also ask

How do I change the video fps in Python?

waitKey(delayTime) in the loop where you are capturing the frames. This delayTime is in Mili seconds. More the delayTime less will be the fps. For eg if you set delayTime=50 then fps will be 1000/50 i.e 20.

When reading a video file simply which method is used to obtain frames per second?

Common property we may want to know, frame rate or frames per second, is discussed in detail. When reading a video file simply use the get method to obtain frames per second.


1 Answers

I can reproduce the behavior you describe (i.e. cv::VideoCapture >> image locked to the frame rate of the recorded video) if opencv is compiled without ffmpeg support. If I compile opencv with ffmpeg support, I can read images from file as fast as my computer will allow. I think that in the absence of ffmpeg, opencv uses gstreamer and essentially treats the video file like its playing back a movie.

If you are using Linux, this link shows which packages you must install to get ffmpeg support for opencv.

like image 121
jonnew Avatar answered Sep 25 '22 23:09

jonnew