wip, add nsfw option due to perf, still some mypy warnings
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
import gradio as gr
|
||||
from modules.shared import opts
|
||||
from modules import sd_models, sd_samplers
|
||||
from scripts.faceswaplab_utils.sd_utils import get_sd_option
|
||||
|
||||
|
||||
def face_inpainting_ui(
|
||||
@@ -19,14 +19,14 @@ def face_inpainting_ui(
|
||||
)
|
||||
|
||||
inpainting_denoising_prompt = gr.Textbox(
|
||||
opts.data.get(
|
||||
get_sd_option(
|
||||
"faceswaplab_pp_default_inpainting_prompt", "Portrait of a [gender]"
|
||||
),
|
||||
elem_id=f"{id_prefix}_pp_inpainting_denoising_prompt",
|
||||
label="Inpainting prompt use [gender] instead of men or woman",
|
||||
)
|
||||
inpainting_denoising_negative_prompt = gr.Textbox(
|
||||
opts.data.get(
|
||||
get_sd_option(
|
||||
"faceswaplab_pp_default_inpainting_negative_prompt", "blurry"
|
||||
),
|
||||
elem_id=f"{id_prefix}_pp_inpainting_denoising_neg_prompt",
|
||||
|
||||
@@ -2,8 +2,8 @@ from typing import List
|
||||
import gradio as gr
|
||||
import modules
|
||||
from modules import shared, sd_models
|
||||
from modules.shared import opts
|
||||
from scripts.faceswaplab_postprocessing.postprocessing_options import InpaintingWhen
|
||||
from scripts.faceswaplab_utils.sd_utils import get_sd_option
|
||||
|
||||
|
||||
def postprocessing_ui() -> List[gr.components.Component]:
|
||||
@@ -15,7 +15,7 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
face_restorer_name = gr.Radio(
|
||||
label="Restore Face",
|
||||
choices=["None"] + [x.name() for x in shared.face_restorers],
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_pp_default_face_restorer",
|
||||
shared.face_restorers[0].name(),
|
||||
),
|
||||
@@ -26,7 +26,7 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
face_restorer_visibility = gr.Slider(
|
||||
0,
|
||||
1,
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_pp_default_face_restorer_visibility", 1
|
||||
),
|
||||
step=0.001,
|
||||
@@ -36,7 +36,7 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
codeformer_weight = gr.Slider(
|
||||
0,
|
||||
1,
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_pp_default_face_restorer_weight", 1
|
||||
),
|
||||
step=0.001,
|
||||
@@ -45,7 +45,7 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
)
|
||||
upscaler_name = gr.Dropdown(
|
||||
choices=[upscaler.name for upscaler in shared.sd_upscalers],
|
||||
value=lambda: opts.data.get("faceswaplab_pp_default_upscaler", "None"),
|
||||
value=lambda: get_sd_option("faceswaplab_pp_default_upscaler", "None"),
|
||||
label="Upscaler",
|
||||
elem_id="faceswaplab_pp_upscaler",
|
||||
)
|
||||
@@ -60,7 +60,7 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
upscaler_visibility = gr.Slider(
|
||||
0,
|
||||
1,
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_pp_default_upscaler_visibility", 1
|
||||
),
|
||||
step=0.1,
|
||||
@@ -87,21 +87,21 @@ def postprocessing_ui() -> List[gr.components.Component]:
|
||||
)
|
||||
|
||||
inpainting_denoising_prompt = gr.Textbox(
|
||||
opts.data.get(
|
||||
get_sd_option(
|
||||
"faceswaplab_pp_default_inpainting_prompt", "Portrait of a [gender]"
|
||||
),
|
||||
elem_id="faceswaplab_pp_inpainting_denoising_prompt",
|
||||
label="Inpainting prompt use [gender] instead of men or woman",
|
||||
)
|
||||
inpainting_denoising_negative_prompt = gr.Textbox(
|
||||
opts.data.get(
|
||||
get_sd_option(
|
||||
"faceswaplab_pp_default_inpainting_negative_prompt", "blurry"
|
||||
),
|
||||
elem_id="faceswaplab_pp_inpainting_denoising_neg_prompt",
|
||||
label="Inpainting negative prompt use [gender] instead of men or woman",
|
||||
)
|
||||
with gr.Row():
|
||||
samplers_names = [s.name for s in modules.sd_samplers.all_samplers]
|
||||
samplers_names = [s.name for s in modules.sd_samplers.all_samplers] # type: ignore
|
||||
inpainting_sampler = gr.Dropdown(
|
||||
choices=samplers_names,
|
||||
value=[samplers_names[0]],
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import traceback
|
||||
from pprint import pformat
|
||||
from typing import *
|
||||
from scripts.faceswaplab_swapping import face_checkpoints
|
||||
from scripts.faceswaplab_utils.sd_utils import get_sd_option
|
||||
from scripts.faceswaplab_utils.typing import *
|
||||
import gradio as gr
|
||||
import onnx
|
||||
import pandas as pd
|
||||
from modules.shared import opts
|
||||
from PIL import Image
|
||||
|
||||
import scripts.faceswaplab_swapping.swapper as swapper
|
||||
@@ -15,7 +16,7 @@ from scripts.faceswaplab_postprocessing.postprocessing_options import (
|
||||
from scripts.faceswaplab_ui.faceswaplab_postprocessing_ui import postprocessing_ui
|
||||
from scripts.faceswaplab_ui.faceswaplab_unit_settings import FaceSwapUnitSettings
|
||||
from scripts.faceswaplab_ui.faceswaplab_unit_ui import faceswap_unit_ui
|
||||
from scripts.faceswaplab_utils import face_checkpoints_utils, imgutils
|
||||
from scripts.faceswaplab_utils import imgutils
|
||||
from scripts.faceswaplab_utils.faceswaplab_logging import logger
|
||||
from scripts.faceswaplab_utils.models_utils import get_swap_models
|
||||
from scripts.faceswaplab_utils.ui_utils import dataclasses_from_flat_list
|
||||
@@ -74,7 +75,7 @@ def extract_faces(
|
||||
[PostProcessingOptions], components
|
||||
).pop()
|
||||
images = [
|
||||
Image.open(file.name) for file in files
|
||||
Image.open(file.name) for file in files # type: ignore
|
||||
] # potentially greedy but Image.open is supposed to be lazy
|
||||
result_images = swapper.extract_faces(
|
||||
images, extract_path=extract_path, postprocess_options=postprocess_options
|
||||
@@ -136,7 +137,7 @@ def analyse_faces(image: PILImage, det_threshold: float = 0.5) -> Optional[str]:
|
||||
|
||||
|
||||
def build_face_checkpoint_and_save(
|
||||
batch_files: gr.File, name: str, overwrite: bool
|
||||
batch_files: List[gr.File], name: str, overwrite: bool
|
||||
) -> PILImage:
|
||||
"""
|
||||
Builds a face checkpoint using the provided image files, performs face swapping,
|
||||
@@ -154,16 +155,16 @@ def build_face_checkpoint_and_save(
|
||||
try:
|
||||
if not batch_files:
|
||||
logger.error("No face found")
|
||||
return None
|
||||
images = [Image.open(file.name) for file in batch_files]
|
||||
preview_image = face_checkpoints_utils.build_face_checkpoint_and_save(
|
||||
return None # type: ignore (Optional not really supported by old gradio)
|
||||
images = [Image.open(file.name) for file in batch_files] # type: ignore
|
||||
preview_image = face_checkpoints.build_face_checkpoint_and_save(
|
||||
images, name, overwrite=overwrite
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Failed to build checkpoint %s", e)
|
||||
|
||||
traceback.print_exc()
|
||||
return None
|
||||
return None # type: ignore
|
||||
return preview_image
|
||||
|
||||
|
||||
@@ -197,7 +198,7 @@ def explore_onnx_faceswap_model(model_path: str) -> pd.DataFrame:
|
||||
logger.error("Failed to explore model %s", e)
|
||||
|
||||
traceback.print_exc()
|
||||
return None
|
||||
return None # type: ignore
|
||||
return df
|
||||
|
||||
|
||||
@@ -205,7 +206,7 @@ def batch_process(
|
||||
files: List[gr.File], save_path: str, *components: Tuple[Any, ...]
|
||||
) -> List[PILImage]:
|
||||
try:
|
||||
units_count = opts.data.get("faceswaplab_units_count", 3)
|
||||
units_count = get_sd_option("faceswaplab_units_count", 3)
|
||||
|
||||
classes: List[Any] = dataclasses_from_flat_list(
|
||||
[FaceSwapUnitSettings] * units_count + [PostProcessingOptions],
|
||||
@@ -216,13 +217,16 @@ def batch_process(
|
||||
]
|
||||
postprocess_options = classes[-1]
|
||||
|
||||
images_paths = [file.name for file in files]
|
||||
images_paths = [file.name for file in files] # type: ignore
|
||||
|
||||
return swapper.batch_process(
|
||||
images_paths,
|
||||
save_path=save_path,
|
||||
units=units,
|
||||
postprocess_options=postprocess_options,
|
||||
return (
|
||||
swapper.batch_process(
|
||||
images_paths,
|
||||
save_path=save_path,
|
||||
units=units,
|
||||
postprocess_options=postprocess_options,
|
||||
)
|
||||
or []
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Batch Process error : %s", e)
|
||||
@@ -304,7 +308,7 @@ def tools_ui() -> None:
|
||||
label="Extracted faces",
|
||||
show_label=False,
|
||||
elem_id="faceswaplab_extract_results",
|
||||
).style(columns=[2], rows=[2])
|
||||
)
|
||||
extract_save_path = gr.Textbox(
|
||||
label="Destination Directory",
|
||||
value="",
|
||||
@@ -360,7 +364,7 @@ def tools_ui() -> None:
|
||||
label="Batch result",
|
||||
show_label=False,
|
||||
elem_id="faceswaplab_batch_results",
|
||||
).style(columns=[2], rows=[2])
|
||||
)
|
||||
batch_save_path = gr.Textbox(
|
||||
label="Destination Directory",
|
||||
value="outputs/faceswap/",
|
||||
@@ -370,7 +374,7 @@ def tools_ui() -> None:
|
||||
"Process & Save", elem_id="faceswaplab_extract_btn"
|
||||
)
|
||||
unit_components = []
|
||||
for i in range(1, opts.data.get("faceswaplab_units_count", 3) + 1):
|
||||
for i in range(1, get_sd_option("faceswaplab_units_count", 3) + 1):
|
||||
unit_components += faceswap_unit_ui(False, i, id_prefix="faceswaplab_tab")
|
||||
|
||||
upscale_options = postprocessing_ui()
|
||||
|
||||
@@ -9,7 +9,7 @@ from PIL import Image
|
||||
from scripts.faceswaplab_swapping.upcaled_inswapper_options import InswappperOptions
|
||||
from scripts.faceswaplab_utils.imgutils import pil_to_cv2
|
||||
from scripts.faceswaplab_utils.faceswaplab_logging import logger
|
||||
from scripts.faceswaplab_utils import face_checkpoints_utils
|
||||
from scripts.faceswaplab_swapping import face_checkpoints
|
||||
from scripts.faceswaplab_inpainting.faceswaplab_inpainting import InpaintingOptions
|
||||
from client_api import api_utils
|
||||
|
||||
@@ -124,7 +124,7 @@ class FaceSwapUnitSettings:
|
||||
if self.source_face and self.source_face != "None":
|
||||
try:
|
||||
logger.info(f"loading face {self.source_face}")
|
||||
face = face_checkpoints_utils.load_face(self.source_face)
|
||||
face = face_checkpoints.load_face(self.source_face)
|
||||
self._reference_face = face
|
||||
except Exception as e:
|
||||
logger.error("Failed to load checkpoint : %s", e)
|
||||
@@ -169,7 +169,7 @@ class FaceSwapUnitSettings:
|
||||
if isinstance(file, Image.Image):
|
||||
img = file
|
||||
else:
|
||||
img = Image.open(file.name)
|
||||
img = Image.open(file.name) # type: ignore
|
||||
|
||||
face = swapper.get_or_default(
|
||||
swapper.get_faces(pil_to_cv2(img)), 0, None
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from typing import List
|
||||
from scripts.faceswaplab_ui.faceswaplab_inpainting_ui import face_inpainting_ui
|
||||
from scripts.faceswaplab_utils.face_checkpoints_utils import get_face_checkpoints
|
||||
from scripts.faceswaplab_swapping.face_checkpoints import get_face_checkpoints
|
||||
import gradio as gr
|
||||
from modules.shared import opts
|
||||
from modules import shared
|
||||
from scripts.faceswaplab_utils.sd_utils import get_sd_option
|
||||
|
||||
|
||||
def faceswap_unit_advanced_options(
|
||||
@@ -17,7 +17,7 @@ def faceswap_unit_advanced_options(
|
||||
face_restorer_name = gr.Radio(
|
||||
label="Restore Face",
|
||||
choices=["None"] + [x.name() for x in shared.face_restorers],
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_face_restorer",
|
||||
"None",
|
||||
),
|
||||
@@ -28,7 +28,7 @@ def faceswap_unit_advanced_options(
|
||||
face_restorer_visibility = gr.Slider(
|
||||
0,
|
||||
1,
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_face_restorer_visibility",
|
||||
1.0,
|
||||
),
|
||||
@@ -39,7 +39,7 @@ def faceswap_unit_advanced_options(
|
||||
codeformer_weight = gr.Slider(
|
||||
0,
|
||||
1,
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_face_restorer_weight", 1.0
|
||||
),
|
||||
step=0.001,
|
||||
@@ -48,7 +48,7 @@ def faceswap_unit_advanced_options(
|
||||
)
|
||||
upscaler_name = gr.Dropdown(
|
||||
choices=[upscaler.name for upscaler in shared.sd_upscalers],
|
||||
value=lambda: opts.data.get(
|
||||
value=lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_upscaler", ""
|
||||
),
|
||||
label="Upscaler",
|
||||
@@ -56,7 +56,7 @@ def faceswap_unit_advanced_options(
|
||||
)
|
||||
|
||||
improved_mask = gr.Checkbox(
|
||||
lambda: opts.data.get(
|
||||
lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_improved_mask", False
|
||||
),
|
||||
interactive=True,
|
||||
@@ -64,7 +64,7 @@ def faceswap_unit_advanced_options(
|
||||
elem_id=f"{id_prefix}_face{unit_num}_improved_mask",
|
||||
)
|
||||
color_corrections = gr.Checkbox(
|
||||
lambda: opts.data.get(
|
||||
lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_fixcolor", False
|
||||
),
|
||||
interactive=True,
|
||||
@@ -72,7 +72,7 @@ def faceswap_unit_advanced_options(
|
||||
elem_id=f"{id_prefix}_face{unit_num}_color_corrections",
|
||||
)
|
||||
sharpen_face = gr.Checkbox(
|
||||
lambda: opts.data.get(
|
||||
lambda: get_sd_option(
|
||||
"faceswaplab_default_upscaled_swapper_sharpen", False
|
||||
),
|
||||
interactive=True,
|
||||
@@ -82,7 +82,7 @@ def faceswap_unit_advanced_options(
|
||||
erosion_factor = gr.Slider(
|
||||
0.0,
|
||||
10.0,
|
||||
lambda: opts.data.get("faceswaplab_default_upscaled_swapper_erosion", 1.0),
|
||||
lambda: get_sd_option("faceswaplab_default_upscaled_swapper_erosion", 1.0),
|
||||
step=0.01,
|
||||
label="Upscaled swapper mask erosion factor, 1 = default behaviour.",
|
||||
elem_id=f"{id_prefix}_face{unit_num}_erosion_factor",
|
||||
|
||||
Reference in New Issue
Block a user