From 565704285b37d395e596341164a0805f152dca4a Mon Sep 17 00:00:00 2001
From: Art Gourieff <85128026+Gourieff@users.noreply.github.com>
Date: Fri, 1 Mar 2024 20:50:52 +0700
Subject: [PATCH] UPDATE: XYZ Script support
FR: #329
+VersionUP (0.7.0 beta1)
---
README.md | 10 +++++-
README_RU.md | 10 +++++-
scripts/reactor_api.py | 11 +++++-
scripts/reactor_faceswap.py | 5 +++
scripts/reactor_version.py | 2 +-
scripts/reactor_xyz.py | 71 +++++++++++++++++++++++++++++++++++++
6 files changed, 105 insertions(+), 4 deletions(-)
create mode 100644 scripts/reactor_xyz.py
diff --git a/README.md b/README.md
index 61d4038..79eac55 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
- 
+ 
@@ -43,6 +43,14 @@
Click to expand
+### 0.7.0 BETA1
+
+- X/Y/Z Script support (up to 3 axis: CodeFormer Weight, Restorer Visibility, Face Mask Correction)
+
+
+
+Full size demo image: [xyz_demo.png](https://raw.githubusercontent.com/Gourieff/Assets/main/sd-webui-reactor/xyz_demo.png)
+
### 0.7.0 ALPHA1
- You can now blend faces to build blended face models ("Tools->Face Models->Blend") - due to popular demand
diff --git a/README_RU.md b/README_RU.md
index 5e4d89a..201255b 100644
--- a/README_RU.md
+++ b/README_RU.md
@@ -2,7 +2,7 @@
- 
+ 
@@ -42,6 +42,14 @@
Нажмите, чтобы посмотреть
+### 0.7.0 BETA1
+
+- Поддержка X/Y/Z скрипта (до 3-х параметров: CodeFormer Weight, Restorer Visibility, Face Mask Correction)
+
+
+
+Full size demo image: [xyz_demo.png](https://raw.githubusercontent.com/Gourieff/Assets/main/sd-webui-reactor/xyz_demo.png)
+
### 0.7.0 ALPHA1
- По многочисленным просьбам появилась возможность строить смешанные модели лиц ("Tools->Face Models->Blend")
diff --git a/scripts/reactor_api.py b/scripts/reactor_api.py
index 7991d81..20bb925 100644
--- a/scripts/reactor_api.py
+++ b/scripts/reactor_api.py
@@ -1,7 +1,7 @@
'''
Thanks SpenserCai for the original version of the roop api script
-----------------------------------
---- ReActor External API v1.0.5 ---
+--- ReActor External API v1.0.6 ---
-----------------------------------
'''
import os, glob
@@ -17,6 +17,15 @@ import gradio as gr
from scripts.reactor_swapper import EnhancementOptions, swap_face, DetectionOptions
from scripts.reactor_logger import logger
+# XYZ init:
+from scripts.reactor_xyz import run
+try:
+ import modules.script_callbacks as script_callbacks
+ script_callbacks.on_before_ui(run)
+ # script_callbacks.on_app_started(reactor_api)
+except:
+ pass
+
def default_file_path():
time = datetime.now()
diff --git a/scripts/reactor_faceswap.py b/scripts/reactor_faceswap.py
index 6f21fb5..428f3cf 100644
--- a/scripts/reactor_faceswap.py
+++ b/scripts/reactor_faceswap.py
@@ -251,6 +251,11 @@ class FaceSwapScript(scripts.Script):
self.random_image = False
if self.upscale_force is None:
self.upscale_force = False
+
+ if shared.state.job_count > 0:
+ self.face_restorer_visibility = shared.opts.data['restorer_visibility'] if 'restorer_visibility' in shared.opts.data.keys() else 1
+ self.codeformer_weight = shared.opts.data['codeformer_weight'] if 'codeformer_weight' in shared.opts.data.keys() else 0.5
+ self.mask_face = shared.opts.data['mask_face'] if 'mask_face' in shared.opts.data.keys() else False
logger.debug("*** Set Device")
set_Device(self.device)
diff --git a/scripts/reactor_version.py b/scripts/reactor_version.py
index 302ca2e..8de88e0 100644
--- a/scripts/reactor_version.py
+++ b/scripts/reactor_version.py
@@ -1,5 +1,5 @@
app_title = "ReActor"
-version_flag = "v0.7.0-a2"
+version_flag = "v0.7.0-b1"
from scripts.reactor_logger import logger, get_Run, set_Run
from scripts.reactor_globals import DEVICE
diff --git a/scripts/reactor_xyz.py b/scripts/reactor_xyz.py
new file mode 100644
index 0000000..52a801a
--- /dev/null
+++ b/scripts/reactor_xyz.py
@@ -0,0 +1,71 @@
+'''
+Thanks @ledahu for contributing
+'''
+
+from modules import scripts
+from modules.shared import opts
+
+# xyz_grid = [x for x in scripts.scripts_data if x.script_class.__module__ == "xyz_grid.py"][0].module
+
+def find_module(module_names):
+ if isinstance(module_names, str):
+ module_names = [s.strip() for s in module_names.split(",")]
+ for data in scripts.scripts_data:
+ if data.script_class.__module__ in module_names and hasattr(data, "module"):
+ return data.module
+ return None
+
+def bool_(string):
+ string = str(string)
+ if string in ["None", ""]:
+ return None
+ elif string.lower() in ["true", "1"]:
+ return True
+ elif string.lower() in ["false", "0"]:
+ return False
+ else:
+ raise ValueError(f"Could not convert string to boolean: {string}")
+
+def choices_bool():
+ return ["False", "True"]
+
+def float_applier(value_name:str, min_range:float = 0, max_range:float = 1):
+ """
+ Returns a function that applies the given value to the given value_name in opts.data.
+ """
+ def validate(value_name:str, value:str):
+ value = float(value)
+ # validate value
+ if not min_range == 0:
+ assert value >= min_range, f"Value {value} for {value_name} must be greater than or equal to {min_range}"
+ if not max_range == 1:
+ assert value <= max_range, f"Value {value} for {value_name} must be less than or equal to {max_range}"
+ def apply_float(p, x, xs):
+ validate(value_name, x)
+ opts.data[value_name] = float(x)
+ return apply_float
+
+def bool_applier(value_name:str):
+ def apply_bool__(p, x, xs):
+ x_normed = bool_(x)
+ opts.data[value_name] = x_normed
+ # print(f'normed = {x_normed}')
+ return apply_bool__
+
+def add_axis_options(xyz_grid):
+ extra_axis_options = [
+ xyz_grid.AxisOption("[ReActor] CodeFormer Weight", float, float_applier("codeformer_weight", 0, 1)),
+ xyz_grid.AxisOption("[ReActor] Restorer Visibility", float, float_applier("restorer_visibility", 0, 1)),
+ xyz_grid.AxisOption("[ReActor] Face Mask Correction", str, bool_applier("mask_face"), choices=choices_bool),
+ ]
+ set_a = {opt.label for opt in xyz_grid.axis_options}
+ set_b = {opt.label for opt in extra_axis_options}
+ if set_a.intersection(set_b):
+ return
+
+ xyz_grid.axis_options.extend(extra_axis_options)
+
+def run():
+ xyz_grid = find_module("xyz_grid.py, xy_grid.py")
+ if xyz_grid:
+ add_axis_options(xyz_grid)