-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
163 lines (137 loc) · 6.31 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import gradio as gr
import os
import subprocess
import shutil
import torch
from PIL import Image
import argparse
import numpy as np
from pathlib import Path
from attrdict import AttrDict
from demo import run_generator
title = "Thin-Plate Spline Motion Model for Image Animation"
def get_style_image_path(style_name: str) -> str:
base_path = 'assets'
filenames = {
'source': 'source.png',
'driving': 'driving.mp4',
}
return f'{base_path}/{filenames[style_name]}'
def get_style_image_markdown_text(style_name: str) -> str:
url = get_style_image_path(style_name)
return f'<img id="style-image" src="{url}" alt="style image">'
def update_style_image(style_name: str) -> dict:
text = get_style_image_markdown_text(style_name)
return gr.Markdown.update(value=text)
def set_example_image(example: list) -> dict:
return gr.Image.update(value=example[0])
def set_example_video(example: list) -> dict:
return gr.Video.update(value=example[0])
def inference(image_source, input_image, input_webcam, vid, use_cuda):
opt = AttrDict()
try:
os.makedirs("temp", exist_ok=True)
img = input_image if image_source=="upload" else input_webcam
# img.save(f"{Path('temp/image.jpg')}", "JPEG")
opt.config = str(Path('config/vox-256.yaml'))
opt.checkpoint = str(Path('./checkpoints/vox.pth.tar'))
opt.source_image = np.asarray(img)
opt.driving_video = str(Path(vid))
opt.result_video = str(Path('./temp/result.mp4'))
opt.cpu = False if torch.cuda.is_available() and use_cuda else True
# Default values
opt.img_shape = [256,256]
opt.mode = 'relative'
opt.find_best_frame = False
run_generator(opt)
print("Done.")
return str(Path('./temp/result.mp4')), ""
except Exception as e:
print(e)
return None, str(e)
def main():
with gr.Blocks(css='style.css') as demo:
gr.Markdown(title)
with gr.Box():
gr.Markdown('''## Step 1 (Provide Input Face Image)
- Drop an image containing a face to the **Input Image**.
- If there are multiple faces in the image, use Edit button in the upper right corner and crop the input image beforehand.
''')
with gr.Row():
with gr.Column():
with gr.Row():
use_cuda = gr.Radio(["Yes", "No"], label="Use GPU (if available)", type="value", value="No")
image_source = gr.Radio(["upload", "webcam"], label="Choose image source", type="value", value="upload", interactive=True)
with gr.Row():
with gr.Column():
with gr.Row():
input_image = gr.Image(label='Input Image',
type="pil", interactive=True, source="upload")
with gr.Row():
with gr.Column():
with gr.Row():
input_webcam = gr.Image(label='Input webcam image', source='webcam',
type="pil", visible=True)
# def toggle(value):
# # if value == "upload":
# # input_webcam.update(visible=False)
# # input_image.update(visible=True)
# # else:
# # input_image.update(visible=False)
# input_webcam.update(visible=True)
# return input_webcam
# image_source.change(fn=toggle, inputs=image_source, outputs=input_webcam)
with gr.Row():
paths = sorted(Path('assets').glob('*.png'))
example_images = gr.Dataset(components=[input_image],
samples=[[path.as_posix()]
for path in paths])
with gr.Box():
gr.Markdown('''## Step 2 (Select Driving Video)
- Select **Style Driving Video for the face image animation**.
''')
with gr.Row():
with gr.Column():
with gr.Row():
video_source = gr.Radio(["upload", "webcam"], label="Choose video source", type="value", value="upload", interactive=True)
driving_video = gr.Video(label='Driving Video',
format="mp4", interactive=True, source="upload")
video_source.change(fn=lambda value: driving_video.update(source=value), inputs=video_source, outputs=driving_video)
with gr.Row():
paths = sorted(Path('assets').glob('*.mp4'))
example_video = gr.Dataset(components=[driving_video],
samples=[[path.as_posix()]
for path in paths])
with gr.Box():
gr.Markdown('''## Step 3 (Generate Animated Image based on the Video)
- Hit the **Generate** button. (Note: It will try to find GPU, if it's available and `Use GPU` is selected.)
''')
with gr.Row():
with gr.Column():
with gr.Row():
generate_button = gr.Button('Generate')
with gr.Column():
result = gr.Video(type="file", label="Output")
logs = gr.Textbox(label="Error (if any):")
generate_button.click(fn=inference,
inputs=[
image_source,
input_image,
input_webcam,
driving_video,
use_cuda
],
outputs=[result, logs])
example_images.click(fn=set_example_image,
inputs=example_images,
outputs=example_images.components)
example_video.click(fn=set_example_video,
inputs=example_video,
outputs=example_video.components)
demo.launch(
enable_queue=True,
debug=True,
share=True
)
if __name__ == '__main__':
main()