gpt4 book ai didi

python - 如何使用 `imageio` 在视频中寻找帧?

转载 作者:行者123 更新时间:2023-12-02 01:34:12 25 4
gpt4 key购买 nike

我有一个视频,我只想从视频中提取特定的帧。

目前我做的是:

index = [1,2,3,4,5,6,7,8]
img_list = []
for i, frame in enumerate(iio.imiter("imageio:cockatoo.mp4")):
if i in index:
img_list.append(frame)

img_array = np.asarray(img_list)

有没有办法像在 opencv 中那样“寻找”我想要的帧,如图所示 here

最佳答案

您可以使用 imreadindex 关键字获取特定帧:

import imageio.v3 as iio
import numpy as np

index = [1,2,3,4,5,6,7,8]
img_list = []
for idx in index:
img_list.append(iio.imread("imageio:cockatoo.mp4", index=idx))

img_array = np.stack(img_list)

如果你想要更高的性能,你可以使用 pyav 作为后端而不是默认的 imageio-ffmpeg

import imageio.v3 as iio
import numpy as np

index = [1,2,3,4,5,6,7,8]
img_list = []
for idx in index:
img_list.append(iio.imread("imageio:cockatoo.mp4", index=idx, plugin="pyav"))

img_array = np.stack(img_list)

时间(每次重复 10 次):

>>> timeit.timeit('[iio.imread("imageio:cockatoo.mp4", index=idx) for idx in index]', setup="from __main__ import index, iio", number=10) 
9.865169799999876
>>> timeit.timeit('[iio.imread("imageio:cockatoo.mp4", index=idx, plugin="pyav") for idx in index]', setup="from __main__ import index, iio", number=10)
2.250104900000224

这,ofc,有每次重新打开文件的缺点,你可以避免使用 imopen:

import imageio.v3 as iio
import numpy as np

index = [1,2,3,4,5,6,7,8]

with iio.imopen("imageio:cockatoo.mp4", "r") as img_file:
img_list = [img_file.read(index=idx) for idx in index]

img_array = np.stack(img_list)

不幸的是,imopen 路由目前不适用于 pyav,因为我在编写插件时遗漏了一个错误。但是,既然我现在知道了,我应该能够在未来几天内编写修复程序:)

编辑:错误现已修复。您现在可以使用

import imageio.v3 as iio
import numpy as np

index = [1,2,3,4,5,6,7,8]

with iio.imopen("imageio:cockatoo.mp4", "r", plugin="pyav") as img_file:
img_list = [img_file.read(index=idx) for idx in index]

img_array = np.stack(img_list)

关于python - 如何使用 `imageio` 在视频中寻找帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72773615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com