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p
|
10 |
+
from src.utils.helper import load_description
|
11 |
+
from src.gradio_pipeline import GradioPipeline
|
12 |
+
from src.config.crop_config import CropConfig
|
13 |
+
from src.config.argument_config import ArgumentConfig
|
14 |
+
from src.config.inference_config import InferenceConfig
|
15 |
+
import spaces
|
16 |
+
import cv2
|
17 |
+
|
18 |
+
# import gdown
|
19 |
+
# folder_url = f"https://drive.google.com/drive/folders/1UtKgzKjFAOmZkhNK-OYT0caJ_w2XAnib"
|
20 |
+
# gdown.download_folder(url=folder_url, output="pretrained_weights", quiet=False)
|
21 |
+
|
22 |
+
|
23 |
+
# import sys
|
24 |
+
# sys.path.append('/home/user/.local/lib/python3.10/site-packages')
|
25 |
+
# sys.path.append('/home/user/.local/lib/python3.10/site-packages/stf_alternative/src/stf_alternative')
|
26 |
+
# sys.path.append('/home/user/.local/lib/python3.10/site-packages/stf_tools/src/stf_tools')
|
27 |
+
# sys.path.append('/home/user/app/')
|
28 |
+
# sys.path.append('/home/user/app/stf/')
|
29 |
+
# sys.path.append('/home/user/app/stf/stf_alternative/')
|
30 |
+
# sys.path.append('/home/user/app/stf/stf_alternative/src/stf_alternative')
|
31 |
+
# sys.path.append('/home/user/app/stf/stf_tools')
|
32 |
+
# sys.path.append('/home/user/app/stf/stf_tools/src/stf_tools')
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
# # CUDA 경로를 환경 변수로 설정
|
37 |
+
# os.environ['PATH'] = '/usr/local/cuda/bin:' + os.environ.get('PATH', '')
|
38 |
+
# os.environ['LD_LIBRARY_PATH'] = '/usr/local/cuda/lib64:' + os.environ.get('LD_LIBRARY_PATH', '')
|
39 |
+
# # 확인용 출력
|
40 |
+
# print("PATH:", os.environ['PATH'])
|
41 |
+
# print("LD_LIBRARY_PATH:", os.environ['LD_LIBRARY_PATH'])
|
42 |
+
|
43 |
+
# from stf_utils import STFPipeline
|
44 |
+
|
45 |
+
|
46 |
+
def partial_fields(target_class, kwargs):
|
47 |
+
return target_class(**{k: v for k, v in kwargs.items() if hasattr(target_class, k)})
|
48 |
+
|
49 |
+
# set tyro theme
|
50 |
+
tyro.extras.set_accent_color("bright_cyan")
|
51 |
+
args = tyro.cli(ArgumentConfig)
|
52 |
+
|
53 |
+
# specify configs for inference
|
54 |
+
inference_cfg = partial_fields(InferenceConfig, args.__dict__) # use attribute of args to initial InferenceConfig
|
55 |
+
crop_cfg = partial_fields(CropConfig, args.__dict__) # use attribute of args to initial CropConfig
|
56 |
+
|
57 |
+
gradio_pipeline = GradioPipeline(
|
58 |
+
inference_cfg=inference_cfg,
|
59 |
+
crop_cfg=crop_cfg,
|
60 |
+
args=args
|
61 |
+
)
|
62 |
+
|
63 |
+
@spaces.GPU(duration=240)
|
64 |
+
def gpu_wrapped_execute_video(*args, **kwargs):
|
65 |
+
return gradio_pipeline.execute_video(*args, **kwargs)
|
66 |
+
|
67 |
+
@spaces.GPU(duration=240)
|
68 |
+
def gpu_wrapped_execute_image(*args, **kwargs):
|
69 |
+
return gradio_pipeline.execute_image(*args, **kwargs)
|
70 |
+
|
71 |
+
def is_square_video(video_path):
|
72 |
+
video = cv2.VideoCapture(video_path)
|
73 |
+
|
74 |
+
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
75 |
+
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
76 |
+
|
77 |
+
video.release()
|
78 |
+
if width != height:
|
79 |
+
raise gr.Error("Error: the video does not have a square aspect ratio. We currently only support square videos")
|
80 |
+
|
81 |
+
return gr.update(visible=True)
|
82 |
+
|
83 |
+
# assets
|
84 |
+
title_md = "assets/gradio_title.md"
|
85 |
+
example_portrait_dir = "assets/examples/source"
|
86 |
+
example_video_dir = "assets/examples/driving"
|
87 |
+
data_examples = [
|
88 |
+
[osp.join(example_portrait_dir, "s9.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
|
89 |
+
[osp.join(example_portrait_dir, "s6.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
|
90 |
+
[osp.join(example_portrait_dir, "s10.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
|
91 |
+
[osp.join(example_portrait_dir, "s5.jpg"), osp.join(example_video_dir, "d18.mp4"), True, True, True, True],
|
92 |
+
[osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d19.mp4"), True, True, True, True],
|
93 |
+
[osp.join(example_portrait_dir, "s22.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
|
94 |
+
]
|
95 |
+
#################### interface logic ####################
|
96 |
+
|
97 |
+
# Define components first
|
98 |
+
eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
|
99 |
+
lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
|
100 |
+
retargeting_input_image = gr.Image(type="filepath")
|
101 |
+
output_image = gr.Image(type="numpy")
|
102 |
+
output_image_paste_back = gr.Image(type="numpy")
|
103 |
+
output_video = gr.Video()
|
104 |
+
output_video_concat = gr.Video()
|
105 |
+
|
106 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
107 |
+
#gr.HTML(load_description(title_md))
|
108 |
+
|
109 |
+
with gr.Tabs():
|
110 |
+
with gr.Tab("Text to LipSync"):
|
111 |
+
gr.Markdown("# Text to LipSync")
|
112 |
+
with gr.Row():
|
113 |
+
script_txt = gr.Text()
|
114 |
+
|
115 |
+
gr.Markdown(load_description("assets/gradio_description_upload.md"))
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Accordion(open=True, label="Source Portrait"):
|
118 |
+
image_input = gr.Image(type="filepath")
|
119 |
+
gr.Examples(
|
120 |
+
examples=[
|
121 |
+
[osp.join(example_portrait_dir, "s9.jpg")],
|
122 |
+
[osp.join(example_portrait_dir, "s6.jpg")],
|
123 |
+
[osp.join(example_portrait_dir, "s10.jpg")],
|
124 |
+
[osp.join(example_portrait_dir, "s5.jpg")],
|
125 |
+
[osp.join(example_portrait_dir, "s7.jpg")],
|
126 |
+
[osp.join(example_portrait_dir, "s12.jpg")],
|
127 |
+
[osp.join(example_portrait_dir, "s22.jpg")],
|
128 |
+
],
|
129 |
+
inputs=[image_input],
|
130 |
+
cache_examples=False,
|
131 |
+
)
|
132 |
+
with gr.Accordion(open=True, label="Driving Video"):
|
133 |
+
video_input = gr.Video()
|
134 |
+
gr.Examples(
|
135 |
+
examples=[
|
136 |
+
[osp.join(example_video_dir, "d0.mp4")],
|
137 |
+
[osp.join(example_video_dir, "d18.mp4")],
|
138 |
+
[osp.join(example_video_dir, "d19.mp4")],
|
139 |
+
[osp.join(example_video_dir, "d14_trim.mp4")],
|
140 |
+
[osp.join(example_video_dir, "d6_trim.mp4")],
|
141 |
+
],
|
142 |
+
inputs=[video_input],
|
143 |
+
cache_examples=False,
|
144 |
+
)
|
145 |
+
with gr.Row():
|
146 |
+
with gr.Accordion(open=False, label="Animation Instructions and Options"):
|
147 |
+
gr.Markdown(load_description("assets/gradio_description_animation.md"))
|
148 |
+
with gr.Row():
|
149 |
+
flag_relative_input = gr.Checkbox(value=True, label="relative motion")
|
150 |
+
flag_do_crop_input = gr.Checkbox(value=True, label="do crop")
|
151 |
+
flag_remap_input = gr.Checkbox(value=True, label="paste-back")
|
152 |
+
gr.Markdown(load_description("assets/gradio_description_animate_clear.md"))
|
153 |
+
with gr.Row():
|
154 |
+
with gr.Column():
|
155 |
+
process_button_animation = gr.Button("🚀 Animate", variant="primary")
|
156 |
+
with gr.Column():
|
157 |
+
process_button_reset = gr.ClearButton([image_input, video_input, output_video, output_video_concat], value="🧹 Clear")
|
158 |
+
with gr.Row():
|
159 |
+
with gr.Column():
|
160 |
+
with gr.Accordion(open=True, label="The animated video in the original image space"):
|
161 |
+
output_video.render()
|
162 |
+
with gr.Column():
|
163 |
+
with gr.Accordion(open=True, label="The animated video"):
|
164 |
+
output_video_concat.render()
|
165 |
+
with gr.Row():
|
166 |
+
# Examples
|
167 |
+
gr.Markdown("## You could also choose the examples below by one click ⬇️")
|
168 |
+
with gr.Row():
|
169 |
+
gr.Examples(
|
170 |
+
examples=data_examples,
|
171 |
+
fn=gpu_wrapped_execute_video,
|
172 |
+
inputs=[
|
173 |
+
image_input,
|
174 |
+
video_input,
|
175 |
+
flag_relative_input,
|
176 |
+
flag_do_crop_input,
|
177 |
+
flag_remap_input
|
178 |
+
],
|
179 |
+
outputs=[output_image, output_image_paste_back],
|
180 |
+
examples_per_page=6,
|
181 |
+
cache_examples=False,
|
182 |
+
)
|
183 |
+
|
184 |
+
process_button_animation.click(
|
185 |
+
fn=gpu_wrapped_execute_video,
|
186 |
+
inputs=[
|
187 |
+
image_input,
|
188 |
+
video_input,
|
189 |
+
flag_relative_input,
|
190 |
+
flag_do_crop_input,
|
191 |
+
flag_remap_input
|
192 |
+
],
|
193 |
+
outputs=[output_video, output_video_concat],
|
194 |
+
show_progress=True
|
195 |
+
)
|
196 |
+
# image_input.change(
|
197 |
+
# fn=gradio_pipeline.prepare_retargeting,
|
198 |
+
# inputs=image_input,
|
199 |
+
# outputs=[eye_retargeting_slider, lip_retargeting_slider, retargeting_input_image]
|
200 |
+
# )
|
201 |
+
video_input.upload(
|
202 |
+
fn=is_square_video,
|
203 |
+
inputs=video_input,
|
204 |
+
outputs=video_input
|
205 |
+
)
|
206 |
+
|
207 |
+
demo.launch(
|
208 |
+
server_port=args.server_port,
|
209 |
+
share=args.share,
|
210 |
+
server_name=args.server_name
|
211 |
+
)
|