问题描述
我正在尝试使用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())
如果可能的话,我很乐意接受其他我已经尝试了数小时却失败的事情的帮助:
解决方法
要显示单个视频,您必须从列表中删除图像-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())