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

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.

+6
source share
2 answers

I can reproduce the behavior you described (i.e. cv::VideoCapture >> image locked for 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 a file as fast as my computer. I think that in the absence of ffmpeg opencv uses gstreamer and, in fact, considers the video file as its movie playback.

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

+6
source

I have not tried this yet, but I think it can work for video files stored on a machine with a finite length (i.e. not live webcams). Only a predictable flaw in the algorithm "can" skip frames if it is not processed fast enough, but it probably will not read the next frame. I think that overall, VideoCapture can read more slowly than FPS, but it cannot read faster. Do not quote me on this, but this is what I think I noticed.

Use VideoCapture :: set () to change the FPS to something faster than the file on your computer. CV_CAP_PROP_FPS is what you want to change.

Use the VideoCapture :: get () function to verify that fps is installed correctly.

0
source

Source: https://habr.com/ru/post/981733/


All Articles