微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试使用this谷歌合作实验室深度伪造教程,但我无法仅显示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,: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张图像放入一个文件夹中,并用相同的驾驶视频 deepfake 全部将它们显示一个视频中,并以5x5网格显示它们。

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。