File size: 8,513 Bytes
e0ac1f0
 
 
1b74ec2
e0ac1f0
 
1b74ec2
d0a9eb9
1b74ec2
e0ac1f0
 
1b74ec2
 
d0a9eb9
1b74ec2
e0ac1f0
1b74ec2
e0ac1f0
 
 
 
 
 
1b74ec2
 
 
e0ac1f0
 
 
 
1b74ec2
e0ac1f0
 
 
1b74ec2
 
 
e0ac1f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b74ec2
 
 
 
 
e0ac1f0
1b74ec2
 
 
e0ac1f0
 
 
 
1b74ec2
 
 
e0ac1f0
1b74ec2
e0ac1f0
1b74ec2
e0ac1f0
 
1b74ec2
e0ac1f0
 
 
1b74ec2
e0ac1f0
 
1b74ec2
e0ac1f0
 
 
 
724654c
 
 
 
 
e0ac1f0
1b74ec2
 
 
 
 
 
 
 
 
 
 
e0ac1f0
1b74ec2
e0ac1f0
 
1b74ec2
e0ac1f0
 
 
 
 
 
 
 
 
 
 
 
1b74ec2
e0ac1f0
 
 
 
 
 
 
 
 
 
 
1b74ec2
 
 
 
 
e0ac1f0
1b74ec2
 
 
724654c
e0ac1f0
1b74ec2
724654c
1b74ec2
 
 
 
e0ac1f0
 
 
1b74ec2
e0ac1f0
 
 
1b74ec2
 
e0ac1f0
724654c
e0ac1f0
 
 
1b74ec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0ac1f0
1b74ec2
e0ac1f0
 
 
1b74ec2
 
e0ac1f0
724654c
e0ac1f0
 
1b74ec2
 
 
 
 
e0ac1f0
 
 
 
 
 
 
 
 
 
cf02bea
1b74ec2
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# coding: utf-8

"""
The entrance of the gradio
"""

import tyro
import gradio as gr
import os.path as osp
import torch
from gradio_client import utils as gradio_client_utils
from src.utils.helper import load_description
from src.gradio_pipeline import GradioPipeline
from src.config.crop_config import CropConfig
from src.config.argument_config import ArgumentConfig
from src.config.inference_config import InferenceConfig
import cv2

try:
    import spaces
except ImportError:
    spaces = None

# import gdown
# folder_url = f"https://drive.google.com/drive/folders/1UtKgzKjFAOmZkhNK-OYT0caJ_w2XAnib"
# gdown.download_folder(url=folder_url, output="pretrained_weights", quiet=False)

def partial_fields(target_class, kwargs):
    return target_class(**{k: v for k, v in kwargs.items() if hasattr(target_class, k)})

# set tyro theme
tyro.extras.set_accent_color("bright_cyan")
args = tyro.cli(ArgumentConfig)

# specify configs for inference
inference_cfg = partial_fields(InferenceConfig, args.__dict__)  # use attribute of args to initial InferenceConfig
crop_cfg = partial_fields(CropConfig, args.__dict__)  # use attribute of args to initial CropConfig

gradio_pipeline = GradioPipeline(
    inference_cfg=inference_cfg,
    crop_cfg=crop_cfg,
    args=args
)

def maybe_gpu(fn):
    if spaces is not None and torch.cuda.is_available():
        return spaces.GPU(fn)
    return fn


_original_json_schema_to_python_type = gradio_client_utils._json_schema_to_python_type


def _patched_json_schema_to_python_type(schema, defs):
    if isinstance(schema, bool):
        return "Any" if schema else "None"
    return _original_json_schema_to_python_type(schema, defs)


gradio_client_utils._json_schema_to_python_type = _patched_json_schema_to_python_type

@maybe_gpu
def gpu_wrapped_execute_video(
    input_image_path,
    input_video_path,
    flag_relative_input,
    flag_do_crop_input,
):
    return gradio_pipeline.execute_video(
        input_image_path,
        input_video_path,
        flag_relative_input,
        flag_do_crop_input,
    )

@maybe_gpu
def gpu_wrapped_execute_image(input_eye_ratio: float, input_lip_ratio: float):
    return gradio_pipeline.execute_image(input_eye_ratio, input_lip_ratio)

def is_square_video(video_path):
    video = cv2.VideoCapture(video_path)
    
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    video.release()
    if width != height:
        raise gr.Error("Error: the video does not have a square aspect ratio. We currently only support square videos")
    
    return gr.update(visible=True)

# assets
title_md = "assets/gradio_title.md"
example_portrait_dir = "assets/examples/source"
example_video_dir = "assets/examples/driving"
data_examples = [
    [osp.join(example_portrait_dir, "s9.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True],
    [osp.join(example_portrait_dir, "s6.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True],
    [osp.join(example_portrait_dir, "s10.jpg"), osp.join(example_video_dir, "d5.mp4"), True, True],
    [osp.join(example_portrait_dir, "s5.jpg"), osp.join(example_video_dir, "d6.mp4"), True, True],
    [osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d7.mp4"), True, True],
]
#################### interface logic ####################

# Define components first
eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
retargeting_input_image = gr.Image(type="numpy")
output_image = gr.Image(type="numpy")
output_image_paste_back = gr.Image(type="numpy")
output_video = gr.Video()

with gr.Blocks(theme=gr.themes.Soft(),title="LivePortrait: Revolutionary AI-Powered Portrait Animation Technology") as demo:
    gr.HTML(load_description(title_md))
    gr.Markdown(load_description("assets/gradio_description_upload.md"))
    with gr.Row():
        with gr.Accordion(open=True, label="Source Portrait"):
            image_input = gr.Image(type="numpy")
            gr.Examples(
                examples=[
                    [osp.join(example_portrait_dir, "s9.jpg")],
                    [osp.join(example_portrait_dir, "s6.jpg")],
                    [osp.join(example_portrait_dir, "s10.jpg")],
                    [osp.join(example_portrait_dir, "s5.jpg")],
                    [osp.join(example_portrait_dir, "s7.jpg")],
                ],
                inputs=[image_input],
                cache_examples=False,
            )
        with gr.Accordion(open=True, label="Driving Video"):
            video_input = gr.Video()
            gr.Examples(
                examples=[
                    [osp.join(example_video_dir, "d0.mp4")],
                    [osp.join(example_video_dir, "d5.mp4")],
                    [osp.join(example_video_dir, "d6.mp4")],
                    [osp.join(example_video_dir, "d7.mp4")],
                ],
                inputs=[video_input],
                cache_examples=False,
            )
    with gr.Row():
        with gr.Accordion(open=False, label="Animation Instructions and Options"):
            gr.Markdown(load_description("assets/gradio_description_animation.md"))
            with gr.Row():
                flag_relative_input = gr.Checkbox(value=True, label="relative motion")
                flag_do_crop_input = gr.Checkbox(value=True, label="do crop")
    with gr.Row():
        with gr.Column():
            process_button_animation = gr.Button("🚀 Animate", variant="primary")
        with gr.Column():
            process_button_reset = gr.ClearButton([image_input, video_input, output_video], value="🧹 Clear")
    with gr.Row():
        with gr.Column():
            with gr.Accordion(open=True, label="Avatar animation video"):
                output_video.render()
    with gr.Row():
        # Examples
        gr.Markdown("## You could choose the examples below ⬇️")
    with gr.Row():
        gr.Examples(
            examples=data_examples,
            fn=gpu_wrapped_execute_video,
            inputs=[
                image_input,
                video_input,
                flag_relative_input,
                flag_do_crop_input,
            ],
            outputs=[output_video],
            examples_per_page=5,
            cache_examples=False,
        )
    gr.Markdown(load_description("assets/gradio_description_retargeting.md"), visible=False)
    with gr.Row(visible=False):
        eye_retargeting_slider.render()
        lip_retargeting_slider.render()
    with gr.Row(visible=False):
        process_button_retargeting = gr.Button("🚗 Retargeting", variant="primary")
        process_button_reset_retargeting = gr.ClearButton(
            [
                eye_retargeting_slider,
                lip_retargeting_slider,
                retargeting_input_image,
                output_image,
                output_image_paste_back
            ],
            value="🧹 Clear"
        )
    with gr.Row(visible=False):
        with gr.Column():
            with gr.Accordion(open=True, label="Retargeting Input"):
                retargeting_input_image.render()
        with gr.Column():
            with gr.Accordion(open=True, label="Retargeting Result"):
                output_image.render()
        with gr.Column():
            with gr.Accordion(open=True, label="Paste-back Result"):
                output_image_paste_back.render()
    # binding functions for buttons
    process_button_retargeting.click(
        # fn=gradio_pipeline.execute_image,
        fn=gpu_wrapped_execute_image,
        inputs=[eye_retargeting_slider, lip_retargeting_slider],
        outputs=[output_image, output_image_paste_back],
        show_progress=True  
    )
    process_button_animation.click(
        fn=gpu_wrapped_execute_video,
        inputs=[
            image_input,
            video_input,
            flag_relative_input,
            flag_do_crop_input,
        ],
        outputs=[output_video],
        show_progress=True
    )
    image_input.change(
        fn=gradio_pipeline.prepare_retargeting,
        inputs=image_input,
        outputs=[eye_retargeting_slider, lip_retargeting_slider, retargeting_input_image]
    )
    video_input.upload(
        fn=is_square_video,
        inputs=video_input,
        outputs=video_input
    )

demo.launch(
    server_port=args.server_port,
    share=args.share,
    server_name=args.server_name,
    show_api=True
)