为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

翻翻过去那场雪 2023-08-15 16:30:17
我无法仅显示 html 格式的一张图像。此代码片段并排显示两个媒体,一个是照片,另一个是驾驶视频,我只想查看驾驶视频import imageioimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationfrom skimage.transform import resizefrom IPython.display import HTMLimport warningswarnings.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 256x256source_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 aniHTML(display(source_image, driving_video).to_html5_video())如果可能的话,我很乐意接受对我尝试了几个小时但失败的另一件事的帮助:我想将 25 张图像放入一个文件夹中,并使用相同的驾驶视频对所有图像进行深度伪造,然后在一个视频中将它们显示在 5x5 网格中。
查看完整描述

1 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

要显示单个视频,您必须从列表中删除图像 -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 ani


HTML(display(all_videos).to_html5_video())


查看完整回答
反对 回复 2023-08-15
  • 1 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信