gpt4 book ai didi

python - Numpy 数组作为 Jupyter Notebook 中的视频

转载 作者:行者123 更新时间:2023-12-04 15:55:52 24 4
gpt4 key购买 nike

我有一组从视频中提取的边缘图像(使用 cv2.Canny )。所以数组的大小是TxHxW .哪里T是时间步长,后面的参数是高度和宽度。

目前我在 上显示输出的方式Jupyter 笔记本 使用以下代码:

from IPython.display import HTML
import imageio

imageio.mimwrite('test2.mp4', edges, fps=30)
HTML("""
<video width="480" height="360" controls>
<source src="{0}">
</video>
""".format('./test2.mp4'))

我觉得生活可能没有必要写入文件,并且可能有更好的方法。如果有请告诉我。重点是在 Jupyter notebook 中显示。

如果你需要一个测试用例,让 edges = np.random.randn(100, 80, 80) .

解决方案1:

感谢下面@Alleo 的评论。使用 Ipython 7.6+,您可以执行以下操作:
import imageio; 
from IPython.display import Video;
imageio.mimwrite('test2.mp4', edges, fps=30);
Video('test2.mp4', width=480, height=360) #the width and height option as additional thing new in Ipython 7.6.1

不过,这仍然需要您写入文件。

最佳答案

执行此操作的一种快速方法(例如用于调试目的)是使用 matplotlib inline和 matplotlib animation包裹。像这样的东西对我有用

%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import animation
from IPython.display import HTML

# np array with shape (frames, height, width, channels)
video = np.array([...])

fig = plt.figure()
im = plt.imshow(video[0,:,:,:])

plt.close() # this is required to not display the generated image

def init():
im.set_data(video[0,:,:,:])

def animate(i):
im.set_data(video[i,:,:,:])
return im

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=video.shape[0],
interval=50)
HTML(anim.to_html5_video())

视频将以指定的帧速率循环再现(在上面的示例中,我将动画间隔设置为 50 毫秒,即 20 fps)。

请查看我对 this question 的回答更多细节。

关于python - Numpy 数组作为 Jupyter Notebook 中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51702749/

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