plt如何将单个视频文件显示为html?

我无法仅显示 html 格式的一张图像。此代码片段并排显示两个媒体,一个是照片,另一个是驾驶视频,我只想查看驾驶视频

import imageio

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

from skimage.transform import resize

from IPython.display import HTML

import warnings

warnings.filterwarnings("ignore")


source_image = imageio.imread('/content/gdrive/My Drive/first-order-motion-model/stat.png')

driving_video = imageio.mimread('/content/gdrive/My Drive/first-order-motion-model/obama.mp4')



#Resize image and video to 256x256


source_image = resize(source_image, (256, 256))[..., :3]

driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]


def display(source, driving, generated=None):

    fig = plt.figure(figsize=(8 + 4 * (generated is not None), 6))


    ims = []

    for i in range(len(driving)):

        cols = [source]

        cols.append(driving[i])

        if generated is not None:

            cols.append(generated[i])

        im = plt.imshow(np.concatenate(cols, axis=1), animated=True)

        plt.axis('off')

        ims.append([im])


    ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000)

    plt.close()

    return ani


HTML(display(source_image, driving_video).to_html5_video())

如果可能的话,我很乐意接受对我尝试了几个小时但失败的另一件事的帮助:


我想将 25 张图像放入一个文件夹中,并使用相同的驾驶视频对所有图像进行深度伪造,然后在一个视频中将它们显示在 5x5 网格中。


翻翻过去那场雪
浏览 115回答 1
1回答

一只斗牛犬

要显示单个视频,您必须从列表中删除图像 -cols = []而不是cols = [source]我无法测试它,但它的工作原理是这样的使用视频文件名创建 5x5 列表读取每个视频并转换为帧列表使用索引来连接行中的帧并将行连接到单个图像。通过这种方式,它会创建显示为动画的图像列表。all_filenames = [    # row 1    [        '/content/gdrive/My Drive/first-order-motion-model/video1.mp4',        '/content/gdrive/My Drive/first-order-motion-model/video2.mp4',        '/content/gdrive/My Drive/first-order-motion-model/video3.mp4',        '/content/gdrive/My Drive/first-order-motion-model/video4.mp4',        '/content/gdrive/My Drive/first-order-motion-model/video5.mp4',    ],    # row 2    [        '/content/gdrive/My Drive/first-order-motion-model/other1.mp4',        '/content/gdrive/My Drive/first-order-motion-model/other2.mp4',        '/content/gdrive/My Drive/first-order-motion-model/other3.mp4',        '/content/gdrive/My Drive/first-order-motion-model/other4.mp4',        '/content/gdrive/My Drive/first-order-motion-model/other5.mp4',    ],    # row 3    # etc.]# --- load all videos and convert every video to list of frames ---all_videos = []for row in all_filenames:    row_videos = []    for filename in row:        # read video        video = imageio.mimread(filename)        # convert to list of frames        frames = [resize(frame, (256, 256))[..., :3] for frame in video]        # keep it in row        row_videos.append(frames)    # keep row in `all_videos`    all_videos.append(row_videos)    # --- concatenate list 5x5 to images ---def display(all_videos):    fig = plt.figure(figsize=(4*5, 4*5))    all_images = []        for i in range(len(all_videos[0][0])):  # use len of first video in first row  but it would rather use `min()` for all videos        col_images = []         for row in all_videos:            row_images = []            for video in row:                row_images.append(video[i])            # concatenate every row            row_img = np.concatenate(row_images, axis=1)            col_images.append(row_img)        # concatenate rows to single images        col_img = np.concatenate(col_images, axis=0)        img = plt.imshow(col_img, animated=True)        plt.axis('off')        all_images.append([img])    ani = animation.ArtistAnimation(fig, all_images, interval=50, repeat_delay=1000)    plt.close()    return aniHTML(display(all_videos).to_html5_video())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python