在jupyter-lab中显示数组视频的快速方法 [英] fast way to display video from arrays in jupyter-lab

查看:593
本文介绍了在jupyter-lab中显示数组视频的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jupyter-lab的笔记本中显示来自某些阵列的视频.数组在运行时生成.哪种显示图像的方法可以提供(相对)较高的帧率?使用matplotlib和imshow有点慢.这些图片大约为1.8兆像素. 以上是一个非常小的示例,以可视化我想要实现的目标.

I'm trying to display a video from some arrays in an notebook in jupyter-lab. The arrays are produced at runtime. What method to display the images can deliver a (relatively) high framerate? Using matplotlib and imshow is a bit slow. The pictures are around 1.8 megapixel large. Above some very small example to visualize what I want to achieve.

while(True): #should run at least 30 times per second 
    array=get_image() #returns RGBA numpy array  
    show_frame(array) #function I search for

推荐答案

最快的方法(例如,用于调试目的)是使用matplotlib inline和matplotlib animation包.像这样的东西对我有用

The fastest way (to be used for debug purpose for instance) is to use matplotlib inline and the matplotlib animation package. Something like this worked for me

%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).

The video will be reproduced in a loop with a specified framerate (in the code above I set the interval to 50 ms, i.e., 20 fps).

注意,这是一个快速的解决方法,并且IPython.display具有Video软件包(您可以找到文档

Please note that this is a quick workaround and that IPython.display has a Video package (you can find the documentation here) that allows you to reproduce a video from file or from an URL (e.g., from YouTube). So you might also consider storing your data locally and leverage the built-in Jupyter video player.

这篇关于在jupyter-lab中显示数组视频的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆