I would like to apply a scikit-image
function (specifically the template matching function match_template
) to the frames of a mp4
video, h264
encoding. It's important for my application to track the time of each frame, but I know the framerate so I can easily calculate from the frame number.
Please note that I'm running on low resources, and I would like to keep dependencies as slim as possible: numpy
is needed anyway, and since I'm planning to use scikit-image
, I would avoid importing (and compiling) openCV
just to read the video.
I see at the bottom of this page that scikit-image
can seamleassly process video stored as a numpy
array, obtaining that would thus be ideal.
An easy way to read video in python is using skviode. A single line code can help to read entire video.
http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html
You could use scikit-video, like this:
This uses avconv or ffmpeg under the hood. The performance is quite good, with a small overhead to move the data into python compared to just decoding the video in avconv.
The advantage of scikit-video is that the API is exactly the same as the video reading/writing API of OpenCV; just replace cv2.VideoCapture with skvideo.io.VideoCapture.
Imageio python package should do what you want. Here is a python snippet using this package:
You can also directly iterate over the images in the file (see the documentation ):
To install imageio you can use pip:
An other solution would be to use moviepy (which use a similar code to read video), but I think imageio is lighter and does the job.
response to first comment
In order to check if the nominal frame rate is the same over the whole file, you can count the number of frame in the iterator:
In order to display the timestamp of each frame: