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|
|
|
|
|
|
|
|
|
| 1 |
+
import os, glob, sys
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
import torchvision.transforms as T
|
| 7 |
+
from torchvision.transforms.functional import normalize
|
| 8 |
+
from torchvision.ops import masks_to_boxes
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import cv2
|
| 12 |
+
import math
|
| 13 |
+
from typing import List
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import io
|
| 16 |
+
from scipy import stats
|
| 17 |
+
from insightface.app.common import Face
|
| 18 |
+
from segment_anything import sam_model_registry
|
| 19 |
+
|
| 20 |
+
from modules.processing import ProcessingImg2Img
|
| 21 |
+
from modules.shared import state
|
| 22 |
+
# from comfy_extras.chainner_models import model_loading
|
| 23 |
+
import comfy.model_management as model_management
|
| 24 |
+
import comfy.utils
|
| 25 |
+
import folder_paths
|
| 26 |
+
|
| 27 |
+
import scripts.reactor_version
|
| 28 |
+
from r_chainner import model_loading
|
| 29 |
+
from scripts.reactor_faceswap import (
|
| 30 |
+
FaceSwapScript,
|
| 31 |
+
get_models,
|
| 32 |
+
get_current_faces_model,
|
| 33 |
+
analyze_faces,
|
| 34 |
+
half_det_size,
|
| 35 |
+
providers
|
| 36 |
+
)
|
| 37 |
+
from scripts.reactor_swapper import (
|
| 38 |
+
unload_all_models,
|
| 39 |
+
)
|
| 40 |
+
from scripts.reactor_logger import logger
|
| 41 |
+
from reactor_utils import (
|
| 42 |
+
batch_tensor_to_pil,
|
| 43 |
+
batched_pil_to_tensor,
|
| 44 |
+
tensor_to_pil,
|
| 45 |
+
img2tensor,
|
| 46 |
+
tensor2img,
|
| 47 |
+
save_face_model,
|
| 48 |
+
load_face_model,
|
| 49 |
+
download,
|
| 50 |
+
set_ort_session,
|
| 51 |
+
prepare_cropped_face,
|
| 52 |
+
normalize_cropped_face,
|
| 53 |
+
add_folder_path_and_extensions,
|
| 54 |
+
rgba2rgb_tensor,
|
| 55 |
+
progress_bar,
|
| 56 |
+
progress_bar_reset
|
| 57 |
+
)
|
| 58 |
+
from reactor_patcher import apply_patch
|
| 59 |
+
from r_facelib.utils.face_restoration_helper import FaceRestoreHelper
|
| 60 |
+
from r_basicsr.utils.registry import ARCH_REGISTRY
|
| 61 |
+
import scripts.r_archs.codeformer_arch
|
| 62 |
+
import scripts.r_masking.subcore as subcore
|
| 63 |
+
import scripts.r_masking.core as core
|
| 64 |
+
import scripts.r_masking.segs as masking_segs
|
| 65 |
+
|
| 66 |
+
import scripts.reactor_sfw as sfw
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
models_dir = folder_paths.models_dir
|
| 70 |
+
REACTOR_MODELS_PATH = os.path.join(models_dir, "reactor")
|
| 71 |
+
FACE_MODELS_PATH = os.path.join(REACTOR_MODELS_PATH, "faces")
|
| 72 |
+
NSFWDET_MODEL_PATH = os.path.join(models_dir, "nsfw_detector","vit-base-nsfw-detector")
|
| 73 |
+
|
| 74 |
+
if not os.path.exists(REACTOR_MODELS_PATH):
|
| 75 |
+
os.makedirs(REACTOR_MODELS_PATH)
|
| 76 |
+
if not os.path.exists(FACE_MODELS_PATH):
|
| 77 |
+
os.makedirs(FACE_MODELS_PATH)
|
| 78 |
+
|
| 79 |
+
dir_facerestore_models = os.path.join(models_dir, "facerestore_models")
|
| 80 |
+
os.makedirs(dir_facerestore_models, exist_ok=True)
|
| 81 |
+
folder_paths.folder_names_and_paths["facerestore_models"] = ([dir_facerestore_models], folder_paths.supported_pt_extensions)
|
| 82 |
+
|
| 83 |
+
BLENDED_FACE_MODEL = None
|
| 84 |
+
FACE_SIZE: int = 512
|
| 85 |
+
FACE_HELPER = None
|
| 86 |
+
|
| 87 |
+
if "ultralytics" not in folder_paths.folder_names_and_paths:
|
| 88 |
+
add_folder_path_and_extensions("ultralytics_bbox", [os.path.join(models_dir, "ultralytics", "bbox")], folder_paths.supported_pt_extensions)
|
| 89 |
+
add_folder_path_and_extensions("ultralytics_segm", [os.path.join(models_dir, "ultralytics", "segm")], folder_paths.supported_pt_extensions)
|
| 90 |
+
add_folder_path_and_extensions("ultralytics", [os.path.join(models_dir, "ultralytics")], folder_paths.supported_pt_extensions)
|
| 91 |
+
if "sams" not in folder_paths.folder_names_and_paths:
|
| 92 |
+
add_folder_path_and_extensions("sams", [os.path.join(models_dir, "sams")], folder_paths.supported_pt_extensions)
|
| 93 |
+
|
| 94 |
+
def get_facemodels():
|
| 95 |
+
models_path = os.path.join(FACE_MODELS_PATH, "*")
|
| 96 |
+
models = glob.glob(models_path)
|
| 97 |
+
models = [x for x in models if x.endswith(".safetensors")]
|
| 98 |
+
return models
|
| 99 |
+
|
| 100 |
+
def get_restorers():
|
| 101 |
+
models_path = os.path.join(models_dir, "facerestore_models/*")
|
| 102 |
+
models = glob.glob(models_path)
|
| 103 |
+
models = [x for x in models if (x.endswith(".pth") or x.endswith(".onnx"))]
|
| 104 |
+
if len(models) == 0:
|
| 105 |
+
fr_urls = [
|
| 106 |
+
"https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GFPGANv1.3.pth",
|
| 107 |
+
"https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GFPGANv1.4.pth",
|
| 108 |
+
"https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/codeformer-v0.1.0.pth",
|
| 109 |
+
"https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GPEN-BFR-512.onnx",
|
| 110 |
+
]
|
| 111 |
+
for model_url in fr_urls:
|
| 112 |
+
model_name = os.path.basename(model_url)
|
| 113 |
+
model_path = os.path.join(dir_facerestore_models, model_name)
|
| 114 |
+
download(model_url, model_path, model_name)
|
| 115 |
+
models = glob.glob(models_path)
|
| 116 |
+
models = [x for x in models if (x.endswith(".pth") or x.endswith(".onnx"))]
|
| 117 |
+
return models
|
| 118 |
+
|
| 119 |
+
def get_model_names(get_models):
|
| 120 |
+
models = get_models()
|
| 121 |
+
names = []
|
| 122 |
+
for x in models:
|
| 123 |
+
names.append(os.path.basename(x))
|
| 124 |
+
names.sort(key=str.lower)
|
| 125 |
+
names.insert(0, "none")
|
| 126 |
+
return names
|
| 127 |
+
|
| 128 |
+
def model_names():
|
| 129 |
+
models = get_models()
|
| 130 |
+
return {os.path.basename(x): x for x in models}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class reactor:
|
| 134 |
+
@classmethod
|
| 135 |
+
def INPUT_TYPES(s):
|
| 136 |
+
return {
|
| 137 |
+
"required": {
|
| 138 |
+
"enabled": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
|
| 139 |
+
"input_image": ("IMAGE",),
|
| 140 |
+
"swap_model": (list(model_names().keys()),),
|
| 141 |
+
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
|
| 142 |
+
"face_restore_model": (get_model_names(get_restorers),),
|
| 143 |
+
"face_restore_visibility": ("FLOAT", {"default": 1, "min": 0.1, "max": 1, "step": 0.05}),
|
| 144 |
+
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
|
| 145 |
+
"detect_gender_input": (["no","female","male"], {"default": "no"}),
|
| 146 |
+
"detect_gender_source": (["no","female","male"], {"default": "no"}),
|
| 147 |
+
"input_faces_index": ("STRING", {"default": "0"}),
|
| 148 |
+
"source_faces_index": ("STRING", {"default": "0"}),
|
| 149 |
+
"console_log_level": ([0, 1, 2], {"default": 1}),
|
| 150 |
+
},
|
| 151 |
+
"optional": {
|
| 152 |
+
"source_image": ("IMAGE",),
|
| 153 |
+
"face_model": ("FACE_MODEL",),
|
| 154 |
+
"face_boost": ("FACE_BOOST",),
|
| 155 |
+
},
|
| 156 |
+
"hidden": {"faces_order": "FACES_ORDER"},
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
RETURN_TYPES = ("IMAGE","FACE_MODEL","IMAGE")
|
| 160 |
+
RETURN_NAMES = ("SWAPPED_IMAGE","FACE_MODEL","ORIGINAL_IMAGE")
|
| 161 |
+
FUNCTION = "execute"
|
| 162 |
+
CATEGORY = "π ReActor"
|
| 163 |
+
|
| 164 |
+
def __init__(self):
|
| 165 |
+
# self.face_helper = None
|
| 166 |
+
self.faces_order = ["large-small", "large-small"]
|
| 167 |
+
# self.face_size = FACE_SIZE
|
| 168 |
+
self.face_boost_enabled = False
|
| 169 |
+
self.restore = True
|
| 170 |
+
self.boost_model = None
|
| 171 |
+
self.interpolation = "Bicubic"
|
| 172 |
+
self.boost_model_visibility = 1
|
| 173 |
+
self.boost_cf_weight = 0.5
|
| 174 |
+
self.last_swapped_bboxes = None
|
| 175 |
+
# self.last_swapped_indices = None
|
| 176 |
+
self.restore_swapped_only = True
|
| 177 |
+
|
| 178 |
+
def restore_face(
|
| 179 |
+
self,
|
| 180 |
+
input_image,
|
| 181 |
+
face_restore_model,
|
| 182 |
+
face_restore_visibility,
|
| 183 |
+
codeformer_weight,
|
| 184 |
+
facedetection,
|
| 185 |
+
):
|
| 186 |
+
|
| 187 |
+
# from datetime import datetime
|
| 188 |
+
# def _current_time():
|
| 189 |
+
# current_datetime = datetime.now()
|
| 190 |
+
# return current_datetime.strftime("%M:%S")
|
| 191 |
+
|
| 192 |
+
# Π»ΠΎΠΊΠ°Π»ΡΠ½Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ IoU
|
| 193 |
+
def _iou(b1, b2):
|
| 194 |
+
xA = max(b1[0], b2[0])
|
| 195 |
+
yA = max(b1[1], b2[1])
|
| 196 |
+
xB = min(b1[2], b2[2])
|
| 197 |
+
yB = min(b1[3], b2[3])
|
| 198 |
+
interW = max(0.0, xB - xA)
|
| 199 |
+
interH = max(0.0, yB - yA)
|
| 200 |
+
interArea = interW * interH
|
| 201 |
+
if interArea == 0:
|
| 202 |
+
return 0.0
|
| 203 |
+
boxAArea = max(1.0, (b1[2]-b1[0]) * (b1[3]-b1[1]))
|
| 204 |
+
boxBArea = max(1.0, (b2[2]-b2[0]) * (b2[3]-b2[1]))
|
| 205 |
+
return interArea / float(boxAArea + boxBArea - interArea)
|
| 206 |
+
|
| 207 |
+
result = input_image
|
| 208 |
+
|
| 209 |
+
if face_restore_model != "none" and not model_management.processing_interrupted():
|
| 210 |
+
|
| 211 |
+
global FACE_SIZE, FACE_HELPER
|
| 212 |
+
|
| 213 |
+
self.face_helper = FACE_HELPER
|
| 214 |
+
|
| 215 |
+
faceSize = 512
|
| 216 |
+
if "1024" in face_restore_model.lower():
|
| 217 |
+
faceSize = 1024
|
| 218 |
+
elif "2048" in face_restore_model.lower():
|
| 219 |
+
faceSize = 2048
|
| 220 |
+
|
| 221 |
+
logger.status(f"Restoring with {face_restore_model} | Face Size is set to {faceSize}")
|
| 222 |
+
|
| 223 |
+
model_path = folder_paths.get_full_path("facerestore_models", face_restore_model)
|
| 224 |
+
|
| 225 |
+
device = model_management.get_torch_device()
|
| 226 |
+
|
| 227 |
+
if "codeformer" in face_restore_model.lower():
|
| 228 |
+
|
| 229 |
+
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(
|
| 230 |
+
dim_embd=512,
|
| 231 |
+
codebook_size=1024,
|
| 232 |
+
n_head=8,
|
| 233 |
+
n_layers=9,
|
| 234 |
+
connect_list=["32", "64", "128", "256"],
|
| 235 |
+
).to(device)
|
| 236 |
+
checkpoint = torch.load(model_path)["params_ema"]
|
| 237 |
+
codeformer_net.load_state_dict(checkpoint)
|
| 238 |
+
facerestore_model = codeformer_net.eval()
|
| 239 |
+
|
| 240 |
+
elif ".onnx" in face_restore_model:
|
| 241 |
+
|
| 242 |
+
ort_session = set_ort_session(model_path, providers=providers)
|
| 243 |
+
ort_session_inputs = {}
|
| 244 |
+
facerestore_model = ort_session
|
| 245 |
+
|
| 246 |
+
else:
|
| 247 |
+
|
| 248 |
+
sd = comfy.utils.load_torch_file(model_path, safe_load=True)
|
| 249 |
+
facerestore_model = model_loading.load_state_dict(sd).eval()
|
| 250 |
+
facerestore_model.to(device)
|
| 251 |
+
|
| 252 |
+
if faceSize != FACE_SIZE or self.face_helper is None:
|
| 253 |
+
self.face_helper = FaceRestoreHelper(1, face_size=faceSize, crop_ratio=(1, 1), det_model=facedetection, save_ext='png', use_parse=True, device=device)
|
| 254 |
+
FACE_SIZE = faceSize
|
| 255 |
+
FACE_HELPER = self.face_helper
|
| 256 |
+
|
| 257 |
+
# Copying Tensor to CPU (if it isn't) to convert torch.Tensor to np.ndarray
|
| 258 |
+
image_np = 255. * result.cpu().numpy()
|
| 259 |
+
|
| 260 |
+
total_images = image_np.shape[0]
|
| 261 |
+
|
| 262 |
+
out_images = []
|
| 263 |
+
|
| 264 |
+
pbar = progress_bar(total_images)
|
| 265 |
+
|
| 266 |
+
for i in range(total_images):
|
| 267 |
+
|
| 268 |
+
# if total_images > 1:
|
| 269 |
+
# logger.status(f"Restoring {i}")
|
| 270 |
+
|
| 271 |
+
cur_image_np = image_np[i,:, :, ::-1]
|
| 272 |
+
|
| 273 |
+
original_resolution = cur_image_np.shape[0:2]
|
| 274 |
+
|
| 275 |
+
if facerestore_model is None or self.face_helper is None:
|
| 276 |
+
return result
|
| 277 |
+
|
| 278 |
+
self.face_helper.clean_all()
|
| 279 |
+
self.face_helper.read_image(cur_image_np)
|
| 280 |
+
self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
|
| 281 |
+
self.face_helper.align_warp_face()
|
| 282 |
+
|
| 283 |
+
# restored_face = None
|
| 284 |
+
restored_faces = []
|
| 285 |
+
|
| 286 |
+
# Π±Π΅ΡΠ΅ΠΌ ΡΠΎΡ
ΡΠ°Π½ΡΠ½Π½ΡΠ΅ bbox ΠΈΠ· swap (ΠΈΠ»ΠΈ None)
|
| 287 |
+
swapped_bboxes = getattr(self, "last_swapped_bboxes", None)
|
| 288 |
+
# ΡΠ»Π°Π³ΠΈ, ΡΡΠΎΠ±Ρ ΠΎΠ΄Π½ΠΎ ΡΠΎΡ
ΡΠ°Π½ΡΠ½Π½ΠΎΠ΅ bbox Π½Π΅ ΡΠΎΠ²ΠΏΠ°Π»ΠΎ Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΠΌΠΈ Π»ΠΈΡΠ°ΠΌΠΈ
|
| 289 |
+
used_swapped = [False] * len(swapped_bboxes) if swapped_bboxes else None
|
| 290 |
+
|
| 291 |
+
IOU_THRESHOLD = 0.5
|
| 292 |
+
|
| 293 |
+
for idx, cropped_face in enumerate(self.face_helper.cropped_faces):
|
| 294 |
+
|
| 295 |
+
# ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ bbox ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ Π»ΠΈΡΠ°, ΠΊΠΎΡΠΎΡΡΠΉ Π΄Π°Π» Π΄Π΅ΡΠ΅ΠΊΡΠΎΡ Π²Π½ΡΡΡΠΈ FaceRestoreHelper
|
| 296 |
+
current_bbox = None
|
| 297 |
+
if hasattr(self.face_helper, 'det_faces') and len(self.face_helper.det_faces) > idx:
|
| 298 |
+
det = self.face_helper.det_faces[idx]
|
| 299 |
+
# det ΠΌ.Π±. [x1,y1,x2,y2,score]
|
| 300 |
+
current_bbox = (float(det[0]), float(det[1]), float(det[2]), float(det[3]))
|
| 301 |
+
|
| 302 |
+
# Π»ΠΎΠ³ΠΈΠΊΠ°: Π΅ΡΠ»ΠΈ Ρ Π½Π°Ρ Π΅ΡΡΡ ΡΠΎΡ
ΡΠ°Π½ΡΠ½Π½ΡΠ΅ bbox β ΡΠ΅ΡΡΠΎΡΠΈΠΌ ΡΠΎΠ»ΡΠΊΠΎ Π΅ΡΠ»ΠΈ iou Ρ ΠΎΠ΄Π½ΠΈΠΌ ΠΈΠ· Π½ΠΈΡ
> ΠΏΠΎΡΠΎΠ³
|
| 303 |
+
do_restore = True
|
| 304 |
+
# if self.last_swapped_indices is not None:
|
| 305 |
+
# do_restore = idx in self.last_swapped_indices
|
| 306 |
+
if swapped_bboxes and self.restore_swapped_only:
|
| 307 |
+
do_restore = False
|
| 308 |
+
if current_bbox is not None:
|
| 309 |
+
for s_idx, sbox in enumerate(swapped_bboxes):
|
| 310 |
+
if used_swapped is not None and used_swapped[s_idx]:
|
| 311 |
+
continue
|
| 312 |
+
if _iou(current_bbox, sbox) >= IOU_THRESHOLD:
|
| 313 |
+
cx1 = (current_bbox[0] + current_bbox[2]) / 2
|
| 314 |
+
cy1 = (current_bbox[1] + current_bbox[3]) / 2
|
| 315 |
+
cx2 = (sbox[0] + sbox[2]) / 2
|
| 316 |
+
cy2 = (sbox[1] + sbox[3]) / 2
|
| 317 |
+
if abs(cx1 - cx2) < (current_bbox[2] - current_bbox[0]) * 0.25:
|
| 318 |
+
if abs(cy1 - cy2) < (current_bbox[3] - current_bbox[1]) * 0.25:
|
| 319 |
+
do_restore = True
|
| 320 |
+
# do_restore = True
|
| 321 |
+
if used_swapped is not None:
|
| 322 |
+
used_swapped[s_idx] = True
|
| 323 |
+
break
|
| 324 |
+
|
| 325 |
+
if do_restore:
|
| 326 |
+
|
| 327 |
+
# if ".pth" in face_restore_model:
|
| 328 |
+
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
| 329 |
+
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
| 330 |
+
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
|
| 331 |
+
|
| 332 |
+
try:
|
| 333 |
+
|
| 334 |
+
with torch.no_grad():
|
| 335 |
+
|
| 336 |
+
if ".onnx" in face_restore_model: # ONNX models
|
| 337 |
+
|
| 338 |
+
for ort_session_input in ort_session.get_inputs():
|
| 339 |
+
if ort_session_input.name == "input":
|
| 340 |
+
cropped_face_prep = prepare_cropped_face(cropped_face)
|
| 341 |
+
ort_session_inputs[ort_session_input.name] = cropped_face_prep
|
| 342 |
+
if ort_session_input.name == "weight":
|
| 343 |
+
weight = np.array([ 1 ], dtype = np.double)
|
| 344 |
+
ort_session_inputs[ort_session_input.name] = weight
|
| 345 |
+
|
| 346 |
+
output = ort_session.run(None, ort_session_inputs)[0][0]
|
| 347 |
+
restored_face = normalize_cropped_face(output)
|
| 348 |
+
|
| 349 |
+
else: # PTH models
|
| 350 |
+
|
| 351 |
+
output = facerestore_model(cropped_face_t, w=codeformer_weight)[0] if "codeformer" in face_restore_model.lower() else facerestore_model(cropped_face_t)[0]
|
| 352 |
+
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
| 353 |
+
|
| 354 |
+
del output
|
| 355 |
+
torch.cuda.empty_cache()
|
| 356 |
+
|
| 357 |
+
except Exception as error:
|
| 358 |
+
|
| 359 |
+
print(f"\tFailed inference: {error}", file=sys.stderr)
|
| 360 |
+
# restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
|
| 361 |
+
restored_face = cropped_face.copy()
|
| 362 |
+
|
| 363 |
+
else:
|
| 364 |
+
restored_face = cropped_face.copy()
|
| 365 |
+
|
| 366 |
+
if face_restore_visibility < 1:
|
| 367 |
+
restored_face = cropped_face * (1 - face_restore_visibility) + restored_face * face_restore_visibility
|
| 368 |
+
|
| 369 |
+
restored_face = restored_face.astype("uint8")
|
| 370 |
+
self.face_helper.add_restored_face(restored_face)
|
| 371 |
+
|
| 372 |
+
self.face_helper.get_inverse_affine(None)
|
| 373 |
+
|
| 374 |
+
restored_img = self.face_helper.paste_faces_to_input_image()
|
| 375 |
+
restored_img = restored_img[:, :, ::-1]
|
| 376 |
+
|
| 377 |
+
if original_resolution != restored_img.shape[0:2]:
|
| 378 |
+
restored_img = cv2.resize(restored_img, (0, 0), fx=original_resolution[1]/restored_img.shape[1], fy=original_resolution[0]/restored_img.shape[0], interpolation=cv2.INTER_AREA)
|
| 379 |
+
|
| 380 |
+
self.face_helper.clean_all()
|
| 381 |
+
|
| 382 |
+
# out_images[i] = restored_img
|
| 383 |
+
out_images.append(restored_img)
|
| 384 |
+
|
| 385 |
+
if state.interrupted or model_management.processing_interrupted():
|
| 386 |
+
logger.status("Interrupted by User")
|
| 387 |
+
return input_image
|
| 388 |
+
|
| 389 |
+
pbar.update(1)
|
| 390 |
+
|
| 391 |
+
restored_img_np = np.array(out_images).astype(np.float32) / 255.0
|
| 392 |
+
restored_img_tensor = torch.from_numpy(restored_img_np)
|
| 393 |
+
|
| 394 |
+
result = restored_img_tensor
|
| 395 |
+
|
| 396 |
+
progress_bar_reset(pbar)
|
| 397 |
+
|
| 398 |
+
# if hasattr(self, "last_swapped_bboxes"):
|
| 399 |
+
self.last_swapped_bboxes = None
|
| 400 |
+
# if hasattr(self, "last_swapped_indices"):
|
| 401 |
+
# self.last_swapped_indices = None
|
| 402 |
+
|
| 403 |
+
return result
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def execute(self, enabled, input_image, swap_model, detect_gender_source, detect_gender_input, source_faces_index, input_faces_index, console_log_level, face_restore_model,face_restore_visibility, codeformer_weight, facedetection, source_image=None, face_model=None, faces_order=None, face_boost=None):
|
| 407 |
+
|
| 408 |
+
device = model_management.get_torch_device()
|
| 409 |
+
|
| 410 |
+
if isinstance(input_image, torch.Tensor) and input_image.device != device:
|
| 411 |
+
input_image = input_image.to(device)
|
| 412 |
+
|
| 413 |
+
if face_boost is not None:
|
| 414 |
+
self.face_boost_enabled = face_boost["enabled"]
|
| 415 |
+
self.boost_model = face_boost["boost_model"]
|
| 416 |
+
self.interpolation = face_boost["interpolation"]
|
| 417 |
+
self.boost_model_visibility = face_boost["visibility"]
|
| 418 |
+
self.boost_cf_weight = face_boost["codeformer_weight"]
|
| 419 |
+
self.restore = face_boost["restore_with_main_after"]
|
| 420 |
+
else:
|
| 421 |
+
self.face_boost_enabled = False
|
| 422 |
+
|
| 423 |
+
if faces_order is None:
|
| 424 |
+
faces_order = self.faces_order
|
| 425 |
+
|
| 426 |
+
apply_patch(console_log_level)
|
| 427 |
+
|
| 428 |
+
if not enabled:
|
| 429 |
+
return (input_image,face_model)
|
| 430 |
+
elif source_image is None and face_model is None:
|
| 431 |
+
logger.error("Please provide 'source_image' or `face_model`")
|
| 432 |
+
return (input_image,face_model)
|
| 433 |
+
|
| 434 |
+
if face_model == "none":
|
| 435 |
+
face_model = None
|
| 436 |
+
|
| 437 |
+
script = FaceSwapScript()
|
| 438 |
+
pil_images = batch_tensor_to_pil(input_image)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
logger.status("Checking for any unsafe content...")
|
| 442 |
+
pbar = progress_bar(len(pil_images))
|
| 443 |
+
pil_images_sfw = []
|
| 444 |
+
for img in pil_images:
|
| 445 |
+
if state.interrupted or model_management.processing_interrupted():
|
| 446 |
+
logger.status("Interrupted by User")
|
| 447 |
+
break
|
| 448 |
+
img_byte_arr = io.BytesIO()
|
| 449 |
+
img.save(img_byte_arr, format='PNG')
|
| 450 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 451 |
+
if not sfw.nsfw_image(img_byte_arr, NSFWDET_MODEL_PATH):
|
| 452 |
+
pil_images_sfw.append(img)
|
| 453 |
+
pbar.update(1)
|
| 454 |
+
pil_images = pil_images_sfw
|
| 455 |
+
|
| 456 |
+
progress_bar_reset(pbar)
|
| 457 |
+
|
| 458 |
+
if len(pil_images) > 0:
|
| 459 |
+
|
| 460 |
+
if source_image is not None:
|
| 461 |
+
source = tensor_to_pil(source_image)
|
| 462 |
+
else:
|
| 463 |
+
source = None
|
| 464 |
+
|
| 465 |
+
p = ProcessingImg2Img(pil_images)
|
| 466 |
+
script.process(
|
| 467 |
+
p=p,
|
| 468 |
+
img=source,
|
| 469 |
+
enable=True,
|
| 470 |
+
source_faces_index=source_faces_index,
|
| 471 |
+
faces_index=input_faces_index,
|
| 472 |
+
model=swap_model,
|
| 473 |
+
swap_in_source=True,
|
| 474 |
+
swap_in_generated=True,
|
| 475 |
+
gender_source=detect_gender_source,
|
| 476 |
+
gender_target=detect_gender_input,
|
| 477 |
+
face_model=face_model,
|
| 478 |
+
faces_order=faces_order,
|
| 479 |
+
# face boost:
|
| 480 |
+
face_boost_enabled=self.face_boost_enabled,
|
| 481 |
+
face_restore_model=self.boost_model,
|
| 482 |
+
face_restore_visibility=self.boost_model_visibility,
|
| 483 |
+
codeformer_weight=self.boost_cf_weight,
|
| 484 |
+
interpolation=self.interpolation,
|
| 485 |
+
)
|
| 486 |
+
result = batched_pil_to_tensor(p.init_images)
|
| 487 |
+
# print(f"bbox={p.bbox}")
|
| 488 |
+
if len(p.bbox) > 0:
|
| 489 |
+
self.last_swapped_bboxes = p.bbox
|
| 490 |
+
# self.last_swapped_indices = p.swapped_indexes
|
| 491 |
+
original_image = input_image
|
| 492 |
+
|
| 493 |
+
if face_model is None:
|
| 494 |
+
current_face_model = get_current_faces_model()
|
| 495 |
+
face_model_to_provide = current_face_model[0] if (current_face_model is not None and len(current_face_model) > 0) else face_model
|
| 496 |
+
else:
|
| 497 |
+
face_model_to_provide = face_model
|
| 498 |
+
|
| 499 |
+
if self.restore or not self.face_boost_enabled:
|
| 500 |
+
result = reactor.restore_face(self,result,face_restore_model,face_restore_visibility,codeformer_weight,facedetection)
|
| 501 |
+
|
| 502 |
+
else:
|
| 503 |
+
image_black = Image.new("RGB", (512, 512))
|
| 504 |
+
result = batched_pil_to_tensor([image_black])
|
| 505 |
+
face_model_to_provide = None
|
| 506 |
+
original_image = result
|
| 507 |
+
|
| 508 |
+
return (result,face_model_to_provide,original_image)
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
class ReActorPlusOpt:
|
| 512 |
+
@classmethod
|
| 513 |
+
def INPUT_TYPES(s):
|
| 514 |
+
return {
|
| 515 |
+
"required": {
|
| 516 |
+
"enabled": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
|
| 517 |
+
"input_image": ("IMAGE",),
|
| 518 |
+
"swap_model": (list(model_names().keys()),),
|
| 519 |
+
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
|
| 520 |
+
"face_restore_model": (get_model_names(get_restorers),),
|
| 521 |
+
"face_restore_visibility": ("FLOAT", {"default": 1, "min": 0.1, "max": 1, "step": 0.05}),
|
| 522 |
+
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
|
| 523 |
+
},
|
| 524 |
+
"optional": {
|
| 525 |
+
"source_image": ("IMAGE",),
|
| 526 |
+
"face_model": ("FACE_MODEL",),
|
| 527 |
+
"options": ("OPTIONS",),
|
| 528 |
+
"face_boost": ("FACE_BOOST",),
|
| 529 |
+
}
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
RETURN_TYPES = ("IMAGE","FACE_MODEL","IMAGE")
|
| 533 |
+
RETURN_NAMES = ("SWAPPED_IMAGE","FACE_MODEL","ORIGINAL_IMAGE")
|
| 534 |
+
FUNCTION = "execute"
|
| 535 |
+
CATEGORY = "π ReActor"
|
| 536 |
+
|
| 537 |
+
def __init__(self):
|
| 538 |
+
# self.face_helper = None
|
| 539 |
+
self.faces_order = ["large-small", "large-small"]
|
| 540 |
+
self.detect_gender_input = "no"
|
| 541 |
+
self.detect_gender_source = "no"
|
| 542 |
+
self.input_faces_index = "0"
|
| 543 |
+
self.source_faces_index = "0"
|
| 544 |
+
self.console_log_level = 1
|
| 545 |
+
self.restore_swapped_only = True
|
| 546 |
+
# self.face_size = 512
|
| 547 |
+
self.face_boost_enabled = False
|
| 548 |
+
self.restore = True
|
| 549 |
+
self.boost_model = None
|
| 550 |
+
self.interpolation = "Bicubic"
|
| 551 |
+
self.boost_model_visibility = 1
|
| 552 |
+
self.boost_cf_weight = 0.5
|
| 553 |
+
|
| 554 |
+
def execute(self, enabled, input_image, swap_model, facedetection, face_restore_model, face_restore_visibility, codeformer_weight, source_image=None, face_model=None, options=None, face_boost=None):
|
| 555 |
+
|
| 556 |
+
if options is not None:
|
| 557 |
+
self.faces_order = [options["input_faces_order"], options["source_faces_order"]]
|
| 558 |
+
self.console_log_level = options["console_log_level"]
|
| 559 |
+
self.detect_gender_input = options["detect_gender_input"]
|
| 560 |
+
self.detect_gender_source = options["detect_gender_source"]
|
| 561 |
+
self.input_faces_index = options["input_faces_index"]
|
| 562 |
+
self.source_faces_index = options["source_faces_index"]
|
| 563 |
+
self.restore_swapped_only = options["restore_swapped_only"]
|
| 564 |
+
|
| 565 |
+
if face_boost is not None:
|
| 566 |
+
self.face_boost_enabled = face_boost["enabled"]
|
| 567 |
+
self.restore = face_boost["restore_with_main_after"]
|
| 568 |
+
else:
|
| 569 |
+
self.face_boost_enabled = False
|
| 570 |
+
|
| 571 |
+
result = reactor.execute(
|
| 572 |
+
self,enabled,input_image,swap_model,self.detect_gender_source,self.detect_gender_input,self.source_faces_index,self.input_faces_index,self.console_log_level,face_restore_model,face_restore_visibility,codeformer_weight,facedetection,source_image,face_model,self.faces_order, face_boost=face_boost
|
| 573 |
+
)
|
| 574 |
+
|
| 575 |
+
return result
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
class LoadFaceModel:
|
| 579 |
+
@classmethod
|
| 580 |
+
def INPUT_TYPES(s):
|
| 581 |
+
return {
|
| 582 |
+
"required": {
|
| 583 |
+
"face_model": (get_model_names(get_facemodels),),
|
| 584 |
+
}
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
RETURN_TYPES = ("FACE_MODEL","STRING")
|
| 588 |
+
RETURN_NAMES = ("FACE_MODEL","FACE_MODEL_NAME")
|
| 589 |
+
FUNCTION = "load_model"
|
| 590 |
+
CATEGORY = "π ReActor"
|
| 591 |
+
|
| 592 |
+
def load_model(self, face_model):
|
| 593 |
+
self.face_model = face_model
|
| 594 |
+
face_model = face_model.split(".safetensors")[0] if ".safetensors" in face_model else face_model
|
| 595 |
+
self.face_models_path = FACE_MODELS_PATH
|
| 596 |
+
if self.face_model != "none":
|
| 597 |
+
face_model_path = os.path.join(self.face_models_path, self.face_model)
|
| 598 |
+
out = load_face_model(face_model_path)
|
| 599 |
+
else:
|
| 600 |
+
out = None
|
| 601 |
+
return (out,face_model)
|
| 602 |
+
|
| 603 |
+
|
| 604 |
+
class ReActorWeight:
|
| 605 |
+
@classmethod
|
| 606 |
+
def INPUT_TYPES(s):
|
| 607 |
+
return {
|
| 608 |
+
"required": {
|
| 609 |
+
"input_image": ("IMAGE",),
|
| 610 |
+
"faceswap_weight": (["0%", "12.5%", "25%", "37.5%", "50%", "62.5%", "75%", "87.5%", "100%"], {"default": "50%"}),
|
| 611 |
+
},
|
| 612 |
+
"optional": {
|
| 613 |
+
"source_image": ("IMAGE",),
|
| 614 |
+
"face_model": ("FACE_MODEL",),
|
| 615 |
+
}
|
| 616 |
+
}
|
| 617 |
+
|
| 618 |
+
RETURN_TYPES = ("IMAGE","FACE_MODEL")
|
| 619 |
+
RETURN_NAMES = ("INPUT_IMAGE","FACE_MODEL")
|
| 620 |
+
FUNCTION = "set_weight"
|
| 621 |
+
|
| 622 |
+
OUTPUT_NODE = True
|
| 623 |
+
|
| 624 |
+
CATEGORY = "π ReActor"
|
| 625 |
+
|
| 626 |
+
def set_weight(self, input_image, faceswap_weight, face_model=None, source_image=None):
|
| 627 |
+
|
| 628 |
+
if input_image is None:
|
| 629 |
+
logger.error("Please provide `input_image`")
|
| 630 |
+
return (input_image,None)
|
| 631 |
+
|
| 632 |
+
if source_image is None and face_model is None:
|
| 633 |
+
logger.error("Please provide `source_image` or `face_model`")
|
| 634 |
+
return (input_image,None)
|
| 635 |
+
|
| 636 |
+
weight = float(faceswap_weight.split("%")[0])
|
| 637 |
+
|
| 638 |
+
images = []
|
| 639 |
+
faces = [] if face_model is None else [face_model]
|
| 640 |
+
embeddings = [] if face_model is None else [face_model.embedding]
|
| 641 |
+
|
| 642 |
+
if weight == 0:
|
| 643 |
+
images = [input_image]
|
| 644 |
+
faces = []
|
| 645 |
+
embeddings = []
|
| 646 |
+
elif weight == 100:
|
| 647 |
+
if face_model is None:
|
| 648 |
+
images = [source_image]
|
| 649 |
+
else:
|
| 650 |
+
if weight > 50:
|
| 651 |
+
images = [input_image]
|
| 652 |
+
count = round(100/(100-weight))
|
| 653 |
+
else:
|
| 654 |
+
if face_model is None:
|
| 655 |
+
images = [source_image]
|
| 656 |
+
count = round(100/(weight))
|
| 657 |
+
for i in range(count-1):
|
| 658 |
+
if weight > 50:
|
| 659 |
+
if face_model is None:
|
| 660 |
+
images.append(source_image)
|
| 661 |
+
else:
|
| 662 |
+
faces.append(face_model)
|
| 663 |
+
embeddings.append(face_model.embedding)
|
| 664 |
+
else:
|
| 665 |
+
images.append(input_image)
|
| 666 |
+
|
| 667 |
+
images_list: List[Image.Image] = []
|
| 668 |
+
|
| 669 |
+
apply_patch(1)
|
| 670 |
+
|
| 671 |
+
if len(images) > 0:
|
| 672 |
+
|
| 673 |
+
for image in images:
|
| 674 |
+
img = tensor_to_pil(image)
|
| 675 |
+
images_list.append(img)
|
| 676 |
+
|
| 677 |
+
for image in images_list:
|
| 678 |
+
face = BuildFaceModel.build_face_model(self,image)
|
| 679 |
+
if isinstance(face, str):
|
| 680 |
+
continue
|
| 681 |
+
faces.append(face)
|
| 682 |
+
embeddings.append(face.embedding)
|
| 683 |
+
|
| 684 |
+
if len(faces) > 0:
|
| 685 |
+
blended_embedding = np.mean(embeddings, axis=0)
|
| 686 |
+
blended_face = Face(
|
| 687 |
+
bbox=faces[0].bbox,
|
| 688 |
+
kps=faces[0].kps,
|
| 689 |
+
det_score=faces[0].det_score,
|
| 690 |
+
landmark_3d_68=faces[0].landmark_3d_68,
|
| 691 |
+
pose=faces[0].pose,
|
| 692 |
+
landmark_2d_106=faces[0].landmark_2d_106,
|
| 693 |
+
embedding=blended_embedding,
|
| 694 |
+
gender=faces[0].gender,
|
| 695 |
+
age=faces[0].age
|
| 696 |
+
)
|
| 697 |
+
if blended_face is None:
|
| 698 |
+
no_face_msg = "Something went wrong, please try another set of images"
|
| 699 |
+
logger.error(no_face_msg)
|
| 700 |
+
|
| 701 |
+
return (input_image,blended_face)
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
class BuildFaceModel:
|
| 705 |
+
def __init__(self):
|
| 706 |
+
self.output_dir = FACE_MODELS_PATH
|
| 707 |
+
|
| 708 |
+
@classmethod
|
| 709 |
+
def INPUT_TYPES(s):
|
| 710 |
+
return {
|
| 711 |
+
"required": {
|
| 712 |
+
"save_mode": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
|
| 713 |
+
"send_only": ("BOOLEAN", {"default": False, "label_off": "NO", "label_on": "YES"}),
|
| 714 |
+
"face_model_name": ("STRING", {"default": "default"}),
|
| 715 |
+
"compute_method": (["Mean", "Median", "Mode"], {"default": "Mean"}),
|
| 716 |
+
},
|
| 717 |
+
"optional": {
|
| 718 |
+
"images": ("IMAGE",),
|
| 719 |
+
"face_models": ("FACE_MODEL",),
|
| 720 |
+
}
|
| 721 |
+
}
|
| 722 |
+
|
| 723 |
+
RETURN_TYPES = ("FACE_MODEL",)
|
| 724 |
+
FUNCTION = "blend_faces"
|
| 725 |
+
|
| 726 |
+
OUTPUT_NODE = True
|
| 727 |
+
|
| 728 |
+
CATEGORY = "π ReActor"
|
| 729 |
+
|
| 730 |
+
def build_face_model(self, image: Image.Image, det_size=(640, 640)):
|
| 731 |
+
logging.StreamHandler.terminator = "\n"
|
| 732 |
+
if image is None:
|
| 733 |
+
error_msg = "Please load an Image"
|
| 734 |
+
logger.error(error_msg)
|
| 735 |
+
return error_msg
|
| 736 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 737 |
+
face_model = analyze_faces(image, det_size)
|
| 738 |
+
|
| 739 |
+
if len(face_model) == 0:
|
| 740 |
+
print("")
|
| 741 |
+
det_size_half = half_det_size(det_size)
|
| 742 |
+
face_model = analyze_faces(image, det_size_half)
|
| 743 |
+
if face_model is not None and len(face_model) > 0:
|
| 744 |
+
print("...........................................................", end=" ")
|
| 745 |
+
|
| 746 |
+
if face_model is not None and len(face_model) > 0:
|
| 747 |
+
return face_model[0]
|
| 748 |
+
else:
|
| 749 |
+
no_face_msg = "No face found, please try another image"
|
| 750 |
+
# logger.error(no_face_msg)
|
| 751 |
+
return no_face_msg
|
| 752 |
+
|
| 753 |
+
def blend_faces(self, save_mode, send_only, face_model_name, compute_method, images=None, face_models=None):
|
| 754 |
+
global BLENDED_FACE_MODEL
|
| 755 |
+
blended_face: Face = BLENDED_FACE_MODEL
|
| 756 |
+
|
| 757 |
+
if send_only and blended_face is None:
|
| 758 |
+
send_only = False
|
| 759 |
+
|
| 760 |
+
if (images is not None or face_models is not None) and not send_only:
|
| 761 |
+
|
| 762 |
+
faces = []
|
| 763 |
+
embeddings = []
|
| 764 |
+
|
| 765 |
+
apply_patch(1)
|
| 766 |
+
|
| 767 |
+
if images is not None:
|
| 768 |
+
images_list: List[Image.Image] = batch_tensor_to_pil(images)
|
| 769 |
+
|
| 770 |
+
n = len(images_list)
|
| 771 |
+
|
| 772 |
+
for i,image in enumerate(images_list):
|
| 773 |
+
logging.StreamHandler.terminator = " "
|
| 774 |
+
logger.status(f"Building Face Model {i+1} of {n}...")
|
| 775 |
+
face = self.build_face_model(image)
|
| 776 |
+
if isinstance(face, str):
|
| 777 |
+
logger.error(f"No faces found in image {i+1}, skipping")
|
| 778 |
+
continue
|
| 779 |
+
else:
|
| 780 |
+
print(f"{int(((i+1)/n)*100)}%")
|
| 781 |
+
faces.append(face)
|
| 782 |
+
embeddings.append(face.embedding)
|
| 783 |
+
|
| 784 |
+
elif face_models is not None:
|
| 785 |
+
|
| 786 |
+
n = len(face_models)
|
| 787 |
+
|
| 788 |
+
for i,face_model in enumerate(face_models):
|
| 789 |
+
logging.StreamHandler.terminator = " "
|
| 790 |
+
logger.status(f"Extracting Face Model {i+1} of {n}...")
|
| 791 |
+
face = face_model
|
| 792 |
+
if isinstance(face, str):
|
| 793 |
+
logger.error(f"No faces found for face_model {i+1}, skipping")
|
| 794 |
+
continue
|
| 795 |
+
else:
|
| 796 |
+
print(f"{int(((i+1)/n)*100)}%")
|
| 797 |
+
faces.append(face)
|
| 798 |
+
embeddings.append(face.embedding)
|
| 799 |
+
|
| 800 |
+
logging.StreamHandler.terminator = "\n"
|
| 801 |
+
if len(faces) > 0:
|
| 802 |
+
# compute_method_name = "Mean" if compute_method == 0 else "Median" if compute_method == 1 else "Mode"
|
| 803 |
+
logger.status(f"Blending with Compute Method '{compute_method}'...")
|
| 804 |
+
blended_embedding = np.mean(embeddings, axis=0) if compute_method == "Mean" else np.median(embeddings, axis=0) if compute_method == "Median" else stats.mode(embeddings, axis=0)[0].astype(np.float32)
|
| 805 |
+
blended_face = Face(
|
| 806 |
+
bbox=faces[0].bbox,
|
| 807 |
+
kps=faces[0].kps,
|
| 808 |
+
det_score=faces[0].det_score,
|
| 809 |
+
landmark_3d_68=faces[0].landmark_3d_68,
|
| 810 |
+
pose=faces[0].pose,
|
| 811 |
+
landmark_2d_106=faces[0].landmark_2d_106,
|
| 812 |
+
embedding=blended_embedding,
|
| 813 |
+
gender=faces[0].gender,
|
| 814 |
+
age=faces[0].age
|
| 815 |
+
)
|
| 816 |
+
if blended_face is not None:
|
| 817 |
+
BLENDED_FACE_MODEL = blended_face
|
| 818 |
+
if save_mode:
|
| 819 |
+
face_model_path = os.path.join(FACE_MODELS_PATH, face_model_name + ".safetensors")
|
| 820 |
+
save_face_model(blended_face,face_model_path)
|
| 821 |
+
# done_msg = f"Face model has been saved to '{face_model_path}'"
|
| 822 |
+
# logger.status(done_msg)
|
| 823 |
+
logger.status("--Done!--")
|
| 824 |
+
# return (blended_face,)
|
| 825 |
+
else:
|
| 826 |
+
no_face_msg = "Something went wrong, please try another set of images"
|
| 827 |
+
logger.error(no_face_msg)
|
| 828 |
+
# return (blended_face,)
|
| 829 |
+
# logger.status("--Done!--")
|
| 830 |
+
if images is None and face_models is None:
|
| 831 |
+
logger.error("Please provide `images` or `face_models`")
|
| 832 |
+
return (blended_face,)
|
| 833 |
+
|
| 834 |
+
|
| 835 |
+
class SaveFaceModel:
|
| 836 |
+
def __init__(self):
|
| 837 |
+
self.output_dir = FACE_MODELS_PATH
|
| 838 |
+
|
| 839 |
+
@classmethod
|
| 840 |
+
def INPUT_TYPES(s):
|
| 841 |
+
return {
|
| 842 |
+
"required": {
|
| 843 |
+
"save_mode": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
|
| 844 |
+
"face_model_name": ("STRING", {"default": "default"}),
|
| 845 |
+
"select_face_index": ("INT", {"default": 0, "min": 0}),
|
| 846 |
+
},
|
| 847 |
+
"optional": {
|
| 848 |
+
"image": ("IMAGE",),
|
| 849 |
+
"face_model": ("FACE_MODEL",),
|
| 850 |
+
}
|
| 851 |
+
}
|
| 852 |
+
|
| 853 |
+
RETURN_TYPES = ()
|
| 854 |
+
FUNCTION = "save_model"
|
| 855 |
+
|
| 856 |
+
OUTPUT_NODE = True
|
| 857 |
+
|
| 858 |
+
CATEGORY = "π ReActor"
|
| 859 |
+
|
| 860 |
+
def save_model(self, save_mode, face_model_name, select_face_index, image=None, face_model=None, det_size=(640, 640)):
|
| 861 |
+
if save_mode and image is not None:
|
| 862 |
+
source = tensor_to_pil(image)
|
| 863 |
+
source = cv2.cvtColor(np.array(source), cv2.COLOR_RGB2BGR)
|
| 864 |
+
apply_patch(1)
|
| 865 |
+
logger.status("Building Face Model...")
|
| 866 |
+
face_model_raw = analyze_faces(source, det_size)
|
| 867 |
+
if len(face_model_raw) == 0:
|
| 868 |
+
det_size_half = half_det_size(det_size)
|
| 869 |
+
face_model_raw = analyze_faces(source, det_size_half)
|
| 870 |
+
try:
|
| 871 |
+
face_model = face_model_raw[select_face_index]
|
| 872 |
+
except:
|
| 873 |
+
logger.error("No face(s) found")
|
| 874 |
+
return face_model_name
|
| 875 |
+
logger.status("--Done!--")
|
| 876 |
+
if save_mode and (face_model != "none" or face_model is not None):
|
| 877 |
+
face_model_path = os.path.join(self.output_dir, face_model_name + ".safetensors")
|
| 878 |
+
save_face_model(face_model,face_model_path)
|
| 879 |
+
if image is None and face_model is None:
|
| 880 |
+
logger.error("Please provide `face_model` or `image`")
|
| 881 |
+
return face_model_name
|
| 882 |
+
|
| 883 |
+
|
| 884 |
+
class RestoreFace:
|
| 885 |
+
@classmethod
|
| 886 |
+
def INPUT_TYPES(s):
|
| 887 |
+
return {
|
| 888 |
+
"required": {
|
| 889 |
+
"image": ("IMAGE",),
|
| 890 |
+
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
|
| 891 |
+
"model": (get_model_names(get_restorers),),
|
| 892 |
+
"visibility": ("FLOAT", {"default": 1, "min": 0.0, "max": 1, "step": 0.05}),
|
| 893 |
+
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
|
| 894 |
+
},
|
| 895 |
+
}
|
| 896 |
+
|
| 897 |
+
RETURN_TYPES = ("IMAGE",)
|
| 898 |
+
FUNCTION = "execute"
|
| 899 |
+
CATEGORY = "π ReActor"
|
| 900 |
+
|
| 901 |
+
def execute(self, image, model, visibility, codeformer_weight, facedetection):
|
| 902 |
+
result = reactor.restore_face(
|
| 903 |
+
self, image, model, visibility, codeformer_weight, facedetection
|
| 904 |
+
)
|
| 905 |
+
return (result,)
|
| 906 |
+
|
| 907 |
+
|
| 908 |
+
class RestoreFaceAdvanced:
|
| 909 |
+
@classmethod
|
| 910 |
+
def INPUT_TYPES(s):
|
| 911 |
+
return {
|
| 912 |
+
"required": {
|
| 913 |
+
"image": ("IMAGE",),
|
| 914 |
+
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
|
| 915 |
+
"model": (get_model_names(get_restorers),),
|
| 916 |
+
"visibility": ("FLOAT", {"default": 1, "min": 0.0, "max": 1, "step": 0.05}),
|
| 917 |
+
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
|
| 918 |
+
"face_selection": (["all", "filter", "largest"],{"default": "all"}),
|
| 919 |
+
},
|
| 920 |
+
"optional": {
|
| 921 |
+
"sort_by": (["area", "x_position", "y_position", "detection_confidence"],{"default": "area"}),
|
| 922 |
+
"reverse_order": ("BOOLEAN", {"default": False}),
|
| 923 |
+
"take_start": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}),
|
| 924 |
+
"take_count": ("INT", {"default": 1, "min": 1, "max": 100, "step": 1}),
|
| 925 |
+
}
|
| 926 |
+
}
|
| 927 |
+
|
| 928 |
+
RETURN_TYPES = ("IMAGE",)
|
| 929 |
+
FUNCTION = "execute"
|
| 930 |
+
CATEGORY = "π ReActor"
|
| 931 |
+
|
| 932 |
+
def execute(
|
| 933 |
+
self, image, model, visibility, codeformer_weight, facedetection, face_selection, sort_by="area", reverse_order=False, take_start=0, take_count=1
|
| 934 |
+
):
|
| 935 |
+
|
| 936 |
+
min_x_position=0.0
|
| 937 |
+
max_x_position=1.0
|
| 938 |
+
min_y_position=0.0
|
| 939 |
+
max_y_position=1.0
|
| 940 |
+
|
| 941 |
+
result = image
|
| 942 |
+
|
| 943 |
+
face_restore_model = model
|
| 944 |
+
|
| 945 |
+
if face_restore_model != "none" and not model_management.processing_interrupted():
|
| 946 |
+
|
| 947 |
+
global FACE_SIZE, FACE_HELPER
|
| 948 |
+
|
| 949 |
+
self.face_helper = FACE_HELPER
|
| 950 |
+
|
| 951 |
+
faceSize = 512
|
| 952 |
+
if "1024" in face_restore_model.lower():
|
| 953 |
+
faceSize = 1024
|
| 954 |
+
elif "2048" in face_restore_model.lower():
|
| 955 |
+
faceSize = 2048
|
| 956 |
+
|
| 957 |
+
logger.status(f"Restoring with {face_restore_model} | Face Size is set to {faceSize}")
|
| 958 |
+
|
| 959 |
+
model_path = folder_paths.get_full_path("facerestore_models", face_restore_model)
|
| 960 |
+
|
| 961 |
+
device = model_management.get_torch_device()
|
| 962 |
+
|
| 963 |
+
if "codeformer" in face_restore_model.lower():
|
| 964 |
+
|
| 965 |
+
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(
|
| 966 |
+
dim_embd=512,
|
| 967 |
+
codebook_size=1024,
|
| 968 |
+
n_head=8,
|
| 969 |
+
n_layers=9,
|
| 970 |
+
connect_list=["32", "64", "128", "256"],
|
| 971 |
+
).to(device)
|
| 972 |
+
checkpoint = torch.load(model_path)["params_ema"]
|
| 973 |
+
codeformer_net.load_state_dict(checkpoint)
|
| 974 |
+
facerestore_model = codeformer_net.eval()
|
| 975 |
+
|
| 976 |
+
elif ".onnx" in face_restore_model:
|
| 977 |
+
|
| 978 |
+
ort_session = set_ort_session(model_path, providers=providers)
|
| 979 |
+
ort_session_inputs = {}
|
| 980 |
+
facerestore_model = ort_session
|
| 981 |
+
|
| 982 |
+
else:
|
| 983 |
+
|
| 984 |
+
sd = comfy.utils.load_torch_file(model_path, safe_load=True)
|
| 985 |
+
facerestore_model = model_loading.load_state_dict(sd).eval()
|
| 986 |
+
facerestore_model.to(device)
|
| 987 |
+
|
| 988 |
+
if faceSize != FACE_SIZE or self.face_helper is None:
|
| 989 |
+
self.face_helper = FaceRestoreHelper(1, face_size=faceSize, crop_ratio=(1, 1), det_model=facedetection, save_ext='png', use_parse=True, device=device)
|
| 990 |
+
FACE_SIZE = faceSize
|
| 991 |
+
FACE_HELPER = self.face_helper
|
| 992 |
+
|
| 993 |
+
image_np = 255. * result.cpu().numpy()
|
| 994 |
+
|
| 995 |
+
total_images = image_np.shape[0]
|
| 996 |
+
|
| 997 |
+
out_images = []
|
| 998 |
+
|
| 999 |
+
pbar = progress_bar(total_images)
|
| 1000 |
+
|
| 1001 |
+
for i in range(total_images):
|
| 1002 |
+
|
| 1003 |
+
cur_image_np = image_np[i,:, :, ::-1]
|
| 1004 |
+
|
| 1005 |
+
original_resolution = cur_image_np.shape[0:2]
|
| 1006 |
+
|
| 1007 |
+
if facerestore_model is None or self.face_helper is None:
|
| 1008 |
+
return result
|
| 1009 |
+
|
| 1010 |
+
self.face_helper.clean_all()
|
| 1011 |
+
self.face_helper.read_image(cur_image_np)
|
| 1012 |
+
self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
|
| 1013 |
+
self.face_helper.align_warp_face()
|
| 1014 |
+
|
| 1015 |
+
# Face-Filter Mode
|
| 1016 |
+
|
| 1017 |
+
# Π€ΠΈΠ»ΡΡΡΠ°ΡΠΈΡ Π»ΠΈΡ
|
| 1018 |
+
if face_selection != "all" and self.face_helper.cropped_faces:
|
| 1019 |
+
# Π‘ΠΎΠ±ΠΈΡΠ°Π΅ΠΌ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ Π»ΠΈΡΠ°Ρ
Π΄Π»Ρ ΡΠΈΠ»ΡΡΡΠ°ΡΠΈΠΈ
|
| 1020 |
+
face_info = []
|
| 1021 |
+
img_height, img_width = cur_image_np.shape[0:2]
|
| 1022 |
+
|
| 1023 |
+
for j, face in enumerate(self.face_helper.cropped_faces):
|
| 1024 |
+
# ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ ΡΠ΅Π½ΡΡ Π»ΠΈΡΠ° Π²ΠΌΠ΅ΡΡΠΎ Π»Π΅Π²ΠΎΠ³ΠΎ Π²Π΅ΡΡ
Π½Π΅Π³ΠΎ ΡΠ³Π»Π°
|
| 1025 |
+
if hasattr(self.face_helper, 'det_faces') and len(self.face_helper.det_faces) > j:
|
| 1026 |
+
bbox = self.face_helper.det_faces[j]
|
| 1027 |
+
# ΠΡΡΠΈΡΠ»ΡΠ΅ΠΌ ΡΠ΅Π½ΡΡ Π»ΠΈΡΠ° Π΄Π»Ρ Π±ΠΎΠ»Π΅Π΅ ΡΠΎΡΠ½ΠΎΠ³ΠΎ ΠΏΠΎΠ·ΠΈΡΠΈΠΎΠ½ΠΈΡΠΎΠ²Π°Π½ΠΈΡ
|
| 1028 |
+
x1 = ((bbox[0] + bbox[2]) / 2) / img_width # ΡΠ΅Π½ΡΡ x
|
| 1029 |
+
y1 = ((bbox[1] + bbox[3]) / 2) / img_height # ΡΠ΅Π½ΡΡ y
|
| 1030 |
+
area = face.shape[0] * face.shape[1]
|
| 1031 |
+
confidence = bbox[4] if len(bbox) > 4 else 1.0
|
| 1032 |
+
else:
|
| 1033 |
+
# ΠΡΠ»ΠΈ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ bbox Π½Π΅Π΄ΠΎΡΡΡΠΏΠ½Π°, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ ΠΏΡΠΈΠ±Π»ΠΈΠ·ΠΈΡΠ΅Π»ΡΠ½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
|
| 1034 |
+
area = face.shape[0] * face.shape[1]
|
| 1035 |
+
x1, y1 = 0.5, 0.5 # ΡΠ΅Π½ΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
|
| 1036 |
+
confidence = 1.0
|
| 1037 |
+
|
| 1038 |
+
face_info.append({
|
| 1039 |
+
'index': j,
|
| 1040 |
+
'area': area,
|
| 1041 |
+
'x_position': x1,
|
| 1042 |
+
'y_position': y1,
|
| 1043 |
+
'detection_confidence': confidence
|
| 1044 |
+
})
|
| 1045 |
+
|
| 1046 |
+
# Π‘Π½Π°ΡΠ°Π»Π° ΡΠΎΡΡΠΈΡΡΠ΅ΠΌ Π²ΡΠ΅ Π»ΠΈΡΠ° ΠΏΠΎ Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠΌΡ ΠΊΡΠΈΡΠ΅ΡΠΈΡ
|
| 1047 |
+
all_indices = list(range(len(self.face_helper.cropped_faces)))
|
| 1048 |
+
|
| 1049 |
+
# ΠΡΠ²ΠΎΠ΄ Π΄Π»Ρ x_position ΠΈ y_position
|
| 1050 |
+
if sort_by == "y_position":
|
| 1051 |
+
all_positions = [(idx, face_info[idx]['y_position']) for idx in all_indices]
|
| 1052 |
+
elif sort_by == "x_position":
|
| 1053 |
+
all_positions = [(idx, face_info[idx]['x_position']) for idx in all_indices]
|
| 1054 |
+
|
| 1055 |
+
# Π‘ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ° ΠΏΠΎ Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠΌΡ ΠΊΡΠΈΡΠ΅ΡΠΈΡ
|
| 1056 |
+
sorted_indices = sorted(
|
| 1057 |
+
all_indices,
|
| 1058 |
+
key=lambda idx: face_info[idx][sort_by],
|
| 1059 |
+
reverse=reverse_order
|
| 1060 |
+
)
|
| 1061 |
+
|
| 1062 |
+
# ΠΡΠ»Π°Π΄ΠΎΡΠ½ΡΠΉ Π²ΡΠ²ΠΎΠ΄ ΠΏΠΎΡΠ»Π΅ ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ
|
| 1063 |
+
if sort_by == "y_position":
|
| 1064 |
+
sorted_positions = [(idx, face_info[idx]['y_position']) for idx in sorted_indices]
|
| 1065 |
+
elif sort_by == "x_position":
|
| 1066 |
+
sorted_positions = [(idx, face_info[idx]['x_position']) for idx in sorted_indices]
|
| 1067 |
+
|
| 1068 |
+
# ΠΡΠΈΠΌΠ΅Π½ΡΠ΅ΠΌ ΡΠΈΠ»ΡΡΡΠ°ΡΠΈΡ Π² Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ ΠΎΡ ΡΠ΅ΠΆΠΈΠΌΠ°
|
| 1069 |
+
if face_selection == "filter":
|
| 1070 |
+
# Π€ΠΈΠ»ΡΡΡΠ°ΡΠΈΡ ΠΏΠΎ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ°ΠΌ
|
| 1071 |
+
filtered_indices = [
|
| 1072 |
+
idx for idx in sorted_indices
|
| 1073 |
+
if min_x_position <= face_info[idx]['x_position'] <= max_x_position and
|
| 1074 |
+
min_y_position <= face_info[idx]['y_position'] <= max_y_position
|
| 1075 |
+
]
|
| 1076 |
+
|
| 1077 |
+
# ΠΡΠ±ΠΎΡΠΊΠ° ΠΏΠΎ take_start ΠΈ take_count
|
| 1078 |
+
selected_indices = filtered_indices[take_start:take_start + take_count]
|
| 1079 |
+
|
| 1080 |
+
elif face_selection == "largest":
|
| 1081 |
+
# ΠΡΠΈ Π²ΡΠ±ΠΎΡΠ΅ "largest" ΠΏΡΠΎΡΡΠΎ Π±Π΅ΡΠ΅ΠΌ take_count Π»ΠΈΡ Ρ Π½Π°ΠΈΠ±ΠΎΠ»ΡΡΠ΅ΠΉ ΠΏΠ»ΠΎΡΠ°Π΄ΡΡ, Π½Π°ΡΠΈΠ½Π°Ρ Ρ take_start
|
| 1082 |
+
selected_indices = sorted_indices[take_start:take_start + take_count]
|
| 1083 |
+
|
| 1084 |
+
elif face_selection == "index":
|
| 1085 |
+
# Π ΡΠ΅ΠΆΠΈΠΌΠ΅ "index" ΠΏΡΠΎΡΡΠΎ Π±Π΅ΡΠ΅ΠΌ Π»ΠΈΡΠ°, Π½Π°ΡΠΈΠ½Π°Ρ Ρ take_start
|
| 1086 |
+
selected_indices = sorted_indices[take_start:take_start + take_count]
|
| 1087 |
+
|
| 1088 |
+
if selected_indices:
|
| 1089 |
+
self.face_helper.cropped_faces = [self.face_helper.cropped_faces[j] for j in selected_indices]
|
| 1090 |
+
if hasattr(self.face_helper, 'restored_faces'):
|
| 1091 |
+
self.face_helper.restored_faces = []
|
| 1092 |
+
if hasattr(self.face_helper, 'affine_matrices'):
|
| 1093 |
+
self.face_helper.affine_matrices = [self.face_helper.affine_matrices[j] for j in selected_indices]
|
| 1094 |
+
if hasattr(self.face_helper, 'det_faces'):
|
| 1095 |
+
self.face_helper.det_faces = [self.face_helper.det_faces[j] for j in selected_indices]
|
| 1096 |
+
|
| 1097 |
+
# Face-Filter Mode END
|
| 1098 |
+
|
| 1099 |
+
restored_face = None
|
| 1100 |
+
|
| 1101 |
+
for idx, cropped_face in enumerate(self.face_helper.cropped_faces):
|
| 1102 |
+
|
| 1103 |
+
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
| 1104 |
+
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
| 1105 |
+
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
|
| 1106 |
+
|
| 1107 |
+
try:
|
| 1108 |
+
|
| 1109 |
+
with torch.no_grad():
|
| 1110 |
+
|
| 1111 |
+
if ".onnx" in face_restore_model: # ONNX models
|
| 1112 |
+
|
| 1113 |
+
for ort_session_input in ort_session.get_inputs():
|
| 1114 |
+
if ort_session_input.name == "input":
|
| 1115 |
+
cropped_face_prep = prepare_cropped_face(cropped_face)
|
| 1116 |
+
ort_session_inputs[ort_session_input.name] = cropped_face_prep
|
| 1117 |
+
if ort_session_input.name == "weight":
|
| 1118 |
+
weight = np.array([ 1 ], dtype = np.double)
|
| 1119 |
+
ort_session_inputs[ort_session_input.name] = weight
|
| 1120 |
+
|
| 1121 |
+
output = ort_session.run(None, ort_session_inputs)[0][0]
|
| 1122 |
+
restored_face = normalize_cropped_face(output)
|
| 1123 |
+
|
| 1124 |
+
else: # PTH models
|
| 1125 |
+
|
| 1126 |
+
output = facerestore_model(cropped_face_t, w=codeformer_weight)[0] if "codeformer" in face_restore_model.lower() else facerestore_model(cropped_face_t)[0]
|
| 1127 |
+
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
| 1128 |
+
|
| 1129 |
+
del output
|
| 1130 |
+
torch.cuda.empty_cache()
|
| 1131 |
+
|
| 1132 |
+
except Exception as error:
|
| 1133 |
+
|
| 1134 |
+
print(f"\tFailed inference: {error}", file=sys.stderr)
|
| 1135 |
+
restored_face = cropped_face.copy()
|
| 1136 |
+
|
| 1137 |
+
if visibility < 1:
|
| 1138 |
+
restored_face = cropped_face * (1 - visibility) + restored_face * visibility
|
| 1139 |
+
|
| 1140 |
+
restored_face = restored_face.astype("uint8")
|
| 1141 |
+
self.face_helper.add_restored_face(restored_face)
|
| 1142 |
+
|
| 1143 |
+
self.face_helper.get_inverse_affine(None)
|
| 1144 |
+
|
| 1145 |
+
restored_img = self.face_helper.paste_faces_to_input_image()
|
| 1146 |
+
restored_img = restored_img[:, :, ::-1]
|
| 1147 |
+
|
| 1148 |
+
if original_resolution != restored_img.shape[0:2]:
|
| 1149 |
+
restored_img = cv2.resize(restored_img, (0, 0), fx=original_resolution[1]/restored_img.shape[1], fy=original_resolution[0]/restored_img.shape[0], interpolation=cv2.INTER_AREA)
|
| 1150 |
+
|
| 1151 |
+
self.face_helper.clean_all()
|
| 1152 |
+
|
| 1153 |
+
out_images.append(restored_img)
|
| 1154 |
+
|
| 1155 |
+
if state.interrupted or model_management.processing_interrupted():
|
| 1156 |
+
logger.status("Interrupted by User")
|
| 1157 |
+
return image
|
| 1158 |
+
|
| 1159 |
+
pbar.update(1)
|
| 1160 |
+
|
| 1161 |
+
restored_img_np = np.array(out_images).astype(np.float32) / 255.0
|
| 1162 |
+
restored_img_tensor = torch.from_numpy(restored_img_np)
|
| 1163 |
+
|
| 1164 |
+
result = restored_img_tensor
|
| 1165 |
+
|
| 1166 |
+
progress_bar_reset(pbar)
|
| 1167 |
+
|
| 1168 |
+
return (result,)
|
| 1169 |
+
|
| 1170 |
+
|
| 1171 |
+
class MaskHelper:
|
| 1172 |
+
def __init__(self):
|
| 1173 |
+
self.labels = "all"
|
| 1174 |
+
self.detailer_hook = None
|
| 1175 |
+
self.device_mode = "AUTO"
|
| 1176 |
+
self.detection_hint = "center-1"
|
| 1177 |
+
self._sam_cache = {}
|
| 1178 |
+
self._bbox_cache = {}
|
| 1179 |
+
|
| 1180 |
+
@classmethod
|
| 1181 |
+
def INPUT_TYPES(s):
|
| 1182 |
+
bboxs = ["bbox/"+x for x in folder_paths.get_filename_list("ultralytics_bbox")]
|
| 1183 |
+
segms = ["segm/"+x for x in folder_paths.get_filename_list("ultralytics_segm")]
|
| 1184 |
+
sam_models = [x for x in folder_paths.get_filename_list("sams") if 'hq' not in x]
|
| 1185 |
+
return {
|
| 1186 |
+
"required": {
|
| 1187 |
+
"image": ("IMAGE",),
|
| 1188 |
+
"swapped_image": ("IMAGE",),
|
| 1189 |
+
"bbox_model_name": (bboxs + segms, ),
|
| 1190 |
+
"bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
|
| 1191 |
+
"bbox_dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}),
|
| 1192 |
+
"bbox_crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}),
|
| 1193 |
+
"bbox_drop_size": ("INT", {"min": 1, "max": 8192, "step": 1, "default": 10}),
|
| 1194 |
+
"sam_model_name": (sam_models, ),
|
| 1195 |
+
"sam_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}),
|
| 1196 |
+
"sam_threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01}),
|
| 1197 |
+
"bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}),
|
| 1198 |
+
"mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
|
| 1199 |
+
"mask_hint_use_negative": (["False", "Small", "Outter"], ),
|
| 1200 |
+
"morphology_operation": (["dilate", "erode", "open", "close"],),
|
| 1201 |
+
"morphology_distance": ("INT", {"default": 0, "min": 0, "max": 128, "step": 1}),
|
| 1202 |
+
"blur_radius": ("INT", {"default": 9, "min": 0, "max": 48, "step": 1}),
|
| 1203 |
+
"sigma_factor": ("FLOAT", {"default": 1.0, "min": 0.01, "max": 3., "step": 0.01}),
|
| 1204 |
+
},
|
| 1205 |
+
"optional": {
|
| 1206 |
+
"mask_optional": ("MASK",),
|
| 1207 |
+
}
|
| 1208 |
+
}
|
| 1209 |
+
|
| 1210 |
+
RETURN_TYPES = ("IMAGE","MASK","IMAGE","IMAGE")
|
| 1211 |
+
RETURN_NAMES = ("IMAGE","MASK","MASK_PREVIEW","SWAPPED_FACE")
|
| 1212 |
+
FUNCTION = "execute"
|
| 1213 |
+
CATEGORY = "π ReActor"
|
| 1214 |
+
|
| 1215 |
+
def execute(self, image, swapped_image, bbox_model_name, bbox_threshold, bbox_dilation, bbox_crop_factor, bbox_drop_size, sam_model_name, sam_dilation, sam_threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative, morphology_operation, morphology_distance, blur_radius, sigma_factor, mask_optional=None):
|
| 1216 |
+
device = model_management.get_torch_device()
|
| 1217 |
+
|
| 1218 |
+
# ΠΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΠΎ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ°Π΅ΠΌ ΡΠ΅Π½Π·ΠΎΡΡ
|
| 1219 |
+
if isinstance(image, torch.Tensor) and image.device != device:
|
| 1220 |
+
image = image.to(device)
|
| 1221 |
+
|
| 1222 |
+
if isinstance(swapped_image, torch.Tensor) and swapped_image.device != device:
|
| 1223 |
+
swapped_image = swapped_image.to(device)
|
| 1224 |
+
|
| 1225 |
+
if mask_optional is not None:
|
| 1226 |
+
combined_mask = mask_optional
|
| 1227 |
+
else:
|
| 1228 |
+
# Load and cache BBox model
|
| 1229 |
+
if bbox_model_name not in self._bbox_cache:
|
| 1230 |
+
bbox_model_path = folder_paths.get_full_path("ultralytics", bbox_model_name)
|
| 1231 |
+
model = subcore.load_yolo(bbox_model_path)
|
| 1232 |
+
self._bbox_cache[bbox_model_name] = subcore.UltraBBoxDetector(model)
|
| 1233 |
+
bbox_detector = self._bbox_cache[bbox_model_name]
|
| 1234 |
+
|
| 1235 |
+
segs_all, seg_labels = bbox_detector.detect(image, bbox_threshold, bbox_dilation, bbox_crop_factor, bbox_drop_size, self.detailer_hook)
|
| 1236 |
+
|
| 1237 |
+
if self.labels != 'all':
|
| 1238 |
+
labels = self.labels.split(',') if isinstance(self.labels, str) else self.labels
|
| 1239 |
+
segs_all, _ = masking_segs.filter(segs_all, labels)
|
| 1240 |
+
|
| 1241 |
+
# Load and cache SAM model
|
| 1242 |
+
if sam_model_name not in self._sam_cache:
|
| 1243 |
+
sam_model_path = folder_paths.get_full_path("sams", sam_model_name)
|
| 1244 |
+
if 'vit_h' in sam_model_name:
|
| 1245 |
+
model_kind = 'vit_h'
|
| 1246 |
+
elif 'vit_l' in sam_model_name:
|
| 1247 |
+
model_kind = 'vit_l'
|
| 1248 |
+
else:
|
| 1249 |
+
model_kind = 'vit_b'
|
| 1250 |
+
sam = sam_model_registry[model_kind](checkpoint=sam_model_path)
|
| 1251 |
+
size = os.path.getsize(sam_model_path)
|
| 1252 |
+
sam.safe_to = core.SafeToGPU(size)
|
| 1253 |
+
sam.safe_to.to_device(sam, device)
|
| 1254 |
+
sam.is_auto_mode = self.device_mode == "AUTO"
|
| 1255 |
+
self._sam_cache[sam_model_name] = sam
|
| 1256 |
+
else:
|
| 1257 |
+
sam = self._sam_cache[sam_model_name]
|
| 1258 |
+
|
| 1259 |
+
# Handle batched input
|
| 1260 |
+
if image.ndim == 4:
|
| 1261 |
+
combined_masks = []
|
| 1262 |
+
for i in range(image.shape[0]):
|
| 1263 |
+
segs_i = segs_all[i] if i < len(segs_all) else []
|
| 1264 |
+
segs_tuple = ([segs_i], seg_labels) if isinstance(segs_i, dict) else (segs_i, seg_labels)
|
| 1265 |
+
image_device = image.to(device) if image.device != device else image
|
| 1266 |
+
image_i = image_device[i]
|
| 1267 |
+
mask_i, _ = core.make_sam_mask_segmented(
|
| 1268 |
+
sam, segs_tuple, image_i, self.detection_hint,
|
| 1269 |
+
sam_dilation, sam_threshold, bbox_expansion,
|
| 1270 |
+
mask_hint_threshold, mask_hint_use_negative
|
| 1271 |
+
)
|
| 1272 |
+
combined_masks.append(mask_i)
|
| 1273 |
+
combined_mask = torch.stack(combined_masks)
|
| 1274 |
+
else:
|
| 1275 |
+
image_device = image.to(device) if image.device != device else image
|
| 1276 |
+
combined_mask, _ = core.make_sam_mask_segmented(
|
| 1277 |
+
sam, (segs_all, seg_labels), image_device, self.detection_hint,
|
| 1278 |
+
sam_dilation, sam_threshold, bbox_expansion,
|
| 1279 |
+
mask_hint_threshold, mask_hint_use_negative
|
| 1280 |
+
)
|
| 1281 |
+
|
| 1282 |
+
# Morph operations
|
| 1283 |
+
if morphology_operation == "dilate":
|
| 1284 |
+
# print(f"max before: {combined_mask.max()}, min: {combined_mask.min()}, sum: {combined_mask.sum()}")
|
| 1285 |
+
combined_mask = self.iterative_morphology(combined_mask, morphology_distance, op="dilate")
|
| 1286 |
+
# print(f"after morph: {combined_mask.max()}, min: {combined_mask.min()}, sum: {combined_mask.sum()}")
|
| 1287 |
+
elif morphology_operation == "erode":
|
| 1288 |
+
combined_mask = self.iterative_morphology(combined_mask, morphology_distance, op="erode")
|
| 1289 |
+
elif morphology_operation == "open":
|
| 1290 |
+
combined_mask = self.iterative_morphology(self.iterative_morphology(combined_mask, morphology_distance, op="erode"), morphology_distance, op="dilate")
|
| 1291 |
+
elif morphology_operation == "close":
|
| 1292 |
+
combined_mask = self.iterative_morphology(self.iterative_morphology(combined_mask, morphology_distance, op="dilate"), morphology_distance, op="erode")
|
| 1293 |
+
|
| 1294 |
+
# Gaussian blur
|
| 1295 |
+
if blur_radius > 0:
|
| 1296 |
+
blur = T.GaussianBlur(kernel_size=blur_radius * 2 + 1, sigma=sigma_factor)
|
| 1297 |
+
mask_blurred = blur(combined_mask.unsqueeze(1)).squeeze(1)
|
| 1298 |
+
else:
|
| 1299 |
+
mask_blurred = combined_mask
|
| 1300 |
+
|
| 1301 |
+
# Apply mask to swapped image (basic RGBA composite)
|
| 1302 |
+
swapped_image = swapped_image.to(device) if swapped_image.device != device else swapped_image
|
| 1303 |
+
swapped_rgba = core.tensor2rgba(swapped_image)
|
| 1304 |
+
|
| 1305 |
+
mask_image_final = mask_blurred
|
| 1306 |
+
|
| 1307 |
+
# *** CUT BY MASK ***:
|
| 1308 |
+
|
| 1309 |
+
if len(swapped_image.shape) < 4:
|
| 1310 |
+
C = 1
|
| 1311 |
+
else:
|
| 1312 |
+
C = swapped_image.shape[3]
|
| 1313 |
+
|
| 1314 |
+
# We operate on RGBA to keep the code clean and then convert back after
|
| 1315 |
+
swapped_image = core.tensor2rgba(swapped_image)
|
| 1316 |
+
mask = core.tensor2mask(mask_image_final)
|
| 1317 |
+
|
| 1318 |
+
# Scale the mask to be a matching size if it isn't
|
| 1319 |
+
B, H, W, _ = swapped_image.shape
|
| 1320 |
+
mask = torch.nn.functional.interpolate(mask.unsqueeze(1), size=(H, W), mode='nearest')[:,0,:,:]
|
| 1321 |
+
MB, _, _ = mask.shape
|
| 1322 |
+
|
| 1323 |
+
if MB < B:
|
| 1324 |
+
assert(B % MB == 0)
|
| 1325 |
+
mask = mask.repeat(B // MB, 1, 1)
|
| 1326 |
+
|
| 1327 |
+
# masks_to_boxes errors if the tensor is all zeros, so we'll add a single pixel and zero it out at the end
|
| 1328 |
+
is_empty = ~torch.gt(torch.max(torch.reshape(mask,[MB, H * W]), dim=1).values, 0.)
|
| 1329 |
+
mask[is_empty,0,0] = 1.
|
| 1330 |
+
boxes = masks_to_boxes(mask)
|
| 1331 |
+
mask[is_empty,0,0] = 0.
|
| 1332 |
+
|
| 1333 |
+
min_x = boxes[:,0]
|
| 1334 |
+
min_y = boxes[:,1]
|
| 1335 |
+
max_x = boxes[:,2]
|
| 1336 |
+
max_y = boxes[:,3]
|
| 1337 |
+
|
| 1338 |
+
width = max_x - min_x + 1
|
| 1339 |
+
height = max_y - min_y + 1
|
| 1340 |
+
|
| 1341 |
+
use_width = int(torch.max(width).item())
|
| 1342 |
+
use_height = int(torch.max(height).item())
|
| 1343 |
+
|
| 1344 |
+
alpha_mask = torch.ones((B, H, W, 4))
|
| 1345 |
+
alpha_mask[:,:,:,3] = mask
|
| 1346 |
+
|
| 1347 |
+
alpha_mask = alpha_mask.to(device) if alpha_mask.device != device else alpha_mask
|
| 1348 |
+
|
| 1349 |
+
swapped_image = swapped_image * alpha_mask
|
| 1350 |
+
|
| 1351 |
+
cutted_image = torch.zeros((B, use_height, use_width, 4))
|
| 1352 |
+
for i in range(0, B):
|
| 1353 |
+
if not is_empty[i]:
|
| 1354 |
+
ymin = int(min_y[i].item())
|
| 1355 |
+
ymax = int(max_y[i].item())
|
| 1356 |
+
xmin = int(min_x[i].item())
|
| 1357 |
+
xmax = int(max_x[i].item())
|
| 1358 |
+
single = (swapped_image[i, ymin:ymax+1, xmin:xmax+1,:]).unsqueeze(0)
|
| 1359 |
+
resized = torch.nn.functional.interpolate(single.permute(0, 3, 1, 2), size=(use_height, use_width), mode='bicubic').permute(0, 2, 3, 1)
|
| 1360 |
+
cutted_image[i] = resized[0]
|
| 1361 |
+
|
| 1362 |
+
# Preserve our type unless we were previously RGB and added non-opaque alpha due to the mask size
|
| 1363 |
+
if C == 1:
|
| 1364 |
+
cutted_image = core.tensor2mask(cutted_image)
|
| 1365 |
+
elif C == 3 and torch.min(cutted_image[:,:,:,3]) == 1:
|
| 1366 |
+
cutted_image = core.tensor2rgb(cutted_image)
|
| 1367 |
+
|
| 1368 |
+
# *** PASTE BY MASK ***:
|
| 1369 |
+
|
| 1370 |
+
image_base = core.tensor2rgba(image)
|
| 1371 |
+
image_to_paste = core.tensor2rgba(cutted_image)
|
| 1372 |
+
mask = core.tensor2mask(mask_image_final)
|
| 1373 |
+
|
| 1374 |
+
# Scale the mask to be a matching size if it isn't
|
| 1375 |
+
B, H, W, C = image_base.shape
|
| 1376 |
+
MB = mask.shape[0]
|
| 1377 |
+
PB = image_to_paste.shape[0]
|
| 1378 |
+
|
| 1379 |
+
if B < PB:
|
| 1380 |
+
assert(PB % B == 0)
|
| 1381 |
+
image_base = image_base.repeat(PB // B, 1, 1, 1)
|
| 1382 |
+
B, H, W, C = image_base.shape
|
| 1383 |
+
if MB < B:
|
| 1384 |
+
assert(B % MB == 0)
|
| 1385 |
+
mask = mask.repeat(B // MB, 1, 1)
|
| 1386 |
+
elif B < MB:
|
| 1387 |
+
assert(MB % B == 0)
|
| 1388 |
+
image_base = image_base.repeat(MB // B, 1, 1, 1)
|
| 1389 |
+
if PB < B:
|
| 1390 |
+
assert(B % PB == 0)
|
| 1391 |
+
image_to_paste = image_to_paste.repeat(B // PB, 1, 1, 1)
|
| 1392 |
+
|
| 1393 |
+
mask = torch.nn.functional.interpolate(mask.unsqueeze(1), size=(H, W), mode='nearest')[:,0,:,:]
|
| 1394 |
+
MB, MH, MW = mask.shape
|
| 1395 |
+
|
| 1396 |
+
# masks_to_boxes errors if the tensor is all zeros, so we'll add a single pixel and zero it out at the end
|
| 1397 |
+
is_empty = ~torch.gt(torch.max(torch.reshape(mask,[MB, MH * MW]), dim=1).values, 0.)
|
| 1398 |
+
mask[is_empty,0,0] = 1.
|
| 1399 |
+
boxes = masks_to_boxes(mask)
|
| 1400 |
+
mask[is_empty,0,0] = 0.
|
| 1401 |
+
|
| 1402 |
+
min_x = boxes[:,0]
|
| 1403 |
+
min_y = boxes[:,1]
|
| 1404 |
+
max_x = boxes[:,2]
|
| 1405 |
+
max_y = boxes[:,3]
|
| 1406 |
+
mid_x = (min_x + max_x) / 2
|
| 1407 |
+
mid_y = (min_y + max_y) / 2
|
| 1408 |
+
|
| 1409 |
+
target_width = max_x - min_x + 1
|
| 1410 |
+
target_height = max_y - min_y + 1
|
| 1411 |
+
|
| 1412 |
+
result = image_base.detach().clone()
|
| 1413 |
+
face_segment = mask_image_final
|
| 1414 |
+
|
| 1415 |
+
pbar = progress_bar(MB)
|
| 1416 |
+
|
| 1417 |
+
for i in range(0, MB):
|
| 1418 |
+
if is_empty[i]:
|
| 1419 |
+
pbar.update(1)
|
| 1420 |
+
continue
|
| 1421 |
+
else:
|
| 1422 |
+
image_index = i
|
| 1423 |
+
SB, SH, SW, _ = image_to_paste.shape
|
| 1424 |
+
|
| 1425 |
+
# Figure out the desired size
|
| 1426 |
+
width = int(target_width[i].item())
|
| 1427 |
+
height = int(target_height[i].item())
|
| 1428 |
+
|
| 1429 |
+
width = SW
|
| 1430 |
+
height = SH
|
| 1431 |
+
|
| 1432 |
+
# Resize the image we're pasting if needed
|
| 1433 |
+
resized_image = image_to_paste[i].unsqueeze(0)
|
| 1434 |
+
|
| 1435 |
+
pasting = torch.ones([H, W, C])
|
| 1436 |
+
ymid = float(mid_y[i].item())
|
| 1437 |
+
ymin = int(math.floor(ymid - height / 2)) + 1
|
| 1438 |
+
ymax = int(math.floor(ymid + height / 2)) + 1
|
| 1439 |
+
xmid = float(mid_x[i].item())
|
| 1440 |
+
xmin = int(math.floor(xmid - width / 2)) + 1
|
| 1441 |
+
xmax = int(math.floor(xmid + width / 2)) + 1
|
| 1442 |
+
|
| 1443 |
+
_, source_ymax, source_xmax, _ = resized_image.shape
|
| 1444 |
+
source_ymin, source_xmin = 0, 0
|
| 1445 |
+
|
| 1446 |
+
if xmin < 0:
|
| 1447 |
+
source_xmin = abs(xmin)
|
| 1448 |
+
xmin = 0
|
| 1449 |
+
if ymin < 0:
|
| 1450 |
+
source_ymin = abs(ymin)
|
| 1451 |
+
ymin = 0
|
| 1452 |
+
if xmax > W:
|
| 1453 |
+
source_xmax -= (xmax - W)
|
| 1454 |
+
xmax = W
|
| 1455 |
+
if ymax > H:
|
| 1456 |
+
source_ymax -= (ymax - H)
|
| 1457 |
+
ymax = H
|
| 1458 |
+
|
| 1459 |
+
pasting[ymin:ymax, xmin:xmax, :] = resized_image[0, source_ymin:source_ymax, source_xmin:source_xmax, :]
|
| 1460 |
+
pasting[:, :, 3] = 1.
|
| 1461 |
+
|
| 1462 |
+
pasting_alpha = torch.zeros([H, W])
|
| 1463 |
+
pasting_alpha[ymin:ymax, xmin:xmax] = resized_image[0, source_ymin:source_ymax, source_xmin:source_xmax, 3]
|
| 1464 |
+
|
| 1465 |
+
paste_mask = torch.min(pasting_alpha, mask[i]).unsqueeze(2).repeat(1, 1, 4)
|
| 1466 |
+
|
| 1467 |
+
pasting = pasting.to(device) if pasting.device != device else pasting
|
| 1468 |
+
paste_mask = paste_mask.to(device) if paste_mask.device != device else paste_mask
|
| 1469 |
+
|
| 1470 |
+
result[image_index] = pasting * paste_mask + result[image_index] * (1. - paste_mask)
|
| 1471 |
+
|
| 1472 |
+
face_segment = result
|
| 1473 |
+
|
| 1474 |
+
face_segment[...,3] = mask[i]
|
| 1475 |
+
|
| 1476 |
+
result = rgba2rgb_tensor(result)
|
| 1477 |
+
result = result.cpu() # ΠΠ΅ΡΠ΅ΠΌΠ΅ΡΠ°Π΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±ΡΠ°ΡΠ½ΠΎ Π½Π° CPU
|
| 1478 |
+
|
| 1479 |
+
pbar.update(1)
|
| 1480 |
+
|
| 1481 |
+
try:
|
| 1482 |
+
torch.cuda.empty_cache()
|
| 1483 |
+
except:
|
| 1484 |
+
pass
|
| 1485 |
+
|
| 1486 |
+
progress_bar_reset(pbar)
|
| 1487 |
+
|
| 1488 |
+
return (result, combined_mask, mask_blurred, face_segment)
|
| 1489 |
+
|
| 1490 |
+
def iterative_morphology(self, image, distance, op="dilate"):
|
| 1491 |
+
if distance <= 0:
|
| 1492 |
+
return image
|
| 1493 |
+
image = image.unsqueeze(1) # shape [B, 1, H, W] or [1, 1, H, W]
|
| 1494 |
+
for _ in range(distance):
|
| 1495 |
+
if op == "dilate":
|
| 1496 |
+
image = F.max_pool2d(image, kernel_size=3, stride=1, padding=1)
|
| 1497 |
+
elif op == "erode":
|
| 1498 |
+
image = -F.max_pool2d(-image, kernel_size=3, stride=1, padding=1)
|
| 1499 |
+
return image.squeeze(1)
|
| 1500 |
+
|
| 1501 |
+
|
| 1502 |
+
class ImageDublicator:
|
| 1503 |
+
@classmethod
|
| 1504 |
+
def INPUT_TYPES(s):
|
| 1505 |
+
return {
|
| 1506 |
+
"required": {
|
| 1507 |
+
"image": ("IMAGE",),
|
| 1508 |
+
"count": ("INT", {"default": 1, "min": 0}),
|
| 1509 |
+
},
|
| 1510 |
+
}
|
| 1511 |
+
|
| 1512 |
+
RETURN_TYPES = ("IMAGE",)
|
| 1513 |
+
RETURN_NAMES = ("IMAGES",)
|
| 1514 |
+
OUTPUT_IS_LIST = (True,)
|
| 1515 |
+
FUNCTION = "execute"
|
| 1516 |
+
CATEGORY = "π ReActor"
|
| 1517 |
+
|
| 1518 |
+
def execute(self, image, count):
|
| 1519 |
+
images = [image for i in range(count)]
|
| 1520 |
+
return (images,)
|
| 1521 |
+
|
| 1522 |
+
|
| 1523 |
+
class ImageRGBA2RGB:
|
| 1524 |
+
@classmethod
|
| 1525 |
+
def INPUT_TYPES(s):
|
| 1526 |
+
return {
|
| 1527 |
+
"required": {
|
| 1528 |
+
"image": ("IMAGE",),
|
| 1529 |
+
},
|
| 1530 |
+
}
|
| 1531 |
+
|
| 1532 |
+
RETURN_TYPES = ("IMAGE",)
|
| 1533 |
+
FUNCTION = "execute"
|
| 1534 |
+
CATEGORY = "π ReActor"
|
| 1535 |
+
|
| 1536 |
+
def execute(self, image):
|
| 1537 |
+
out = rgba2rgb_tensor(image)
|
| 1538 |
+
return (out,)
|
| 1539 |
+
|
| 1540 |
+
|
| 1541 |
+
class MakeFaceModelBatch:
|
| 1542 |
+
@classmethod
|
| 1543 |
+
def INPUT_TYPES(s):
|
| 1544 |
+
return {
|
| 1545 |
+
"required": {
|
| 1546 |
+
"face_model1": ("FACE_MODEL",),
|
| 1547 |
+
},
|
| 1548 |
+
"optional": {
|
| 1549 |
+
"face_model2": ("FACE_MODEL",),
|
| 1550 |
+
"face_model3": ("FACE_MODEL",),
|
| 1551 |
+
"face_model4": ("FACE_MODEL",),
|
| 1552 |
+
"face_model5": ("FACE_MODEL",),
|
| 1553 |
+
"face_model6": ("FACE_MODEL",),
|
| 1554 |
+
"face_model7": ("FACE_MODEL",),
|
| 1555 |
+
"face_model8": ("FACE_MODEL",),
|
| 1556 |
+
"face_model9": ("FACE_MODEL",),
|
| 1557 |
+
"face_model10": ("FACE_MODEL",),
|
| 1558 |
+
},
|
| 1559 |
+
}
|
| 1560 |
+
|
| 1561 |
+
RETURN_TYPES = ("FACE_MODEL",)
|
| 1562 |
+
RETURN_NAMES = ("FACE_MODELS",)
|
| 1563 |
+
FUNCTION = "execute"
|
| 1564 |
+
|
| 1565 |
+
CATEGORY = "π ReActor"
|
| 1566 |
+
|
| 1567 |
+
def execute(self, **kwargs):
|
| 1568 |
+
if len(kwargs) > 0:
|
| 1569 |
+
face_models = [value for value in kwargs.values()]
|
| 1570 |
+
return (face_models,)
|
| 1571 |
+
else:
|
| 1572 |
+
logger.error("Please provide at least 1 `face_model`")
|
| 1573 |
+
return (None,)
|
| 1574 |
+
|
| 1575 |
+
|
| 1576 |
+
class ReActorOptions:
|
| 1577 |
+
@classmethod
|
| 1578 |
+
def INPUT_TYPES(s):
|
| 1579 |
+
return {
|
| 1580 |
+
"required": {
|
| 1581 |
+
"input_faces_order": (
|
| 1582 |
+
["left-right","right-left","top-bottom","bottom-top","small-large","large-small"], {"default": "large-small"}
|
| 1583 |
+
),
|
| 1584 |
+
"input_faces_index": ("STRING", {"default": "0"}),
|
| 1585 |
+
"detect_gender_input": (["no","female","male"], {"default": "no"}),
|
| 1586 |
+
"source_faces_order": (
|
| 1587 |
+
["left-right","right-left","top-bottom","bottom-top","small-large","large-small"], {"default": "large-small"}
|
| 1588 |
+
),
|
| 1589 |
+
"source_faces_index": ("STRING", {"default": "0"}),
|
| 1590 |
+
"detect_gender_source": (["no","female","male"], {"default": "no"}),
|
| 1591 |
+
"console_log_level": ([0, 1, 2], {"default": 1}),
|
| 1592 |
+
"restore_swapped_only": ("BOOLEAN", {"default": True, "label_off": "no", "label_on": "yes"})
|
| 1593 |
+
}
|
| 1594 |
+
}
|
| 1595 |
+
|
| 1596 |
+
RETURN_TYPES = ("OPTIONS",)
|
| 1597 |
+
FUNCTION = "execute"
|
| 1598 |
+
CATEGORY = "π ReActor"
|
| 1599 |
+
|
| 1600 |
+
def execute(self,input_faces_order, input_faces_index, detect_gender_input, source_faces_order, source_faces_index, detect_gender_source, console_log_level, restore_swapped_only):
|
| 1601 |
+
options: dict = {
|
| 1602 |
+
"input_faces_order": input_faces_order,
|
| 1603 |
+
"input_faces_index": input_faces_index,
|
| 1604 |
+
"detect_gender_input": detect_gender_input,
|
| 1605 |
+
"source_faces_order": source_faces_order,
|
| 1606 |
+
"source_faces_index": source_faces_index,
|
| 1607 |
+
"detect_gender_source": detect_gender_source,
|
| 1608 |
+
"console_log_level": console_log_level,
|
| 1609 |
+
"restore_swapped_only": restore_swapped_only,
|
| 1610 |
+
}
|
| 1611 |
+
return (options, )
|
| 1612 |
+
|
| 1613 |
+
|
| 1614 |
+
class ReActorFaceBoost:
|
| 1615 |
+
@classmethod
|
| 1616 |
+
def INPUT_TYPES(s):
|
| 1617 |
+
return {
|
| 1618 |
+
"required": {
|
| 1619 |
+
"enabled": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
|
| 1620 |
+
"boost_model": (get_model_names(get_restorers),),
|
| 1621 |
+
"interpolation": (["Nearest","Bilinear","Bicubic","Lanczos"], {"default": "Bicubic"}),
|
| 1622 |
+
"visibility": ("FLOAT", {"default": 1, "min": 0.1, "max": 1, "step": 0.05}),
|
| 1623 |
+
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
|
| 1624 |
+
"restore_with_main_after": ("BOOLEAN", {"default": False}),
|
| 1625 |
+
}
|
| 1626 |
+
}
|
| 1627 |
+
|
| 1628 |
+
RETURN_TYPES = ("FACE_BOOST",)
|
| 1629 |
+
FUNCTION = "execute"
|
| 1630 |
+
CATEGORY = "π ReActor"
|
| 1631 |
+
|
| 1632 |
+
def execute(self,enabled,boost_model,interpolation,visibility,codeformer_weight,restore_with_main_after):
|
| 1633 |
+
face_boost: dict = {
|
| 1634 |
+
"enabled": enabled,
|
| 1635 |
+
"boost_model": boost_model,
|
| 1636 |
+
"interpolation": interpolation,
|
| 1637 |
+
"visibility": visibility,
|
| 1638 |
+
"codeformer_weight": codeformer_weight,
|
| 1639 |
+
"restore_with_main_after": restore_with_main_after,
|
| 1640 |
+
}
|
| 1641 |
+
return (face_boost, )
|
| 1642 |
+
|
| 1643 |
+
class ReActorUnload:
|
| 1644 |
+
@classmethod
|
| 1645 |
+
def INPUT_TYPES(s):
|
| 1646 |
+
return {
|
| 1647 |
+
"required": {
|
| 1648 |
+
"trigger": ("IMAGE", ),
|
| 1649 |
+
},
|
| 1650 |
+
}
|
| 1651 |
+
|
| 1652 |
+
RETURN_TYPES = ("IMAGE",)
|
| 1653 |
+
FUNCTION = "execute"
|
| 1654 |
+
CATEGORY = "π ReActor"
|
| 1655 |
+
|
| 1656 |
+
def execute(self, trigger):
|
| 1657 |
+
unload_all_models()
|
| 1658 |
+
return (trigger,)
|
| 1659 |
+
|
| 1660 |
+
|
| 1661 |
+
NODE_CLASS_MAPPINGS = {
|
| 1662 |
+
# --- MAIN NODES ---
|
| 1663 |
+
"ReActorFaceSwap": reactor,
|
| 1664 |
+
"ReActorFaceSwapOpt": ReActorPlusOpt,
|
| 1665 |
+
"ReActorOptions": ReActorOptions,
|
| 1666 |
+
"ReActorFaceBoost": ReActorFaceBoost,
|
| 1667 |
+
"ReActorMaskHelper": MaskHelper,
|
| 1668 |
+
"ReActorSetWeight": ReActorWeight,
|
| 1669 |
+
# --- Operations with Face Models ---
|
| 1670 |
+
"ReActorSaveFaceModel": SaveFaceModel,
|
| 1671 |
+
"ReActorLoadFaceModel": LoadFaceModel,
|
| 1672 |
+
"ReActorBuildFaceModel": BuildFaceModel,
|
| 1673 |
+
"ReActorMakeFaceModelBatch": MakeFaceModelBatch,
|
| 1674 |
+
# --- Additional Nodes ---
|
| 1675 |
+
"ReActorRestoreFace": RestoreFace,
|
| 1676 |
+
"ReActorRestoreFaceAdvanced": RestoreFaceAdvanced,
|
| 1677 |
+
"ReActorImageDublicator": ImageDublicator,
|
| 1678 |
+
"ImageRGBA2RGB": ImageRGBA2RGB,
|
| 1679 |
+
"ReActorUnload": ReActorUnload,
|
| 1680 |
+
}
|
| 1681 |
+
|
| 1682 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 1683 |
+
# --- MAIN NODES ---
|
| 1684 |
+
"ReActorFaceSwap": "ReActor π Fast Face Swap",
|
| 1685 |
+
"ReActorFaceSwapOpt": "ReActor π Fast Face Swap [OPTIONS]",
|
| 1686 |
+
"ReActorOptions": "ReActor π Options",
|
| 1687 |
+
"ReActorFaceBoost": "ReActor π Face Booster",
|
| 1688 |
+
"ReActorMaskHelper": "ReActor π Masking Helper",
|
| 1689 |
+
"ReActorSetWeight": "ReActor π Set Face Swap Weight",
|
| 1690 |
+
# --- Operations with Face Models ---
|
| 1691 |
+
"ReActorSaveFaceModel": "Save Face Model π ReActor",
|
| 1692 |
+
"ReActorLoadFaceModel": "Load Face Model π ReActor",
|
| 1693 |
+
"ReActorBuildFaceModel": "Build Blended Face Model π ReActor",
|
| 1694 |
+
"ReActorMakeFaceModelBatch": "Make Face Model Batch π ReActor",
|
| 1695 |
+
# --- Additional Nodes ---
|
| 1696 |
+
"ReActorRestoreFace": "Restore Face π ReActor",
|
| 1697 |
+
"ReActorRestoreFaceAdvanced": "Restore Face Advanced π ReActor",
|
| 1698 |
+
"ReActorImageDublicator": "Image Dublicator (List) π ReActor",
|
| 1699 |
+
"ImageRGBA2RGB": "Convert RGBA to RGB π ReActor",
|
| 1700 |
+
"ReActorUnload": "Unload ReActor Models π ReActor",
|
| 1701 |
+
}
|