teganmosi commited on
Commit
a125901
Β·
verified Β·
1 Parent(s): 3f6d3d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pathlib
3
+ import spaces # Added for ZeroGPU
4
+ import gradio as gr
5
+ import torch
6
+ from PIL import Image
7
+
8
+ repo_dir = pathlib.Path("Thin-Plate-Spline-Motion-Model").absolute()
9
+ if not repo_dir.exists():
10
+ os.system("git clone https://github.com/yoyo-nb/Thin-Plate-Spline-Motion-Model")
11
+ os.chdir(repo_dir.name)
12
+ if not (repo_dir / "checkpoints").exists():
13
+ os.system("mkdir checkpoints")
14
+ if not (repo_dir / "checkpoints/vox.pth.tar").exists():
15
+ os.system("gdown 1-CKOjv_y_TzNe-dwQsjjeVxJUuyBAb5X -O checkpoints/vox.pth.tar")
16
+
17
+ title = "#✨ MotionMagicAI"
18
+ DESCRIPTION = '''### πŸŽ₯ <b>MotionMagicAI</b> Brings Images to Life! πŸš€ Powered by Thin-Plate Spline Motion Model (CVPR 2022). Upload a face, add a video, and watch it dance or sing! πŸ•ΊπŸ’ƒ <a href='https://arxiv.org/abs/2203.14367'>[Paper]</a> <a href='https://github.com/yoyo-nb/Thin-Plate-Spline-Motion-Model'>[Code]</a>
19
+ <img id="overview" alt="overview" src="https://github.com/yoyo-nb/Thin-Plate-Spline-Motion-Model/raw/main/assets/vox.gif" />
20
+ '''
21
+ FOOTER = '<img id="visitor-badge" alt="visitor badge" src="https://visitor-badge.glitch.me/badge?page_id=gradio-blocks.Image-Animation-using-Thin-Plate-Spline-Motion-Model" />'
22
+
23
+ def get_style_image_path(style_name: str) -> str:
24
+ base_path = 'assets'
25
+ filenames = {
26
+ 'source': 'source.png',
27
+ 'driving': 'driving.mp4',
28
+ }
29
+ return f'{base_path}/{filenames[style_name]}'
30
+
31
+ def get_style_image_markdown_text(style_name: str) -> str:
32
+ url = get_style_image_path(style_name)
33
+ return f'<img id="style-image" src="{url}" alt="style image">'
34
+
35
+ def update_style_image(style_name: str) -> dict:
36
+ text = get_style_image_markdown_text(style_name)
37
+ return gr.Markdown.update(value=text)
38
+
39
+ @spaces.GPU(duration=120) # Added for ZeroGPU, set duration for long inference
40
+ def inference(img, vid):
41
+ if not os.path.exists('temp'):
42
+ os.system('mkdir temp')
43
+
44
+ img.save("temp/image.jpg", "JPEG")
45
+ if torch.cuda.is_available():
46
+ os.system(f"python demo.py --config config/vox-256.yaml --checkpoint ./checkpoints/vox.pth.tar --source_image 'temp/image.jpg' --driving_video {vid} --result_video './temp/result.mp4'")
47
+ else:
48
+ os.system(f"python demo.py --config config/vox-256.yaml --checkpoint ./checkpoints/vox.pth.tar --source_image 'temp/image.jpg' --driving_video {vid} --result_video './temp/result.mp4' --cpu")
49
+ return './temp/result.mp4'
50
+
51
+ def main():
52
+ with gr.Blocks(css='style.css') as demo:
53
+ gr.Markdown(title)
54
+ gr.Markdown(DESCRIPTION)
55
+
56
+ with gr.Box():
57
+ gr.Markdown('''## Step 1 (Provide Input Face Image)
58
+ - Drop an image containing a face to the **Input Image**.
59
+ - If there are multiple faces in the image, use Edit button in the upper right corner and crop the input image beforehand.
60
+ ''')
61
+ with gr.Row():
62
+ with gr.Column():
63
+ with gr.Row():
64
+ input_image = gr.Image(label='Input Image',
65
+ type="pil")
66
+
67
+ with gr.Row():
68
+ paths = sorted(pathlib.Path('assets').glob('*.png'))
69
+ gr.Examples(inputs=[input_image],
70
+ examples=[[path.as_posix()] for path in paths])
71
+
72
+ with gr.Box():
73
+ gr.Markdown('''## Step 2 (Select Driving Video)
74
+ - Select **Style Driving Video for the face image animation**.
75
+ ''')
76
+ with gr.Row():
77
+ with gr.Column():
78
+ with gr.Row():
79
+ driving_video = gr.Video(label='Driving Video',
80
+ format="mp4")
81
+
82
+ with gr.Row():
83
+ paths = sorted(pathlib.Path('assets').glob('*.mp4'))
84
+ gr.Examples(inputs=[driving_video],
85
+ examples=[[path.as_posix()] for path in paths])
86
+
87
+ with gr.Box():
88
+ gr.Markdown('''## Step 3 (Generate Animated Image based on the Video)
89
+ - Hit the **Generate** button. (Note: On cpu-basic, it takes ~ 10 minutes to generate final results.)
90
+ ''')
91
+ with gr.Row():
92
+ with gr.Column():
93
+ with gr.Row():
94
+ generate_button = gr.Button('Generate')
95
+
96
+ with gr.Column():
97
+ result = gr.Video(label="Output")
98
+ gr.Markdown(FOOTER)
99
+ generate_button.click(fn=inference,
100
+ inputs=[
101
+ input_image,
102
+ driving_video
103
+ ],
104
+ outputs=result)
105
+
106
+ demo.queue(max_size=10).launch()
107
+
108
+ if __name__ == '__main__':
109
+ main()