Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
import requests
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import PIL.Image
|
| 7 |
+
import imageio
|
| 8 |
+
import numpy
|
| 9 |
+
import skimage.transform
|
| 10 |
+
from shutil import copyfileobj
|
| 11 |
+
from skimage import img_as_ubyte
|
| 12 |
+
|
| 13 |
+
#Clonar repo si no existe
|
| 14 |
+
if not os.path.isdir('first-order-model'):
|
| 15 |
+
subprocess.run(['git', 'clone', 'https://github.com/AliaksandrSiarohin/first-order-model'])
|
| 16 |
+
|
| 17 |
+
sys.path.insert(0, 'first-order-model')
|
| 18 |
+
from demo import load_checkpoints, make_animation
|
| 19 |
+
|
| 20 |
+
#Descargar checkpoint si no existe (En este caso el de caras)
|
| 21 |
+
filename = 'vox-cpk.pth.tar'
|
| 22 |
+
if not os.path.isfile(filename):
|
| 23 |
+
response = requests.get(
|
| 24 |
+
'https://github.com/graphemecluster/first-order-model-demo/releases/download/checkpoints/' + filename,
|
| 25 |
+
stream=True
|
| 26 |
+
)
|
| 27 |
+
with open(filename, 'wb') as f:
|
| 28 |
+
copyfileobj(response.raw, f)
|
| 29 |
+
print('Checkpoint descargado')
|
| 30 |
+
|
| 31 |
+
#Detectar CPU o GPU (en nuestro caso será cpu, pero nunca se sabe)
|
| 32 |
+
import torch
|
| 33 |
+
use_cpu = not torch.cuda.is_available()
|
| 34 |
+
print(f'Usando: {"CPU" if use_cpu else "GPU"}')
|
| 35 |
+
|
| 36 |
+
generator, kp_detector = load_checkpoints(
|
| 37 |
+
config_path='first-order-model/config/vox-256.yaml',
|
| 38 |
+
checkpoint_path=filename,
|
| 39 |
+
cpu=use_cpu
|
| 40 |
+
)
|
| 41 |
+
print('Modelo cargado')
|
| 42 |
+
|
| 43 |
+
def resize(image, size=(256, 256)):
|
| 44 |
+
w, h = image.size
|
| 45 |
+
d = min(w, h)
|
| 46 |
+
r = ((w-d)//2, (h-d)//2, (w+d)//2, (h+d)//2)
|
| 47 |
+
return image.resize(size, resample=PIL.Image.LANCZOS, box=r)
|
| 48 |
+
|
| 49 |
+
def generate(source_image, driving_video):
|
| 50 |
+
img = PIL.Image.fromarray(source_image).convert("RGB")
|
| 51 |
+
img = resize(img)
|
| 52 |
+
|
| 53 |
+
reader = imageio.get_reader(driving_video, mode='I', format='FFMPEG')
|
| 54 |
+
fps = reader.get_meta_data()['fps']
|
| 55 |
+
driving_frames = [frame for frame in reader]
|
| 56 |
+
|
| 57 |
+
predictions = make_animation(
|
| 58 |
+
skimage.transform.resize(numpy.asarray(img), (256, 256)),
|
| 59 |
+
[skimage.transform.resize(f, (256, 256)) for f in driving_frames],
|
| 60 |
+
generator,
|
| 61 |
+
kp_detector,
|
| 62 |
+
relative=True,
|
| 63 |
+
adapt_movement_scale=True,
|
| 64 |
+
cpu=use_cpu
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
output_path = 'output.mp4'
|
| 68 |
+
imageio.mimsave(output_path, [img_as_ubyte(f) for f in predictions], fps=fps)
|
| 69 |
+
return output_path
|
| 70 |
+
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("First Order Motion Model")
|
| 73 |
+
gr.Markdown("Anima una foto de cara con el movimiento de un vídeo conductor.")
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column():
|
| 76 |
+
input_image = gr.Image(type="numpy", label="Imagen fuente (foto de cara)")
|
| 77 |
+
input_video = gr.Video(label="Vídeo conductor")
|
| 78 |
+
btn = gr.Button("Generar animación", variant="primary")
|
| 79 |
+
with gr.Column():
|
| 80 |
+
output_video = gr.Video(label="Resultado")
|
| 81 |
+
|
| 82 |
+
btn.click(fn=generate, inputs=[input_image, input_video], outputs=output_video)
|
| 83 |
+
|
| 84 |
+
gr.Examples(
|
| 85 |
+
examples=[
|
| 86 |
+
["LinusTechTips.jpg","kirbypro.jfif", "DameDane.mp4"]
|
| 87 |
+
],
|
| 88 |
+
inputs=[input_image, input_video],
|
| 89 |
+
label="Ejemplo"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
demo.launch()
|