Ahmad Faris commited on
Commit
33d53d8
·
1 Parent(s): 4a763bf

remove : unused

Browse files
Files changed (1) hide show
  1. app.py +70 -364
app.py CHANGED
@@ -154,36 +154,37 @@ load_face_swapper_model()
154
 
155
 
156
  def process(
157
- input_type,
158
- image_path,
159
  video_path,
160
- directory_path,
161
  source_path,
162
- output_path,
163
- output_name,
164
- keep_output_sequence,
165
- condition,
166
- age,
167
- distance,
168
- face_enhancer_name,
169
- enable_face_parser,
170
- mask_includes,
171
- mask_soft_kernel,
172
- mask_soft_iterations,
173
- blur_amount,
174
- erode_amount,
175
- face_scale,
176
- enable_laplacian_blend,
177
- crop_top,
178
- crop_bott,
179
- crop_left,
180
- crop_right,
181
- *specifics,
182
  ):
183
  global WORKSPACE
184
  global OUTPUT_FILE
185
  global PREVIEW
 
 
 
 
 
186
  WORKSPACE, OUTPUT_FILE, PREVIEW = None, None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
  ## ------------------------------ GUI UPDATE FUNC ------------------------------
189
 
@@ -217,8 +218,6 @@ def process(
217
 
218
  ## ------------------------------ PREPARE INPUTS & LOAD MODELS ------------------------------
219
 
220
-
221
-
222
  yield "### \n ⌛ Loading face analyser model...", *ui_before()
223
  load_face_analyser_model()
224
 
@@ -236,11 +235,7 @@ def process(
236
  yield "### \n ⌛ Loading face parsing model...", *ui_before()
237
  load_face_parser_model()
238
 
239
- includes = mask_regions_to_list(mask_includes)
240
- specifics = list(specifics)
241
- half = len(specifics) // 2
242
- sources = specifics[:half]
243
- specifics = specifics[half:]
244
  if crop_top > crop_bott:
245
  crop_top, crop_bott = crop_bott, crop_top
246
  if crop_left > crop_right:
@@ -251,10 +246,7 @@ def process(
251
  ## ------------------------------ CONTENT CHECK ------------------------------
252
  print("### \n ⌛ Analysing face data...")
253
  log_message("⌛ Analysing face data...")
254
- if condition != "Specific Face":
255
- source_data = source_path, age
256
- else:
257
- source_data = ((sources, specifics), distance)
258
  analysed_targets, analysed_sources, whole_frame_list, num_faces_per_frame = get_analysed_data(
259
  FACE_ANALYSER,
260
  image_sequence,
@@ -306,7 +298,7 @@ def process(
306
  log_message("⌛ Face-parsing mask...")
307
  masks = []
308
  count = 0
309
- for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=device, batch_size=BATCH_SIZE, softness=int(mask_soft_iterations)):
310
  masks.append(batch_mask)
311
  EMPTY_CACHE()
312
  count += 1
@@ -374,147 +366,59 @@ def process(
374
 
375
  ## ------------------------------ IMAGE ------------------------------
376
 
377
- if input_type == "Image":
378
- target = cv2.imread(image_path)
379
- output_file = os.path.join(output_path, output_name + ".png")
380
- cv2.imwrite(output_file, target)
381
-
382
- for info_update in swap_process([output_file]):
383
- yield info_update
384
-
385
- OUTPUT_FILE = output_file
386
- WORKSPACE = output_path
387
- PREVIEW = cv2.imread(output_file)[:, :, ::-1]
388
-
389
- yield get_finsh_text(start_time), *ui_after()
390
-
391
  ## ------------------------------ VIDEO ------------------------------
392
 
393
- elif input_type == "Video":
394
- temp_path = os.path.join(output_path, output_name, "sequence")
395
- os.makedirs(temp_path, exist_ok=True)
396
-
397
- print("### \n ⌛ Extracting video frames...")
398
- log_message("⌛ Extracting video frames...")
399
- image_sequence = []
400
- cap = cv2.VideoCapture(video_path)
401
- curr_idx = 0
402
- while True:
403
- ret, frame = cap.read()
404
- if not ret:break
405
- frame_path = os.path.join(temp_path, f"frame_{curr_idx}.jpg")
406
- cv2.imwrite(frame_path, frame)
407
- image_sequence.append(frame_path)
408
- curr_idx += 1
409
- print("Curr IDX: {}".format(curr_idx))
410
- cap.release()
411
- cv2.destroyAllWindows()
412
-
413
- print("Total image sequence: {}".format(len(image_sequence)))
414
- swap_process(image_sequence)
415
- # for info_update in swap_process(image_sequence):
416
- # # print(info_update)
417
- # yield info_update, *ui_before()
418
-
419
- print("End swap_process")
420
-
421
- # yield "### \n ⌛ Merging sequence...", *ui_before()
422
- print("### \n ⌛ Merging sequence...")
423
- log_message("⌛ Merging sequence...")
424
- output_video_path = os.path.join(output_path, output_name + ".mp4")
425
- merge_img_sequence_from_ref(video_path, image_sequence, output_video_path)
426
-
427
- if os.path.exists(temp_path) and not keep_output_sequence:
428
- print("### \n ⌛ Removing temporary files...")
429
- print("⌛ Removing temporary files...")
430
- shutil.rmtree(temp_path)
431
-
432
- WORKSPACE = output_path
433
- OUTPUT_FILE = output_video_path
434
- log_result(OUTPUT_FILE)
435
-
436
- gr.update(value=OUTPUT_FILE, visible=True)
437
-
438
- yield get_finsh_text(start_time), *ui_after_vid()
439
-
440
- ## ------------------------------ DIRECTORY ------------------------------
441
-
442
- elif input_type == "Directory":
443
- extensions = ["jpg", "jpeg", "png", "bmp", "tiff", "ico", "webp"]
444
- temp_path = os.path.join(output_path, output_name)
445
- if os.path.exists(temp_path):
446
- shutil.rmtree(temp_path)
447
- os.mkdir(temp_path)
448
 
449
- file_paths =[]
450
- for file_path in glob.glob(os.path.join(directory_path, "*")):
451
- if any(file_path.lower().endswith(ext) for ext in extensions):
452
- img = cv2.imread(file_path)
453
- new_file_path = os.path.join(temp_path, os.path.basename(file_path))
454
- cv2.imwrite(new_file_path, img)
455
- file_paths.append(new_file_path)
456
 
457
- for info_update in swap_process(file_paths):
458
- yield info_update
 
 
459
 
460
- PREVIEW = cv2.imread(file_paths[-1])[:, :, ::-1]
461
- WORKSPACE = temp_path
462
- OUTPUT_FILE = file_paths[-1]
463
 
464
- yield get_finsh_text(start_time), *ui_after()
465
 
466
- ## ------------------------------ STREAM ------------------------------
467
 
468
- elif input_type == "Stream":
469
- pass
470
 
471
 
472
  ## ------------------------------ GRADIO FUNC ------------------------------
473
 
474
-
475
- def update_radio(value):
476
- if value == "Image":
477
- return (
478
- gr.update(visible=True),
479
- gr.update(visible=False),
480
- gr.update(visible=False),
481
- )
482
- elif value == "Video":
483
- return (
484
- gr.update(visible=False),
485
- gr.update(visible=True),
486
- gr.update(visible=False),
487
- )
488
- elif value == "Directory":
489
- return (
490
- gr.update(visible=False),
491
- gr.update(visible=False),
492
- gr.update(visible=True),
493
- )
494
- elif value == "Stream":
495
- return (
496
- gr.update(visible=False),
497
- gr.update(visible=False),
498
- gr.update(visible=True),
499
- )
500
-
501
-
502
- def swap_option_changed(value):
503
- if value.startswith("Age"):
504
- return (
505
- gr.update(visible=True),
506
- gr.update(visible=False),
507
- gr.update(visible=True),
508
- )
509
- elif value == "Specific Face":
510
- return (
511
- gr.update(visible=False),
512
- gr.update(visible=True),
513
- gr.update(visible=False),
514
- )
515
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
516
-
517
-
518
  def video_changed(video_path):
519
  sliders_update = gr.Slider.update
520
  button_update = gr.Button.update
@@ -605,161 +509,12 @@ with gr.Blocks(css=css) as interface:
605
  with gr.Row():
606
  with gr.Row():
607
  with gr.Column(scale=0.4):
608
- with gr.Tab("📄 Swap Condition"):
609
- swap_option = gr.Dropdown(
610
- swap_options_list,
611
- info="Choose which face or faces in the target image to swap.",
612
- multiselect=False,
613
- show_label=False,
614
- value=swap_options_list[0],
615
- interactive=True,
616
- )
617
- age = gr.Number(
618
- value=25, label="Value", interactive=True, visible=False
619
- )
620
-
621
- with gr.Tab("🎚️ Detection Settings"):
622
- detect_condition_dropdown = gr.Dropdown(
623
- detect_conditions,
624
- label="Condition",
625
- value=DETECT_CONDITION,
626
- interactive=True,
627
- info="This condition is only used when multiple faces are detected on source or specific image.",
628
- )
629
- detection_size = gr.Number(
630
- label="Detection Size", value=DETECT_SIZE, interactive=True
631
- )
632
- detection_threshold = gr.Number(
633
- label="Detection Threshold",
634
- value=DETECT_THRESH,
635
- interactive=True,
636
- )
637
- apply_detection_settings = gr.Button("Apply settings")
638
-
639
- with gr.Tab("📤 Output Settings"):
640
- output_directory = gr.Text(
641
- label="Output Directory",
642
- value=DEF_OUTPUT_PATH,
643
- interactive=True,
644
- )
645
- output_name = gr.Text(
646
- label="Output Name", value="Result", interactive=True
647
- )
648
- keep_output_sequence = gr.Checkbox(
649
- label="Keep output sequence", value=False, interactive=True
650
- )
651
-
652
- with gr.Tab("🪄 Other Settings"):
653
- face_scale = gr.Slider(
654
- label="Face Scale",
655
- minimum=0,
656
- maximum=2,
657
- value=1,
658
- interactive=True,
659
- )
660
-
661
- face_enhancer_name = gr.Dropdown(
662
- FACE_ENHANCER_LIST, label="Face Enhancer", value="NONE", multiselect=False, interactive=True
663
- )
664
-
665
- with gr.Accordion("Advanced Mask", open=False):
666
- enable_face_parser_mask = gr.Checkbox(
667
- label="Enable Face Parsing",
668
- value=False,
669
- interactive=True,
670
- )
671
-
672
- mask_include = gr.Dropdown(
673
- mask_regions.keys(),
674
- value=MASK_INCLUDE,
675
- multiselect=True,
676
- label="Include",
677
- interactive=True,
678
- )
679
- mask_soft_kernel = gr.Number(
680
- label="Soft Erode Kernel",
681
- value=MASK_SOFT_KERNEL,
682
- minimum=3,
683
- interactive=True,
684
- visible = False
685
- )
686
- mask_soft_iterations = gr.Number(
687
- label="Soft Erode Iterations",
688
- value=MASK_SOFT_ITERATIONS,
689
- minimum=0,
690
- interactive=True,
691
-
692
- )
693
-
694
-
695
- with gr.Accordion("Crop Mask", open=False):
696
- crop_top = gr.Slider(label="Top", minimum=0, maximum=511, value=0, step=1, interactive=True)
697
- crop_bott = gr.Slider(label="Bottom", minimum=0, maximum=511, value=511, step=1, interactive=True)
698
- crop_left = gr.Slider(label="Left", minimum=0, maximum=511, value=0, step=1, interactive=True)
699
- crop_right = gr.Slider(label="Right", minimum=0, maximum=511, value=511, step=1, interactive=True)
700
-
701
-
702
- erode_amount = gr.Slider(
703
- label="Mask Erode",
704
- minimum=0,
705
- maximum=1,
706
- value=MASK_ERODE_AMOUNT,
707
- step=0.05,
708
- interactive=True,
709
- )
710
-
711
- blur_amount = gr.Slider(
712
- label="Mask Blur",
713
- minimum=0,
714
- maximum=1,
715
- value=MASK_BLUR_AMOUNT,
716
- step=0.05,
717
- interactive=True,
718
- )
719
-
720
- enable_laplacian_blend = gr.Checkbox(
721
- label="Laplacian Blending",
722
- value=True,
723
- interactive=True,
724
- )
725
-
726
-
727
  source_image_input = gr.Image(
728
  label="Source face", type="filepath", interactive=True
729
  )
730
 
731
- with gr.Box(visible=False) as specific_face:
732
- for i in range(NUM_OF_SRC_SPECIFIC):
733
- idx = i + 1
734
- code = "\n"
735
- code += f"with gr.Tab(label='({idx})'):"
736
- code += "\n\twith gr.Row():"
737
- code += f"\n\t\tsrc{idx} = gr.Image(interactive=True, type='numpy', label='Source Face {idx}')"
738
- code += f"\n\t\ttrg{idx} = gr.Image(interactive=True, type='numpy', label='Specific Face {idx}')"
739
- exec(code)
740
-
741
- distance_slider = gr.Slider(
742
- minimum=0,
743
- maximum=2,
744
- value=0.6,
745
- interactive=True,
746
- label="Distance",
747
- info="Lower distance is more similar and higher distance is less similar to the target face.",
748
- )
749
-
750
  with gr.Group():
751
- input_type = gr.Radio(
752
- ["Image", "Video"],
753
- label="Target Type",
754
- value="Image",
755
- )
756
-
757
- with gr.Box(visible=True) as input_image_group:
758
- image_input = gr.Image(
759
- label="Target Image", interactive=True, type="filepath"
760
- )
761
-
762
- with gr.Box(visible=False) as input_video_group:
763
  vid_widget = gr.Video if USE_COLAB else gr.Text
764
  video_input = gr.Video(
765
  label="Target Video", interactive=True
@@ -804,9 +559,6 @@ with gr.Blocks(css=css) as interface:
804
  "Trim and Reload", interactive=True
805
  )
806
 
807
- with gr.Box(visible=False) as input_directory_group:
808
- direc_input = gr.Text(label="Path", interactive=True)
809
-
810
  with gr.Column(scale=0.6):
811
  info = gr.Markdown(value="...")
812
 
@@ -873,55 +625,9 @@ with gr.Blocks(css=css) as interface:
873
  show_progress=True,
874
  )
875
 
876
- input_type.change(
877
- update_radio,
878
- inputs=[input_type],
879
- outputs=[input_image_group, input_video_group, input_directory_group],
880
- )
881
- swap_option.change(
882
- swap_option_changed,
883
- inputs=[swap_option],
884
- outputs=[age, specific_face, source_image_input],
885
- )
886
-
887
- apply_detection_settings.click(
888
- analyse_settings_changed,
889
- inputs=[detect_condition_dropdown, detection_size, detection_threshold],
890
- outputs=[info],
891
- )
892
-
893
- src_specific_inputs = []
894
- gen_variable_txt = ",".join(
895
- [f"src{i+1}" for i in range(NUM_OF_SRC_SPECIFIC)]
896
- + [f"trg{i+1}" for i in range(NUM_OF_SRC_SPECIFIC)]
897
- )
898
- exec(f"src_specific_inputs = ({gen_variable_txt})")
899
  swap_inputs = [
900
- input_type,
901
- image_input,
902
  video_input,
903
- direc_input,
904
- source_image_input,
905
- output_directory,
906
- output_name,
907
- keep_output_sequence,
908
- swap_option,
909
- age,
910
- distance_slider,
911
- face_enhancer_name,
912
- enable_face_parser_mask,
913
- mask_include,
914
- mask_soft_kernel,
915
- mask_soft_iterations,
916
- blur_amount,
917
- erode_amount,
918
- face_scale,
919
- enable_laplacian_blend,
920
- crop_top,
921
- crop_bott,
922
- crop_left,
923
- crop_right,
924
- *src_specific_inputs,
925
  ]
926
 
927
  swap_outputs = [
 
154
 
155
 
156
  def process(
 
 
157
  video_path,
 
158
  source_path,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  ):
160
  global WORKSPACE
161
  global OUTPUT_FILE
162
  global PREVIEW
163
+ global MASK_INCLUDE
164
+ global MASK_SOFT_ITERATIONS
165
+ global MASK_BLUR_AMOUNT
166
+ global MASK_ERODE_AMOUNT
167
+ global NUM_OF_SRC_SPECIFIC
168
  WORKSPACE, OUTPUT_FILE, PREVIEW = None, None, None
169
+
170
+ ## Hardcoded value
171
+
172
+ input_type = "Video"
173
+ output_path = "/home/user/app"
174
+ output_name = "Result"
175
+ keep_output_sequence = False
176
+ face_scale = 1.0
177
+ condition = "All Female"
178
+ age = 25
179
+ face_enhancer_name = "NONE"
180
+ enable_face_parser = True
181
+ crop_top = 0
182
+ crop_bott = 511
183
+ crop_left = 0
184
+ crop_right = 511
185
+ blur_amount = MASK_BLUR_AMOUNT
186
+ erode_amount = MASK_ERODE_AMOUNT
187
+ enable_laplacian_blend = True
188
 
189
  ## ------------------------------ GUI UPDATE FUNC ------------------------------
190
 
 
218
 
219
  ## ------------------------------ PREPARE INPUTS & LOAD MODELS ------------------------------
220
 
 
 
221
  yield "### \n ⌛ Loading face analyser model...", *ui_before()
222
  load_face_analyser_model()
223
 
 
235
  yield "### \n ⌛ Loading face parsing model...", *ui_before()
236
  load_face_parser_model()
237
 
238
+ includes = mask_regions_to_list(MASK_INCLUDE)
 
 
 
 
239
  if crop_top > crop_bott:
240
  crop_top, crop_bott = crop_bott, crop_top
241
  if crop_left > crop_right:
 
246
  ## ------------------------------ CONTENT CHECK ------------------------------
247
  print("### \n ⌛ Analysing face data...")
248
  log_message("⌛ Analysing face data...")
249
+ source_data = source_path, age
 
 
 
250
  analysed_targets, analysed_sources, whole_frame_list, num_faces_per_frame = get_analysed_data(
251
  FACE_ANALYSER,
252
  image_sequence,
 
298
  log_message("⌛ Face-parsing mask...")
299
  masks = []
300
  count = 0
301
+ for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=device, batch_size=BATCH_SIZE, softness=int(MASK_SOFT_ITERATIONS)):
302
  masks.append(batch_mask)
303
  EMPTY_CACHE()
304
  count += 1
 
366
 
367
  ## ------------------------------ IMAGE ------------------------------
368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  ## ------------------------------ VIDEO ------------------------------
370
 
371
+ temp_path = os.path.join(output_path, output_name, "sequence")
372
+ os.makedirs(temp_path, exist_ok=True)
373
+
374
+ print("### \n ⌛ Extracting video frames...")
375
+ log_message("⌛ Extracting video frames...")
376
+ image_sequence = []
377
+ cap = cv2.VideoCapture(video_path)
378
+ curr_idx = 0
379
+ while True:
380
+ ret, frame = cap.read()
381
+ if not ret:break
382
+ frame_path = os.path.join(temp_path, f"frame_{curr_idx}.jpg")
383
+ cv2.imwrite(frame_path, frame)
384
+ image_sequence.append(frame_path)
385
+ curr_idx += 1
386
+ print("Curr IDX: {}".format(curr_idx))
387
+ cap.release()
388
+ cv2.destroyAllWindows()
389
+
390
+ print("Total image sequence: {}".format(len(image_sequence)))
391
+ swap_process(image_sequence)
392
+ # for info_update in swap_process(image_sequence):
393
+ # # print(info_update)
394
+ # yield info_update, *ui_before()
395
+
396
+ print("End swap_process")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
 
398
+ # yield "### \n ⌛ Merging sequence...", *ui_before()
399
+ print("### \n Merging sequence...")
400
+ log_message("⌛ Merging sequence...")
401
+ output_video_path = os.path.join(output_path, output_name + ".mp4")
402
+ merge_img_sequence_from_ref(video_path, image_sequence, output_video_path)
 
 
403
 
404
+ if os.path.exists(temp_path) and not keep_output_sequence:
405
+ print("### \n ⌛ Removing temporary files...")
406
+ print("⌛ Removing temporary files...")
407
+ shutil.rmtree(temp_path)
408
 
409
+ WORKSPACE = output_path
410
+ OUTPUT_FILE = output_video_path
411
+ log_result(OUTPUT_FILE)
412
 
413
+ gr.update(value=OUTPUT_FILE, visible=True)
414
 
415
+ yield get_finsh_text(start_time), *ui_after_vid()
416
 
417
+ ## ------------------------------ DIRECTORY ------------------------------
 
418
 
419
 
420
  ## ------------------------------ GRADIO FUNC ------------------------------
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  def video_changed(video_path):
423
  sliders_update = gr.Slider.update
424
  button_update = gr.Button.update
 
509
  with gr.Row():
510
  with gr.Row():
511
  with gr.Column(scale=0.4):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512
  source_image_input = gr.Image(
513
  label="Source face", type="filepath", interactive=True
514
  )
515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
  with gr.Group():
517
+ with gr.Box(visible=True) as input_video_group:
 
 
 
 
 
 
 
 
 
 
 
518
  vid_widget = gr.Video if USE_COLAB else gr.Text
519
  video_input = gr.Video(
520
  label="Target Video", interactive=True
 
559
  "Trim and Reload", interactive=True
560
  )
561
 
 
 
 
562
  with gr.Column(scale=0.6):
563
  info = gr.Markdown(value="...")
564
 
 
625
  show_progress=True,
626
  )
627
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
628
  swap_inputs = [
 
 
629
  video_input,
630
+ source_image_input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631
  ]
632
 
633
  swap_outputs = [