如何读取要由 scikit-image 处理的 mp4 视频?

How to read mp4 video to be processed by scikit-image?

我想将 scikit-image 函数(特别是模板匹配函数 match_template)应用于 mp4 视频的帧,h264 编码。对于我的应用程序来说,跟踪每一帧的时间很重要,但我知道帧速率,因此我可以轻松地从帧数计算出来。

请注意,我 运行 资源不足,我希望尽可能减少依赖性:无论如何都需要 numpy,因为我打算使用 scikit-image,我会避免导入(和编译)openCV 只是为了阅读视频。

我在 this 页面的底部看到 scikit-image 可以无缝处理存储为 numpy 数组的视频,因此获得它是理想的。

Imageio python 包应该做你想做的。这是使用此包的 python 片段:

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()

也可以直接遍历文件中的图片(see the documentation):

for i, im in enumerate(vid):
    print('Mean of frame %i is %1.1f' % (i, im.mean()))

要安装 imageio,您可以使用 pip:

pip install imageio

另一种解决方案是使用 moviepy(它使用类似的代码来读取视频),但我认为 imageio 更轻便并且可以完成工作。


对第一条评论的回复

为了检查整个文件的标称帧率是否相同,您可以计算迭代器中的帧数:

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

为了显示每一帧的时间戳:

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-video,像这样:

from skvideo.io import VideoCapture

cap = VideoCapture(filename)
cap.open()

while True:
    retval, image = cap.read()
    # image is a numpy array containing the next frame
    # do something with image here
    if not retval:
        break

这在后台使用了 avconv 或 ffmpeg。性能非常好,与仅在 avconv 中解码视频相比,将数据移动到 python 的开销很小。

scikit-video的优点是API和OpenCV的视频reading/writingAPI一模一样;只需将 cv2.VideoCapture 替换为 skvideo.io.VideoCapture.

在 python 中阅读视频的一种简单方法是使用 skviode。一行代码可以帮助阅读整个视频。

import skvideo.io  
videodata = skvideo.io.vread("video_file_name")  
print(videodata.shape)

http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html