Kayra Uckilinc
Kayra Uckilinc

Reputation: 111

How can plt show a single video file to html?

I am trying to use this google colab deep fake tutorial but I fail to show only one image in html format. This code snippet shows two media side by side one a photo and the other is driving video i want to see only driving video

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())

if possible I can gladly accept help for the other thing I've tried for hours and failed:

Upvotes: 0

Views: 607

Answers (1)

furas
furas

Reputation: 142711

To display single video you have to remove image from list - cols = [] instead of cols = [source]


I can't test it but it works like this

  • create list 5x5 with video filenames
  • read every video and convert to list of frames
  • use index to concatenate frames in rows and to concatenate rows to single image.

This way it creates list of images which it displays as animation.

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())

Upvotes: 1

Related Questions