<gh_stars>0
# -*- coding: utf-8 -*-
"""Copy of first-order-model-demo.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1cKqGPvdQF1n-b7LJe3xM1JtvKImFbUWt

# Demo for paper "First Order Motion Model for Image Animation"

**Clone repository**
"""

from os import system

import tkinter as tk

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

main = tk.Tk()

def convert():
    
    image_file = tk.filedialog.askopenfilename()

    convert_button.destroy()

    source_image = imageio.imread(image_file)
    driving_video = imageio.mimread('files/04.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())

    """**Create a model and load checkpoints**"""

    from demo import load_checkpoints
    generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml', 
                                checkpoint_path='files/vox-cpk.pth.tar')

    """**Perform image animation**"""

    from demo import make_animation
    from skimage import img_as_ubyte

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True)

    #save resulting video
    imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions])
    #video can be downloaded from /content folder

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

    """**In the cell above we use relative keypoint displacement to animate the objects. We can use absolute coordinates instead,  but in this way all the object proporions will be inherited from the driving video. For example Putin haircut will be extended to match Trump haircut.**"""

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=False, adapt_movement_scale=True)
    HTML(display(source_image, driving_video, predictions).to_html5_video())

    """## Running on your data

    **First we need to crop a face from both source image and video, while simple graphic editor like paint can be used for cropping from image. Cropping from video is more complicated. You can use ffpmeg for this.**
    """


    """**Another posibility is to use some screen recording tool, or if you need to crop many images at ones use face detector(https://github.com/1adrianb/face-alignment) , see https://github.com/AliaksandrSiarohin/video-preprocessing for preprcessing of VoxCeleb.**"""

    source_image = imageio.imread('files/09.png')
    driving_video = imageio.mimread('hinton.mp4', memtest=False)


    #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]

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True,
                                adapt_movement_scale=True)

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


convert_button = tk.Button(text = "Select image file", command = convert)
convert_button.pack()

main.mainloop()
# -*- coding: utf-8 -*-
"""Copy of first-order-model-demo.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1cKqGPvdQF1n-b7LJe3xM1JtvKImFbUWt

# Demo for paper "First Order Motion Model for Image Animation"

**Clone repository**
"""

from os import system

import tkinter as tk

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

main = tk.Tk()

def convert():
    
    image_file = tk.filedialog.askopenfilename()

    convert_button.destroy()

    source_image = imageio.imread(image_file)
    driving_video = imageio.mimread('files/04.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())

    """**Create a model and load checkpoints**"""

    from demo import load_checkpoints
    generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml', 
                                checkpoint_path='files/vox-cpk.pth.tar')

    """**Perform image animation**"""

    from demo import make_animation
    from skimage import img_as_ubyte

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True)

    #save resulting video
    imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions])
    #video can be downloaded from /content folder

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

    """**In the cell above we use relative keypoint displacement to animate the objects. We can use absolute coordinates instead,  but in this way all the object proporions will be inherited from the driving video. For example Putin haircut will be extended to match Trump haircut.**"""

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=False, adapt_movement_scale=True)
    HTML(display(source_image, driving_video, predictions).to_html5_video())

    """## Running on your data

    **First we need to crop a face from both source image and video, while simple graphic editor like paint can be used for cropping from image. Cropping from video is more complicated. You can use ffpmeg for this.**
    """


    """**Another posibility is to use some screen recording tool, or if you need to crop many images at ones use face detector(https://github.com/1adrianb/face-alignment) , see https://github.com/AliaksandrSiarohin/video-preprocessing for preprcessing of VoxCeleb.**"""

    source_image = imageio.imread('files/09.png')
    driving_video = imageio.mimread('hinton.mp4', memtest=False)


    #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]

    predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True,
                                adapt_movement_scale=True)

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


convert_button = tk.Button(text = "Select image file", command = convert)
convert_button.pack()

main.mainloop()
# -*- coding: utf-8 -*- Copy of first-order-model-demo.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1cKqGPvdQF1n-b7LJe3xM1JtvKImFbUWt  # Demo for paper "First Order Motion Model for Image Animation"  **Clone repository** #Resize image and video to 256x256 **Create a model and load checkpoints** **Perform image animation** #save resulting video #video can be downloaded from /content folder **In the cell above we use relative keypoint displacement to animate the objects. We can use absolute coordinates instead,  but in this way all the object proporions will be inherited from the driving video. For example Putin haircut will be extended to match Trump haircut.** ## Running on your data      **First we need to crop a face from both source image and video, while simple graphic editor like paint can be used for cropping from image. Cropping from video is more complicated. You can use ffpmeg for this.** **Another posibility is to use some screen recording tool, or if you need to crop many images at ones use face detector(https://github.com/1adrianb/face-alignment) , see https://github.com/AliaksandrSiarohin/video-preprocessing for preprcessing of VoxCeleb.** #Resize image and video to 256x256