marcelocardosof commited on
Commit
2611f90
·
verified ·
1 Parent(s): 911b193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -13,7 +13,6 @@ 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"
@@ -27,8 +26,8 @@ tyro.extras.set_accent_color("bright_cyan")
27
  args = tyro.cli(ArgumentConfig)
28
 
29
  # specify configs for inference
30
- inference_cfg = partial_fields(InferenceConfig, args.__dict__) # use attribute of args to initial InferenceConfig
31
- crop_cfg = partial_fields(CropConfig, args.__dict__) # use attribute of args to initial CropConfig
32
 
33
  gradio_pipeline = GradioPipeline(
34
  inference_cfg=inference_cfg,
@@ -44,17 +43,10 @@ def gpu_wrapped_execute_video(*args, **kwargs):
44
  def gpu_wrapped_execute_image(*args, **kwargs):
45
  return gradio_pipeline.execute_image(*args, **kwargs)
46
 
47
- def is_square_video(video_path):
48
- video = cv2.VideoCapture(video_path)
49
-
50
- width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
51
- height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
52
-
53
- video.release()
54
- if width != height:
55
- raise gr.Error("Error: the video does not have a square aspect ratio. We currently only support square videos")
56
-
57
- return gr.update(visible=True)
58
 
59
  # assets
60
  title_md = "assets/gradio_title.md"
@@ -68,9 +60,9 @@ data_examples = [
68
  [osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d19.mp4"), True, True, True, True],
69
  [osp.join(example_portrait_dir, "s22.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
70
  ]
 
71
  #################### interface logic ####################
72
 
73
- # Define components first
74
  eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
75
  lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
76
  retargeting_input_image = gr.Image(type="filepath")
@@ -82,6 +74,7 @@ output_video_concat = gr.Video()
82
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
83
  gr.HTML(load_description(title_md))
84
  gr.Markdown(load_description("assets/gradio_description_upload.md"))
 
85
  with gr.Row():
86
  with gr.Accordion(open=True, label="Source Portrait"):
87
  image_input = gr.Image(type="filepath")
@@ -98,6 +91,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
98
  inputs=[image_input],
99
  cache_examples=False,
100
  )
 
101
  with gr.Accordion(open=True, label="Driving Video"):
102
  video_input = gr.Video()
103
  gr.Examples(
@@ -111,6 +105,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
111
  inputs=[video_input],
112
  cache_examples=False,
113
  )
 
114
  with gr.Row():
115
  with gr.Accordion(open=False, label="Animation Instructions and Options"):
116
  gr.Markdown(load_description("assets/gradio_description_animation.md"))
@@ -118,12 +113,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
118
  flag_relative_input = gr.Checkbox(value=True, label="relative motion")
119
  flag_do_crop_input = gr.Checkbox(value=True, label="do crop")
120
  flag_remap_input = gr.Checkbox(value=True, label="paste-back")
 
121
  gr.Markdown(load_description("assets/gradio_description_animate_clear.md"))
 
122
  with gr.Row():
123
  with gr.Column():
124
  process_button_animation = gr.Button("🚀 Animate", variant="primary")
125
  with gr.Column():
126
- process_button_reset = gr.ClearButton([image_input, video_input, output_video, output_video_concat], value="🧹 Clear")
 
 
 
 
127
  with gr.Row():
128
  with gr.Column():
129
  with gr.Accordion(open=True, label="The animated video in the original image space"):
@@ -131,9 +132,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
131
  with gr.Column():
132
  with gr.Accordion(open=True, label="The animated video"):
133
  output_video_concat.render()
 
134
  with gr.Row():
135
- # Examples
136
  gr.Markdown("## You could also choose the examples below by one click ⬇️")
 
137
  with gr.Row():
138
  gr.Examples(
139
  examples=data_examples,
@@ -145,14 +147,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
145
  flag_do_crop_input,
146
  flag_remap_input
147
  ],
148
- outputs=[output_image, output_image_paste_back],
149
  examples_per_page=6,
150
  cache_examples=False,
151
  )
 
152
  gr.Markdown(load_description("assets/gradio_description_retargeting.md"), visible=True)
 
153
  with gr.Row(visible=True):
154
  eye_retargeting_slider.render()
155
  lip_retargeting_slider.render()
 
156
  with gr.Row(visible=True):
157
  process_button_retargeting = gr.Button("🚗 Retargeting", variant="primary")
158
  process_button_reset_retargeting = gr.ClearButton(
@@ -165,6 +170,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
165
  ],
166
  value="🧹 Clear"
167
  )
 
168
  with gr.Row(visible=True):
169
  with gr.Column():
170
  with gr.Accordion(open=True, label="Retargeting Input"):
@@ -188,14 +194,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
188
  with gr.Column():
189
  with gr.Accordion(open=True, label="Paste-back Result"):
190
  output_image_paste_back.render()
191
- # binding functions for buttons
192
  process_button_retargeting.click(
193
- # fn=gradio_pipeline.execute_image,
194
  fn=gpu_wrapped_execute_image,
195
  inputs=[eye_retargeting_slider, lip_retargeting_slider, retargeting_input_image, flag_do_crop_input],
196
  outputs=[output_image, output_image_paste_back],
197
  show_progress=True
198
  )
 
199
  process_button_animation.click(
200
  fn=gpu_wrapped_execute_video,
201
  inputs=[
@@ -208,13 +214,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
208
  outputs=[output_video, output_video_concat],
209
  show_progress=True
210
  )
211
- # image_input.change(
212
- # fn=gradio_pipeline.prepare_retargeting,
213
- # inputs=image_input,
214
- # outputs=[eye_retargeting_slider, lip_retargeting_slider, retargeting_input_image]
215
- # )
216
  video_input.upload(
217
- fn=is_square_video,
218
  inputs=video_input,
219
  outputs=video_input
220
  )
@@ -222,4 +225,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
222
  demo.launch(
223
  server_name="0.0.0.0",
224
  server_port=7860
225
- )
 
13
  from src.config.argument_config import ArgumentConfig
14
  from src.config.inference_config import InferenceConfig
15
  import spaces
 
16
 
17
  # import gdown
18
  # folder_url = f"https://drive.google.com/drive/folders/1UtKgzKjFAOmZkhNK-OYT0caJ_w2XAnib"
 
26
  args = tyro.cli(ArgumentConfig)
27
 
28
  # specify configs for inference
29
+ inference_cfg = partial_fields(InferenceConfig, args.__dict__)
30
+ crop_cfg = partial_fields(CropConfig, args.__dict__)
31
 
32
  gradio_pipeline = GradioPipeline(
33
  inference_cfg=inference_cfg,
 
43
  def gpu_wrapped_execute_image(*args, **kwargs):
44
  return gradio_pipeline.execute_image(*args, **kwargs)
45
 
46
+ # Removida a exigência de vídeo quadrado.
47
+ # Mantemos uma função neutra só para não quebrar a lógica se quiser reutilizar depois.
48
+ def accept_any_video(video_path):
49
+ return gr.update()
 
 
 
 
 
 
 
50
 
51
  # assets
52
  title_md = "assets/gradio_title.md"
 
60
  [osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d19.mp4"), True, True, True, True],
61
  [osp.join(example_portrait_dir, "s22.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
62
  ]
63
+
64
  #################### interface logic ####################
65
 
 
66
  eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
67
  lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
68
  retargeting_input_image = gr.Image(type="filepath")
 
74
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
75
  gr.HTML(load_description(title_md))
76
  gr.Markdown(load_description("assets/gradio_description_upload.md"))
77
+
78
  with gr.Row():
79
  with gr.Accordion(open=True, label="Source Portrait"):
80
  image_input = gr.Image(type="filepath")
 
91
  inputs=[image_input],
92
  cache_examples=False,
93
  )
94
+
95
  with gr.Accordion(open=True, label="Driving Video"):
96
  video_input = gr.Video()
97
  gr.Examples(
 
105
  inputs=[video_input],
106
  cache_examples=False,
107
  )
108
+
109
  with gr.Row():
110
  with gr.Accordion(open=False, label="Animation Instructions and Options"):
111
  gr.Markdown(load_description("assets/gradio_description_animation.md"))
 
113
  flag_relative_input = gr.Checkbox(value=True, label="relative motion")
114
  flag_do_crop_input = gr.Checkbox(value=True, label="do crop")
115
  flag_remap_input = gr.Checkbox(value=True, label="paste-back")
116
+
117
  gr.Markdown(load_description("assets/gradio_description_animate_clear.md"))
118
+
119
  with gr.Row():
120
  with gr.Column():
121
  process_button_animation = gr.Button("🚀 Animate", variant="primary")
122
  with gr.Column():
123
+ process_button_reset = gr.ClearButton(
124
+ [image_input, video_input, output_video, output_video_concat],
125
+ value="🧹 Clear"
126
+ )
127
+
128
  with gr.Row():
129
  with gr.Column():
130
  with gr.Accordion(open=True, label="The animated video in the original image space"):
 
132
  with gr.Column():
133
  with gr.Accordion(open=True, label="The animated video"):
134
  output_video_concat.render()
135
+
136
  with gr.Row():
 
137
  gr.Markdown("## You could also choose the examples below by one click ⬇️")
138
+
139
  with gr.Row():
140
  gr.Examples(
141
  examples=data_examples,
 
147
  flag_do_crop_input,
148
  flag_remap_input
149
  ],
150
+ outputs=[output_video, output_video_concat],
151
  examples_per_page=6,
152
  cache_examples=False,
153
  )
154
+
155
  gr.Markdown(load_description("assets/gradio_description_retargeting.md"), visible=True)
156
+
157
  with gr.Row(visible=True):
158
  eye_retargeting_slider.render()
159
  lip_retargeting_slider.render()
160
+
161
  with gr.Row(visible=True):
162
  process_button_retargeting = gr.Button("🚗 Retargeting", variant="primary")
163
  process_button_reset_retargeting = gr.ClearButton(
 
170
  ],
171
  value="🧹 Clear"
172
  )
173
+
174
  with gr.Row(visible=True):
175
  with gr.Column():
176
  with gr.Accordion(open=True, label="Retargeting Input"):
 
194
  with gr.Column():
195
  with gr.Accordion(open=True, label="Paste-back Result"):
196
  output_image_paste_back.render()
197
+
198
  process_button_retargeting.click(
 
199
  fn=gpu_wrapped_execute_image,
200
  inputs=[eye_retargeting_slider, lip_retargeting_slider, retargeting_input_image, flag_do_crop_input],
201
  outputs=[output_image, output_image_paste_back],
202
  show_progress=True
203
  )
204
+
205
  process_button_animation.click(
206
  fn=gpu_wrapped_execute_video,
207
  inputs=[
 
214
  outputs=[output_video, output_video_concat],
215
  show_progress=True
216
  )
217
+
218
+ # Sem validação obrigando vídeo quadrado
 
 
 
219
  video_input.upload(
220
+ fn=accept_any_video,
221
  inputs=video_input,
222
  outputs=video_input
223
  )
 
225
  demo.launch(
226
  server_name="0.0.0.0",
227
  server_port=7860
228
+ )