Python 如何读取要由 scikit-image 处理的 mp4 视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29718238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read mp4 video to be processed by scikit-image?
提问by gaggio
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.
我想将一个scikit-image
函数(特别是模板匹配函数match_template
)应用于mp4
视频的帧,h264
编码。对于我的应用程序来说,跟踪每一帧的时间很重要,但我知道帧率,因此我可以轻松地根据帧数进行计算。
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.
请注意,我运行的资源很少,我希望尽可能保持依赖关系:numpy
无论如何都需要,而且由于我打算使用scikit-image
,我将避免导入(和编译)openCV
只是为了阅读视频。
I see at the bottom of thispage that scikit-image
can seamleassly process video stored as a numpy
array, obtaining that would thus be ideal.
我在这个页面的底部看到scikit-image
可以无缝处理存储为numpy
数组的视频,因此获得它是理想的。
采纳答案by head7
Imageiopython package should do what you want. Here is a python snippet using this package:
Imageiopython 包应该做你想做的。这是一个使用这个包的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()
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()))
To install imageio you can use pip:
要安装 imageio,您可以使用 pip:
pip install 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.
另一种解决方案是使用moviepy(它使用类似的代码来读取视频),但我认为imageio 更轻巧并且可以完成这项工作。
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:
为了检查整个文件的标称帧速率是否相同,您可以计算迭代器中的帧数:
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')
回答by Alex I
You could use scikit-video, like this:
您可以使用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
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.
这在引擎盖下使用 avconv 或 ffmpeg。性能相当好,与仅在 avconv 中解码视频相比,将数据移动到 python 的开销很小。
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.
scikit-video的优点是API和OpenCV的视频读写API完全一样;只需将 cv2.VideoCapture 替换为 skvideo.io.VideoCapture。
回答by Win GATE ECE
An easy way to read video in python is using skviode. A single line code can help to read entire video.
在 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
http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html