From 9af216e8195334b220850be8104e8c1683181cdc Mon Sep 17 00:00:00 2001 From: Makaru Date: Tue, 14 Jan 2025 22:45:16 +0800 Subject: [PATCH 1/2] Opacity Update - Added 0 value, if it is set to 0, the face swapping will be disabled --- modules/globals.py | 2 ++ modules/processors/frame/face_swapper.py | 3 +++ modules/ui.py | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/modules/globals.py b/modules/globals.py index 693084d..c5b3e1e 100644 --- a/modules/globals.py +++ b/modules/globals.py @@ -41,3 +41,5 @@ show_mouth_mask_box = False mask_feather_ratio = 8 mask_down_size = 0.50 mask_size = 1 +opacity = 1.0 +face_swapper_enabled = True diff --git a/modules/processors/frame/face_swapper.py b/modules/processors/frame/face_swapper.py index 7c23775..890feeb 100644 --- a/modules/processors/frame/face_swapper.py +++ b/modules/processors/frame/face_swapper.py @@ -103,6 +103,9 @@ def process_frame(source_face: Face, temp_frame: Frame) -> Frame: if modules.globals.color_correction: temp_frame = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) + if modules.globals.opacity or modules.globals.face_swapper_enabled == 0: + return temp_frame + if modules.globals.many_faces: many_faces = get_many_faces(temp_frame) if many_faces: diff --git a/modules/ui.py b/modules/ui.py index d3feee9..f4b9048 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -175,14 +175,22 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C ) select_target_button.place(relx=0.6, rely=0.375, relwidth=0.3, relheight=0.1) - transparency_values = ["25%", "50%", "75%", "100%"] + transparency_values = ["0%","25%", "50%", "75%", "100%"] transparency_var = ctk.StringVar(value="100%") # Default to 100% def on_transparency_change(value: str): percentage = int(value.strip('%')) - opacity = percentage / 100.0 - modules.globals.opacity = opacity # Save opacity globally for real-time updates - update_status(f"Transparency set to {value}") + modules.globals.opacity = percentage / 100.0 + + if percentage == 0: + modules.globals.face_swapper_enabled = False + update_status("Transparency set to 0% - Face swapping disabled.") + elif percentage == 100: + modules.globals.face_swapper_enabled = True + update_status("Transparency set to 100%.") + else: + modules.globals.face_swapper_enabled = True + update_status(f"Transparency set to {value}") transparency_label = ctk.CTkLabel(root, text="Transparency:") transparency_label.place(relx=0.1, rely=0.5, relwidth=0.2, relheight=0.05) From 5dd6d1fe640e581de4e6be6a6507d728b9b0c1a7 Mon Sep 17 00:00:00 2001 From: Makaru Date: Wed, 15 Jan 2025 02:53:11 +0800 Subject: [PATCH 2/2] Fixed 0 Transparency The error was caused by an erroneous designation of "face_swapper_enabled" in lieu of "fp_ui." --- modules/processors/frame/face_swapper.py | 10 +++++++--- modules/ui.py | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/processors/frame/face_swapper.py b/modules/processors/frame/face_swapper.py index 890feeb..ccd3ef8 100644 --- a/modules/processors/frame/face_swapper.py +++ b/modules/processors/frame/face_swapper.py @@ -14,6 +14,7 @@ from modules.utilities import ( is_video, ) from modules.cluster_analysis import find_closest_centroid +from modules.globals import face_swapper_enabled, opacity import os FACE_SWAPPER = None @@ -100,12 +101,12 @@ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame: def process_frame(source_face: Face, temp_frame: Frame) -> Frame: + if getattr(modules.globals, "opacity", 1.0) == 0: + return temp_frame + if modules.globals.color_correction: temp_frame = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) - if modules.globals.opacity or modules.globals.face_swapper_enabled == 0: - return temp_frame - if modules.globals.many_faces: many_faces = get_many_faces(temp_frame) if many_faces: @@ -119,6 +120,9 @@ def process_frame(source_face: Face, temp_frame: Frame) -> Frame: def process_frame_v2(temp_frame: Frame, temp_frame_path: str = "") -> Frame: + if getattr(modules.globals, "opacity", 1.0) == 0: + return temp_frame + if is_image(modules.globals.target_path): if modules.globals.many_faces: source_face = default_source_face() diff --git a/modules/ui.py b/modules/ui.py index f4b9048..992ab5b 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -27,6 +27,7 @@ from modules.utilities import ( ) from modules.video_capture import VideoCapturer from modules.gettext import LanguageManager +from modules import globals import platform if platform.system() == "Windows": @@ -183,7 +184,7 @@ def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.C modules.globals.opacity = percentage / 100.0 if percentage == 0: - modules.globals.face_swapper_enabled = False + modules.globals.fp_ui["face_enhancer"] = False update_status("Transparency set to 0% - Face swapping disabled.") elif percentage == 100: modules.globals.face_swapper_enabled = True