我有一组从视频中提取的边缘图像(使用cv2.Canny)。所以数组的大小是TxHxW。其中T是时间步骤,下面的参数是高度和宽度。
目前,我在木星笔记本上显示输出的方式有以下代码:
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'))我觉得生活,写文件可能是不必要的,也许有一个更好的方法。如果有,请告诉我。重点是在木星笔记本上展示。
如果您需要一个测试用例,请让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不过,这仍然需要您编写文件。
发布于 2019-07-30 15:53:32
一种快速的方法(例如用于调试)是使用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 ms,即20 fps)。
请查看我对this question的答复,了解更多细节。
https://stackoverflow.com/questions/51702749
复制相似问题