victorisgeek commited on
Commit
f6e07f5
·
verified ·
1 Parent(s): 6b3d598

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -2
app.py CHANGED
@@ -17,6 +17,31 @@ theme = gr.themes.Default(
17
  background_fill_secondary='#eaeaea'
18
  )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def add_bbox_padding(bbox, margin=5):
21
  return [
22
  bbox[0] - margin,
@@ -223,8 +248,120 @@ def create_interface():
223
  process_video.click(fn=swap_video_fct,inputs=[source_video,video_file_path,source_face_vid,dest_face_vid, face_tolerance], outputs=output_video)
224
  preview_video.click(fn=swap_video_fct,inputs=[source_video,video_file_path,source_face_vid,dest_face_vid, face_tolerance, video_position], outputs=image_output)
225
 
226
- face_swap_ui.queue().launch()
227
- #face_swap_ui.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
 
230
 
 
17
  background_fill_secondary='#eaeaea'
18
  )
19
 
20
+ def load_face_analyser_model(name="buffalo_l"):
21
+ global FACE_ANALYSER
22
+ if FACE_ANALYSER is None:
23
+ FACE_ANALYSER = insightface.app.FaceAnalysis(name=name, providers=PROVIDER)
24
+ FACE_ANALYSER.prepare(
25
+ ctx_id=0, det_size=(DETECT_SIZE, DETECT_SIZE), det_thresh=DETECT_THRESH
26
+ )
27
+
28
+
29
+ def load_face_swapper_model(path="./assets/pretrained_models/inswapper_128.onnx"):
30
+ global FACE_SWAPPER
31
+ if FACE_SWAPPER is None:
32
+ batch = int(BATCH_SIZE) if device == "cuda" else 1
33
+ FACE_SWAPPER = Inswapper(model_file=path, batch_size=batch, providers=PROVIDER)
34
+
35
+
36
+ def load_face_parser_model(path="./assets/pretrained_models/79999_iter.pth"):
37
+ global FACE_PARSER
38
+ if FACE_PARSER is None:
39
+ FACE_PARSER = init_parsing_model(path, device=device)
40
+
41
+
42
+ load_face_analyser_model()
43
+ load_face_swapper_model()
44
+
45
  def add_bbox_padding(bbox, margin=5):
46
  return [
47
  bbox[0] - margin,
 
248
  process_video.click(fn=swap_video_fct,inputs=[source_video,video_file_path,source_face_vid,dest_face_vid, face_tolerance], outputs=output_video)
249
  preview_video.click(fn=swap_video_fct,inputs=[source_video,video_file_path,source_face_vid,dest_face_vid, face_tolerance, video_position], outputs=image_output)
250
 
251
+ set_slider_range_event = set_slider_range_btn.click(
252
+ video_changed,
253
+ inputs=[video_input],
254
+ outputs=[start_frame, end_frame, video_fps],
255
+ )
256
+
257
+ trim_and_reload_event = trim_and_reload_btn.click(
258
+ fn=trim_and_reload,
259
+ inputs=[video_input, output_directory, output_name, start_frame, end_frame],
260
+ outputs=[video_input, info],
261
+ )
262
+
263
+ start_frame_event = start_frame.release(
264
+ fn=slider_changed,
265
+ inputs=[show_trim_preview_btn, video_input, start_frame],
266
+ outputs=[preview_image, preview_video],
267
+ show_progress=True,
268
+ )
269
+
270
+ end_frame_event = end_frame.release(
271
+ fn=slider_changed,
272
+ inputs=[show_trim_preview_btn, video_input, end_frame],
273
+ outputs=[preview_image, preview_video],
274
+ show_progress=True,
275
+ )
276
+
277
+ input_type.change(
278
+ update_radio,
279
+ inputs=[input_type],
280
+ outputs=[input_image_group, input_video_group, input_directory_group],
281
+ )
282
+ swap_option.change(
283
+ swap_option_changed,
284
+ inputs=[swap_option],
285
+ outputs=[age, specific_face, source_image_input],
286
+ )
287
+
288
+ apply_detection_settings.click(
289
+ analyse_settings_changed,
290
+ inputs=[detect_condition_dropdown, detection_size, detection_threshold],
291
+ outputs=[info],
292
+ )
293
+
294
+ src_specific_inputs = []
295
+ gen_variable_txt = ",".join(
296
+ [f"src{i+1}" for i in range(NUM_OF_SRC_SPECIFIC)]
297
+ + [f"trg{i+1}" for i in range(NUM_OF_SRC_SPECIFIC)]
298
+ )
299
+ exec(f"src_specific_inputs = ({gen_variable_txt})")
300
+ swap_inputs = [
301
+ input_type,
302
+ image_input,
303
+ video_input,
304
+ direc_input,
305
+ source_image_input,
306
+ output_directory,
307
+ output_name,
308
+ keep_output_sequence,
309
+ swap_option,
310
+ age,
311
+ distance_slider,
312
+ face_enhancer_name,
313
+ enable_face_parser_mask,
314
+ mask_include,
315
+ mask_soft_kernel,
316
+ mask_soft_iterations,
317
+ blur_amount,
318
+ erode_amount,
319
+ face_scale,
320
+ enable_laplacian_blend,
321
+ crop_top,
322
+ crop_bott,
323
+ crop_left,
324
+ crop_right,
325
+ *src_specific_inputs,
326
+ ]
327
+
328
+ swap_outputs = [
329
+ info,
330
+ preview_image,
331
+ output_directory_button,
332
+ output_video_button,
333
+ preview_video,
334
+ ]
335
+
336
+ swap_event = swap_button.click(
337
+ fn=process, inputs=swap_inputs, outputs=swap_outputs, show_progress=True
338
+ )
339
+
340
+ cancel_button.click(
341
+ fn=stop_running,
342
+ inputs=None,
343
+ outputs=[info],
344
+ cancels=[
345
+ swap_event,
346
+ trim_and_reload_event,
347
+ set_slider_range_event,
348
+ start_frame_event,
349
+ end_frame_event,
350
+ ],
351
+ show_progress=True,
352
+ )
353
+ output_directory_button.click(
354
+ lambda: open_directory(path=WORKSPACE), inputs=None, outputs=None
355
+ )
356
+ output_video_button.click(
357
+ lambda: open_directory(path=OUTPUT_FILE), inputs=None, outputs=None
358
+ )
359
+
360
+ if __name__ == "__main__":
361
+ if USE_COLAB:
362
+ print("Running in colab mode")
363
+
364
+ interface.queue(concurrency_count=2, max_size=20).launch(share=False)
365
 
366
 
367