如何阅读scikit-image要处理的mp4视频? [英] How to read mp4 video to be processed by scikit-image?
问题描述
我想对mp4
视频,h264
编码的帧应用scikit-image
函数(特别是模板匹配函数match_template
).对我的应用程序来说,跟踪每个帧的时间很重要,但是我知道帧速率,因此可以轻松地从帧号进行计算.
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.
请注意,我使用的资源较少,并且我希望保持依赖性尽可能的小:无论如何都需要numpy
,并且由于我打算使用scikit-image
,因此我避免导入(并进行编译)openCV
只是为了阅读视频.
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.
我在此页面的底部看到,scikit-image
可以无缝处理存储的视频作为numpy
数组,因此获得该数组将是理想的.
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.
推荐答案
Imageio python包应该可以完成您的操作想.这是使用此软件包的python代码段:
Imageio python package should do what you want. Here is a python snippet using this package:
import pylab
import imageio
filename = '/tmp/file.mp4'
vid = imageio.get_reader(filename, 'ffmpeg')
nums = [10, 287]
for num in nums:
image = vid.get_data(num)
fig = pylab.figure()
fig.suptitle('image #{}'.format(num), fontsize=20)
pylab.imshow(image)
pylab.show()
您还可以直接遍历文件中的图像(请参阅文档):
You can also directly iterate over the images in the file (see the documentation ):
for i, im in enumerate(vid):
print('Mean of frame %i is %1.1f' % (i, im.mean()))
要安装imageio,可以使用pip:
To install imageio you can use pip:
pip install imageio
另一种解决方案是使用 moviepy (使用类似的代码来读取视频),但我认为imageio更轻巧,可以完成工作.
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.
回复第一条评论
为了检查整个文件的标称帧频是否相同,您可以计算迭代器中的帧数:
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:
count = 0
try:
for _ in vid:
count += 1
except RuntimeError:
print('something went wront in iterating, maybee wrong fps number')
finally:
print('number of frames counted {}, number of frames in metada {}'.format(count, vid.get_meta_data()['nframes']))
In [10]: something went wront in iterating, maybee wrong fps number
number of frames counted 454, number of frames in metada 461
为了显示每帧的时间戳:
In order to display the timestamp of each frame:
try:
for num, image in enumerate(vid.iter_data()):
if num % int(vid._meta['fps']):
continue
else:
fig = pylab.figure()
pylab.imshow(image)
timestamp = float(num)/ vid.get_meta_data()['fps']
print(timestamp)
fig.suptitle('image #{}, timestamp={}'.format(num, timestamp), fontsize=20)
pylab.show()
except RuntimeError:
print('something went wrong')
这篇关于如何阅读scikit-image要处理的mp4视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!