Spaces:
Running
Running
victorisgeek
commited on
Commit
โข
efbabed
1
Parent(s):
de0bb6c
Upload app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import cv2
|
3 |
import glob
|
4 |
import time
|
|
|
5 |
import shutil
|
6 |
import argparse
|
7 |
import platform
|
@@ -188,21 +189,21 @@ def process(
|
|
188 |
|
189 |
|
190 |
|
191 |
-
yield "### \n
|
192 |
load_face_analyser_model()
|
193 |
|
194 |
-
yield "### \n
|
195 |
load_face_swapper_model()
|
196 |
|
197 |
if face_enhancer_name != "NONE":
|
198 |
if face_enhancer_name not in cv2_interpolations:
|
199 |
-
yield f"### \n
|
200 |
FACE_ENHANCER = load_face_enhancer_model(name=face_enhancer_name, device=device)
|
201 |
else:
|
202 |
FACE_ENHANCER = None
|
203 |
|
204 |
if enable_face_parser:
|
205 |
-
yield "### \n
|
206 |
load_face_parser_model()
|
207 |
|
208 |
includes = mask_regions_to_list(mask_includes)
|
@@ -220,7 +221,7 @@ def process(
|
|
220 |
## ------------------------------ CONTENT CHECK ------------------------------
|
221 |
|
222 |
|
223 |
-
yield "### \n
|
224 |
if condition != "Specific Face":
|
225 |
source_data = source_path, age
|
226 |
else:
|
@@ -236,7 +237,7 @@ def process(
|
|
236 |
|
237 |
## ------------------------------ SWAP FUNC ------------------------------
|
238 |
|
239 |
-
yield "### \n
|
240 |
preds = []
|
241 |
matrs = []
|
242 |
count = 0
|
@@ -250,13 +251,13 @@ def process(
|
|
250 |
if USE_CUDA:
|
251 |
image_grid = create_image_grid(batch_pred, size=128)
|
252 |
PREVIEW = image_grid[:, :, ::-1]
|
253 |
-
yield f"### \n
|
254 |
|
255 |
## ------------------------------ FACE ENHANCEMENT ------------------------------
|
256 |
|
257 |
generated_len = len(preds)
|
258 |
if face_enhancer_name != "NONE":
|
259 |
-
yield f"### \n
|
260 |
for idx, pred in tqdm(enumerate(preds), total=generated_len, desc=f"Upscaling with {face_enhancer_name}"):
|
261 |
enhancer_model, enhancer_model_runner = FACE_ENHANCER
|
262 |
pred = enhancer_model_runner(pred, enhancer_model)
|
@@ -266,7 +267,7 @@ def process(
|
|
266 |
## ------------------------------ FACE PARSING ------------------------------
|
267 |
|
268 |
if enable_face_parser:
|
269 |
-
yield "### \n
|
270 |
masks = []
|
271 |
count = 0
|
272 |
for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=device, batch_size=BATCH_SIZE, softness=int(mask_soft_iterations)):
|
@@ -277,7 +278,7 @@ def process(
|
|
277 |
if len(batch_mask) > 1:
|
278 |
image_grid = create_image_grid(batch_mask, size=128)
|
279 |
PREVIEW = image_grid[:, :, ::-1]
|
280 |
-
yield f"### \n
|
281 |
masks = np.concatenate(masks, axis=0) if len(masks) >= 1 else masks
|
282 |
else:
|
283 |
masks = [None] * generated_len
|
@@ -293,7 +294,7 @@ def process(
|
|
293 |
|
294 |
## ------------------------------ PASTE-BACK ------------------------------
|
295 |
|
296 |
-
yield "### \n
|
297 |
def post_process(frame_idx, frame_img, split_preds, split_matrs, split_masks, enable_laplacian_blend, crop_mask, blur_amount, erode_amount):
|
298 |
whole_img_path = frame_img
|
299 |
whole_img = cv2.imread(whole_img_path)
|
@@ -349,7 +350,7 @@ def process(
|
|
349 |
temp_path = os.path.join(output_path, output_name, "sequence")
|
350 |
os.makedirs(temp_path, exist_ok=True)
|
351 |
|
352 |
-
yield "### \n
|
353 |
image_sequence = []
|
354 |
cap = cv2.VideoCapture(video_path)
|
355 |
curr_idx = 0
|
@@ -366,12 +367,12 @@ def process(
|
|
366 |
for info_update in swap_process(image_sequence):
|
367 |
yield info_update
|
368 |
|
369 |
-
yield "### \n
|
370 |
output_video_path = os.path.join(output_path, output_name + ".mp4")
|
371 |
merge_img_sequence_from_ref(video_path, image_sequence, output_video_path)
|
372 |
|
373 |
if os.path.exists(temp_path) and not keep_output_sequence:
|
374 |
-
yield "### \n
|
375 |
shutil.rmtree(temp_path)
|
376 |
|
377 |
WORKSPACE = output_path
|
@@ -489,7 +490,7 @@ def video_changed(video_path):
|
|
489 |
|
490 |
|
491 |
def analyse_settings_changed(detect_condition, detection_size, detection_threshold):
|
492 |
-
yield "### \n
|
493 |
global FACE_ANALYSER
|
494 |
global DETECT_CONDITION
|
495 |
DETECT_CONDITION = detect_condition
|
@@ -525,14 +526,14 @@ def slider_changed(show_frame, video_path, frame_index):
|
|
525 |
|
526 |
|
527 |
def trim_and_reload(video_path, output_path, output_name, start_frame, stop_frame):
|
528 |
-
yield video_path, f"### \n
|
529 |
try:
|
530 |
output_path = os.path.join(output_path, output_name)
|
531 |
trimmed_video = trim_video(video_path, output_path, start_frame, stop_frame)
|
532 |
-
yield trimmed_video, "### \n
|
533 |
except Exception as e:
|
534 |
print(e)
|
535 |
-
yield video_path, "### \n โVideo trimming failed. See console for more info."
|
536 |
|
537 |
|
538 |
## ------------------------------ GRADIO GUI ------------------------------
|
@@ -541,13 +542,13 @@ css = """
|
|
541 |
footer{display:none !important}
|
542 |
"""
|
543 |
|
544 |
-
with gr.Blocks(
|
545 |
-
gr.Markdown("#
|
546 |
-
gr.Markdown("###
|
547 |
with gr.Row():
|
548 |
with gr.Row():
|
549 |
with gr.Column(scale=0.4):
|
550 |
-
with gr.Tab("
|
551 |
swap_option = gr.Dropdown(
|
552 |
swap_options_list,
|
553 |
info="Choose which face or faces in the target image to swap.",
|
@@ -560,7 +561,7 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
560 |
value=25, label="Value", interactive=True, visible=False
|
561 |
)
|
562 |
|
563 |
-
with gr.Tab("๐Detection Settings"):
|
564 |
detect_condition_dropdown = gr.Dropdown(
|
565 |
detect_conditions,
|
566 |
label="Condition",
|
@@ -578,7 +579,7 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
578 |
)
|
579 |
apply_detection_settings = gr.Button("Apply settings")
|
580 |
|
581 |
-
with gr.Tab("
|
582 |
output_directory = gr.Text(
|
583 |
label="Output Directory",
|
584 |
value=DEF_OUTPUT_PATH,
|
@@ -591,7 +592,7 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
591 |
label="Keep output sequence", value=False, interactive=True
|
592 |
)
|
593 |
|
594 |
-
with gr.Tab("
|
595 |
face_scale = gr.Slider(
|
596 |
label="Face Scale",
|
597 |
minimum=0,
|
@@ -670,7 +671,7 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
670 |
label="Source face", type="filepath", interactive=True
|
671 |
)
|
672 |
|
673 |
-
with gr.
|
674 |
for i in range(NUM_OF_SRC_SPECIFIC):
|
675 |
idx = i + 1
|
676 |
code = "\n"
|
@@ -696,17 +697,17 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
696 |
value="Image",
|
697 |
)
|
698 |
|
699 |
-
with gr.
|
700 |
image_input = gr.Image(
|
701 |
label="Target Image", interactive=True, type="filepath"
|
702 |
)
|
703 |
|
704 |
-
with gr.
|
705 |
vid_widget = gr.Video if USE_COLAB else gr.Text
|
706 |
video_input = gr.Video(
|
707 |
label="Target Video", interactive=True
|
708 |
)
|
709 |
-
with gr.Accordion("
|
710 |
with gr.Column():
|
711 |
with gr.Row():
|
712 |
set_slider_range_btn = gr.Button(
|
@@ -746,15 +747,15 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
746 |
"Trim and Reload", interactive=True
|
747 |
)
|
748 |
|
749 |
-
with gr.
|
750 |
direc_input = gr.Text(label="Path", interactive=True)
|
751 |
|
752 |
-
with gr.Column(scale=0.
|
753 |
info = gr.Markdown(value="...")
|
754 |
|
755 |
with gr.Row():
|
756 |
swap_button = gr.Button("๐ฏ Swap", variant="primary")
|
757 |
-
cancel_button = gr.Button("
|
758 |
|
759 |
preview_image = gr.Image(label="Output", interactive=False)
|
760 |
preview_video = gr.Video(
|
@@ -763,29 +764,14 @@ with gr.Blocks(theme='earneleh/paris') as interface:
|
|
763 |
|
764 |
with gr.Row():
|
765 |
output_directory_button = gr.Button(
|
766 |
-
"
|
767 |
)
|
768 |
output_video_button = gr.Button(
|
769 |
-
"
|
770 |
)
|
771 |
|
772 |
-
|
773 |
-
|
774 |
-
gr.Markdown(
|
775 |
-
"### [๐ญ Sponsor]"
|
776 |
-
)
|
777 |
-
gr.Markdown(
|
778 |
-
"### [๐ฅ๏ธ Source code](https://huggingface.co/spaces/victorisgeek/SwapFace2Pon)"
|
779 |
-
)
|
780 |
-
gr.Markdown(
|
781 |
-
"### [ ๐งฉ Playground](https://huggingface.co/spaces/victorisgeek/SwapFace2Pon)"
|
782 |
-
)
|
783 |
-
gr.Markdown(
|
784 |
-
"### [๐ธ Run in Colab](https://colab.research.google.com/github/victorgeel/FaceSwapNoNfsw/blob/main/SwapFace.ipynb)"
|
785 |
-
)
|
786 |
-
gr.Markdown(
|
787 |
-
"### [๐ค Modified Version](https://github.com/victorgeel/FaceSwapNoNfsw)"
|
788 |
-
)
|
789 |
|
790 |
## ------------------------------ GRADIO EVENTS ------------------------------
|
791 |
|
@@ -902,4 +888,4 @@ if __name__ == "__main__":
|
|
902 |
if USE_COLAB:
|
903 |
print("Running in colab mode")
|
904 |
|
905 |
-
interface.queue(concurrency_count=2, max_size=20).launch(share=
|
|
|
2 |
import cv2
|
3 |
import glob
|
4 |
import time
|
5 |
+
import torch
|
6 |
import shutil
|
7 |
import argparse
|
8 |
import platform
|
|
|
189 |
|
190 |
|
191 |
|
192 |
+
yield "### \n โ Loading face analyser model...", *ui_before()
|
193 |
load_face_analyser_model()
|
194 |
|
195 |
+
yield "### \n โ Loading face swapper model...", *ui_before()
|
196 |
load_face_swapper_model()
|
197 |
|
198 |
if face_enhancer_name != "NONE":
|
199 |
if face_enhancer_name not in cv2_interpolations:
|
200 |
+
yield f"### \n โ Loading {face_enhancer_name} model...", *ui_before()
|
201 |
FACE_ENHANCER = load_face_enhancer_model(name=face_enhancer_name, device=device)
|
202 |
else:
|
203 |
FACE_ENHANCER = None
|
204 |
|
205 |
if enable_face_parser:
|
206 |
+
yield "### \n โ Loading face parsing model...", *ui_before()
|
207 |
load_face_parser_model()
|
208 |
|
209 |
includes = mask_regions_to_list(mask_includes)
|
|
|
221 |
## ------------------------------ CONTENT CHECK ------------------------------
|
222 |
|
223 |
|
224 |
+
yield "### \n โ Analysing face data...", *ui_before()
|
225 |
if condition != "Specific Face":
|
226 |
source_data = source_path, age
|
227 |
else:
|
|
|
237 |
|
238 |
## ------------------------------ SWAP FUNC ------------------------------
|
239 |
|
240 |
+
yield "### \n โ Generating faces...", *ui_before()
|
241 |
preds = []
|
242 |
matrs = []
|
243 |
count = 0
|
|
|
251 |
if USE_CUDA:
|
252 |
image_grid = create_image_grid(batch_pred, size=128)
|
253 |
PREVIEW = image_grid[:, :, ::-1]
|
254 |
+
yield f"### \n โ Generating face Batch {count}", *ui_before()
|
255 |
|
256 |
## ------------------------------ FACE ENHANCEMENT ------------------------------
|
257 |
|
258 |
generated_len = len(preds)
|
259 |
if face_enhancer_name != "NONE":
|
260 |
+
yield f"### \n โ Upscaling faces with {face_enhancer_name}...", *ui_before()
|
261 |
for idx, pred in tqdm(enumerate(preds), total=generated_len, desc=f"Upscaling with {face_enhancer_name}"):
|
262 |
enhancer_model, enhancer_model_runner = FACE_ENHANCER
|
263 |
pred = enhancer_model_runner(pred, enhancer_model)
|
|
|
267 |
## ------------------------------ FACE PARSING ------------------------------
|
268 |
|
269 |
if enable_face_parser:
|
270 |
+
yield "### \n โ Face-parsing mask...", *ui_before()
|
271 |
masks = []
|
272 |
count = 0
|
273 |
for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=device, batch_size=BATCH_SIZE, softness=int(mask_soft_iterations)):
|
|
|
278 |
if len(batch_mask) > 1:
|
279 |
image_grid = create_image_grid(batch_mask, size=128)
|
280 |
PREVIEW = image_grid[:, :, ::-1]
|
281 |
+
yield f"### \n โ Face parsing Batch {count}", *ui_before()
|
282 |
masks = np.concatenate(masks, axis=0) if len(masks) >= 1 else masks
|
283 |
else:
|
284 |
masks = [None] * generated_len
|
|
|
294 |
|
295 |
## ------------------------------ PASTE-BACK ------------------------------
|
296 |
|
297 |
+
yield "### \n โ Pasting back...", *ui_before()
|
298 |
def post_process(frame_idx, frame_img, split_preds, split_matrs, split_masks, enable_laplacian_blend, crop_mask, blur_amount, erode_amount):
|
299 |
whole_img_path = frame_img
|
300 |
whole_img = cv2.imread(whole_img_path)
|
|
|
350 |
temp_path = os.path.join(output_path, output_name, "sequence")
|
351 |
os.makedirs(temp_path, exist_ok=True)
|
352 |
|
353 |
+
yield "### \n โ Extracting video frames...", *ui_before()
|
354 |
image_sequence = []
|
355 |
cap = cv2.VideoCapture(video_path)
|
356 |
curr_idx = 0
|
|
|
367 |
for info_update in swap_process(image_sequence):
|
368 |
yield info_update
|
369 |
|
370 |
+
yield "### \n โ Merging sequence...", *ui_before()
|
371 |
output_video_path = os.path.join(output_path, output_name + ".mp4")
|
372 |
merge_img_sequence_from_ref(video_path, image_sequence, output_video_path)
|
373 |
|
374 |
if os.path.exists(temp_path) and not keep_output_sequence:
|
375 |
+
yield "### \n โ Removing temporary files...", *ui_before()
|
376 |
shutil.rmtree(temp_path)
|
377 |
|
378 |
WORKSPACE = output_path
|
|
|
490 |
|
491 |
|
492 |
def analyse_settings_changed(detect_condition, detection_size, detection_threshold):
|
493 |
+
yield "### \n โ Applying new values..."
|
494 |
global FACE_ANALYSER
|
495 |
global DETECT_CONDITION
|
496 |
DETECT_CONDITION = detect_condition
|
|
|
526 |
|
527 |
|
528 |
def trim_and_reload(video_path, output_path, output_name, start_frame, stop_frame):
|
529 |
+
yield video_path, f"### \n โ Trimming video frame {start_frame} to {stop_frame}..."
|
530 |
try:
|
531 |
output_path = os.path.join(output_path, output_name)
|
532 |
trimmed_video = trim_video(video_path, output_path, start_frame, stop_frame)
|
533 |
+
yield trimmed_video, "### \n โ๏ธ Video trimmed and reloaded."
|
534 |
except Exception as e:
|
535 |
print(e)
|
536 |
+
yield video_path, "### \n โ Video trimming failed. See console for more info."
|
537 |
|
538 |
|
539 |
## ------------------------------ GRADIO GUI ------------------------------
|
|
|
542 |
footer{display:none !important}
|
543 |
"""
|
544 |
|
545 |
+
with gr.Blocks(css=css) as interface:
|
546 |
+
gr.Markdown("# ๐ญ Swapper")
|
547 |
+
gr.Markdown("### Face swap app based on insightface inswapper.")
|
548 |
with gr.Row():
|
549 |
with gr.Row():
|
550 |
with gr.Column(scale=0.4):
|
551 |
+
with gr.Tab("๐ Swap Condition"):
|
552 |
swap_option = gr.Dropdown(
|
553 |
swap_options_list,
|
554 |
info="Choose which face or faces in the target image to swap.",
|
|
|
561 |
value=25, label="Value", interactive=True, visible=False
|
562 |
)
|
563 |
|
564 |
+
with gr.Tab("๐ Detection Settings"):
|
565 |
detect_condition_dropdown = gr.Dropdown(
|
566 |
detect_conditions,
|
567 |
label="Condition",
|
|
|
579 |
)
|
580 |
apply_detection_settings = gr.Button("Apply settings")
|
581 |
|
582 |
+
with gr.Tab("๐ง Output Settings"):
|
583 |
output_directory = gr.Text(
|
584 |
label="Output Directory",
|
585 |
value=DEF_OUTPUT_PATH,
|
|
|
592 |
label="Keep output sequence", value=False, interactive=True
|
593 |
)
|
594 |
|
595 |
+
with gr.Tab("๐จ Other Settings"):
|
596 |
face_scale = gr.Slider(
|
597 |
label="Face Scale",
|
598 |
minimum=0,
|
|
|
671 |
label="Source face", type="filepath", interactive=True
|
672 |
)
|
673 |
|
674 |
+
with gr.Box(visible=False) as specific_face:
|
675 |
for i in range(NUM_OF_SRC_SPECIFIC):
|
676 |
idx = i + 1
|
677 |
code = "\n"
|
|
|
697 |
value="Image",
|
698 |
)
|
699 |
|
700 |
+
with gr.Box(visible=True) as input_image_group:
|
701 |
image_input = gr.Image(
|
702 |
label="Target Image", interactive=True, type="filepath"
|
703 |
)
|
704 |
|
705 |
+
with gr.Box(visible=False) as input_video_group:
|
706 |
vid_widget = gr.Video if USE_COLAB else gr.Text
|
707 |
video_input = gr.Video(
|
708 |
label="Target Video", interactive=True
|
709 |
)
|
710 |
+
with gr.Accordion("๐ช Trim video", open=False):
|
711 |
with gr.Column():
|
712 |
with gr.Row():
|
713 |
set_slider_range_btn = gr.Button(
|
|
|
747 |
"Trim and Reload", interactive=True
|
748 |
)
|
749 |
|
750 |
+
with gr.Box(visible=False) as input_directory_group:
|
751 |
direc_input = gr.Text(label="Path", interactive=True)
|
752 |
|
753 |
+
with gr.Column(scale=0.6):
|
754 |
info = gr.Markdown(value="...")
|
755 |
|
756 |
with gr.Row():
|
757 |
swap_button = gr.Button("๐ฏ Swap", variant="primary")
|
758 |
+
cancel_button = gr.Button("โ Cancel")
|
759 |
|
760 |
preview_image = gr.Image(label="Output", interactive=False)
|
761 |
preview_video = gr.Video(
|
|
|
764 |
|
765 |
with gr.Row():
|
766 |
output_directory_button = gr.Button(
|
767 |
+
"๐", interactive=False, visible=False
|
768 |
)
|
769 |
output_video_button = gr.Button(
|
770 |
+
"๐บ", interactive=False, visible=False
|
771 |
)
|
772 |
|
773 |
+
|
774 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
775 |
|
776 |
## ------------------------------ GRADIO EVENTS ------------------------------
|
777 |
|
|
|
888 |
if USE_COLAB:
|
889 |
print("Running in colab mode")
|
890 |
|
891 |
+
interface.queue(concurrency_count=2, max_size=20).launch(share=False)
|