【问题标题】:How to display a video in colab, using a PyTorch tensor of RGB image arrays?如何使用 RGB 图像数组的 PyTorch 张量在 colab 中显示视频?
【发布时间】:2021-07-19 12:00:59
【问题描述】:

我有一个形状为(125, 3, 128, 128)的张量:

  • 125 帧
  • 3 通道 (RGB)
  • 每帧 128 x 128 尺寸。
  • 张量中的值在[0,1] 范围内。

我想在 Google Colab 中使用 Pytorch 显示这 125 帧的视频。我该怎么做?

【问题讨论】:

    标签: python video pytorch google-colaboratory tensor


    【解决方案1】:

    在 Colab 中启用内联动画的一种方法是使用 jshtml

    from matplotlib import rc
    rc('animation', html='jshtml')
    

    启用此功能后,您可以绘制动画 like so(请注意,您需要 permute 您的图像张量才能以 PIL/matplotlib 格式获取它们):

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    fig, ax = plt.subplots()
    
    imgs = torch.rand(10,3,128,128)
    imgs = imgs.permute(0,2,3,1) # Permuting to (Bx)HxWxC format
    frames = [[ax.imshow(imgs[i])] for i in range(len(imgs))]
    
    ani = animation.ArtistAnimation(fig, frames)
    ani
    

    【讨论】:

    • 谢谢,但它不适用于我的代码。我哪里出错了?我怎样才能把截图放在这里?
    • @SRIVINODPALACHARLA 你能更明确地说明出了什么问题吗?不要添加屏幕截图,添加您的代码的minimal reproduceable example 和错误输出。
    • for i,[inp] in enumerate(train_loader, 0): print(inp.shape) #(8,3,128,128) from matplotlib import rc rc('animation', html='jshtml')导入 matplotlib.pyplot 作为 plt 导入 matplotlib.animation 作为动画 inp = inp.permute(0,2,3,1) fig, ax = plt.subplots() frames = [[ax.imshow(inp[i])] for i in range(len(inp))] ani = animation.ArtistAnimation(fig, frames) ani break
    • @SRIVINODPALACHARLA 您的代码似乎在 Colab 上运行良好,我使用 inp = torch.rand(8,3,128,128)。究竟是什么错误/看起来有什么问题?
    • 输出只是一个带有黑色边框的白色方框。当我使用torch.rand时,它就像你说的那样工作。但是当我使用火车装载机时,它不是。我可以邮寄你的截图吗?
    猜你喜欢
    • 2020-10-13
    • 2019-11-28
    • 2021-04-08
    • 2021-10-25
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多