forked from Gourieff/comfyui-reactor-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.py
1215 lines (994 loc) · 49 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, glob, sys
import logging
import torch
import torch.nn.functional as torchfn
from torchvision.transforms.functional import normalize
from torchvision.ops import masks_to_boxes
import numpy as np
import cv2
import math
from typing import List
from PIL import Image
from scipy import stats
from insightface.app.common import Face
from segment_anything import sam_model_registry
from modules.processing import StableDiffusionProcessingImg2Img
from modules.shared import state
# from comfy_extras.chainner_models import model_loading
import comfy.model_management as model_management
import comfy.utils
import folder_paths
import scripts.reactor_version
from r_chainner import model_loading
from scripts.reactor_faceswap import (
FaceSwapScript,
get_models,
get_current_faces_model,
analyze_faces,
half_det_size,
providers
)
from scripts.reactor_logger import logger
from reactor_utils import (
batch_tensor_to_pil,
batched_pil_to_tensor,
tensor_to_pil,
img2tensor,
tensor2img,
save_face_model,
load_face_model,
download,
set_ort_session,
prepare_cropped_face,
normalize_cropped_face,
add_folder_path_and_extensions,
rgba2rgb_tensor
)
from reactor_patcher import apply_patch
from r_facelib.utils.face_restoration_helper import FaceRestoreHelper
from r_basicsr.utils.registry import ARCH_REGISTRY
import scripts.r_archs.codeformer_arch
import scripts.r_masking.subcore as subcore
import scripts.r_masking.core as core
import scripts.r_masking.segs as masking_segs
models_dir = folder_paths.models_dir
REACTOR_MODELS_PATH = os.path.join(models_dir, "reactor")
FACE_MODELS_PATH = os.path.join(REACTOR_MODELS_PATH, "faces")
if not os.path.exists(REACTOR_MODELS_PATH):
os.makedirs(REACTOR_MODELS_PATH)
if not os.path.exists(FACE_MODELS_PATH):
os.makedirs(FACE_MODELS_PATH)
dir_facerestore_models = os.path.join(models_dir, "facerestore_models")
os.makedirs(dir_facerestore_models, exist_ok=True)
folder_paths.folder_names_and_paths["facerestore_models"] = ([dir_facerestore_models], folder_paths.supported_pt_extensions)
BLENDED_FACE_MODEL = None
FACE_SIZE: int = 512
FACE_HELPER = None
if "ultralytics" not in folder_paths.folder_names_and_paths:
add_folder_path_and_extensions("ultralytics_bbox", [os.path.join(models_dir, "ultralytics", "bbox")], folder_paths.supported_pt_extensions)
add_folder_path_and_extensions("ultralytics_segm", [os.path.join(models_dir, "ultralytics", "segm")], folder_paths.supported_pt_extensions)
add_folder_path_and_extensions("ultralytics", [os.path.join(models_dir, "ultralytics")], folder_paths.supported_pt_extensions)
if "sams" not in folder_paths.folder_names_and_paths:
add_folder_path_and_extensions("sams", [os.path.join(models_dir, "sams")], folder_paths.supported_pt_extensions)
def get_facemodels():
models_path = os.path.join(FACE_MODELS_PATH, "*")
models = glob.glob(models_path)
models = [x for x in models if x.endswith(".safetensors")]
return models
def get_restorers():
models_path = os.path.join(models_dir, "facerestore_models/*")
models = glob.glob(models_path)
models = [x for x in models if (x.endswith(".pth") or x.endswith(".onnx"))]
# if len(models) == 0:
# fr_urls = [
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GFPGANv1.3.pth",
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GFPGANv1.4.pth",
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/codeformer-v0.1.0.pth",
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GPEN-BFR-512.onnx",
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GPEN-BFR-1024.onnx",
# "https://huggingface.co/datasets/Gourieff/ReActor/resolve/main/models/facerestore_models/GPEN-BFR-2048.onnx",
# ]
# for model_url in fr_urls:
# model_name = os.path.basename(model_url)
# model_path = os.path.join(dir_facerestore_models, model_name)
# download(model_url, model_path, model_name)
# models = glob.glob(models_path)
# models = [x for x in models if (x.endswith(".pth") or x.endswith(".onnx"))]
return models
def get_model_names(get_models):
models = get_models()
names = []
for x in models:
names.append(os.path.basename(x))
names.sort(key=str.lower)
names.insert(0, "none")
return names
def model_names():
models = get_models()
return {os.path.basename(x): x for x in models}
class reactor:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"enabled": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
"input_image": ("IMAGE",),
"swap_model": (list(model_names().keys()),),
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
"face_restore_model": (get_model_names(get_restorers),),
"face_restore_visibility": ("FLOAT", {"default": 1, "min": 0.1, "max": 1, "step": 0.05}),
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
"detect_gender_input": (["no","female","male"], {"default": "no"}),
"detect_gender_source": (["no","female","male"], {"default": "no"}),
"input_faces_index": ("STRING", {"default": "0"}),
"source_faces_index": ("STRING", {"default": "0"}),
"console_log_level": ([0, 1, 2], {"default": 1}),
},
"optional": {
"source_image": ("IMAGE",),
"face_model": ("FACE_MODEL",),
"face_boost": ("FACE_BOOST",),
},
"hidden": {"faces_order": "FACES_ORDER"},
}
RETURN_TYPES = ("IMAGE","FACE_MODEL")
FUNCTION = "execute"
CATEGORY = "🌌 ReActor"
def __init__(self):
# self.face_helper = None
self.faces_order = ["large-small", "large-small"]
# self.face_size = FACE_SIZE
self.face_boost_enabled = False
self.restore = True
self.boost_model = None
self.interpolation = "Bicubic"
self.boost_model_visibility = 1
self.boost_cf_weight = 0.5
def restore_face(
self,
input_image,
face_restore_model,
face_restore_visibility,
codeformer_weight,
facedetection,
):
result = input_image
if face_restore_model != "none" and not model_management.processing_interrupted():
global FACE_SIZE, FACE_HELPER
self.face_helper = FACE_HELPER
faceSize = 512
if "1024" in face_restore_model.lower():
faceSize = 1024
elif "2048" in face_restore_model.lower():
faceSize = 2048
logger.status(f"Restoring with {face_restore_model} | Face Size is set to {faceSize}")
model_path = folder_paths.get_full_path("facerestore_models", face_restore_model)
device = model_management.get_torch_device()
if "codeformer" in face_restore_model.lower():
codeformer_net = ARCH_REGISTRY.get("CodeFormer")(
dim_embd=512,
codebook_size=1024,
n_head=8,
n_layers=9,
connect_list=["32", "64", "128", "256"],
).to(device)
checkpoint = torch.load(model_path)["params_ema"]
codeformer_net.load_state_dict(checkpoint)
facerestore_model = codeformer_net.eval()
elif ".onnx" in face_restore_model:
ort_session = set_ort_session(model_path, providers=providers)
ort_session_inputs = {}
facerestore_model = ort_session
else:
sd = comfy.utils.load_torch_file(model_path, safe_load=True)
facerestore_model = model_loading.load_state_dict(sd).eval()
facerestore_model.to(device)
if faceSize != FACE_SIZE or self.face_helper is None:
self.face_helper = FaceRestoreHelper(1, face_size=faceSize, crop_ratio=(1, 1), det_model=facedetection, save_ext='png', use_parse=True, device=device)
FACE_SIZE = faceSize
FACE_HELPER = self.face_helper
image_np = 255. * result.numpy()
total_images = image_np.shape[0]
out_images = []
for i in range(total_images):
if total_images > 1:
logger.status(f"Restoring {i+1}")
cur_image_np = image_np[i,:, :, ::-1]
original_resolution = cur_image_np.shape[0:2]
if facerestore_model is None or self.face_helper is None:
return result
self.face_helper.clean_all()
self.face_helper.read_image(cur_image_np)
self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
self.face_helper.align_warp_face()
restored_face = None
for idx, cropped_face in enumerate(self.face_helper.cropped_faces):
# if ".pth" in face_restore_model:
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
try:
with torch.no_grad():
if ".onnx" in face_restore_model: # ONNX models
for ort_session_input in ort_session.get_inputs():
if ort_session_input.name == "input":
cropped_face_prep = prepare_cropped_face(cropped_face)
ort_session_inputs[ort_session_input.name] = cropped_face_prep
if ort_session_input.name == "weight":
weight = np.array([ 1 ], dtype = np.double)
ort_session_inputs[ort_session_input.name] = weight
output = ort_session.run(None, ort_session_inputs)[0][0]
restored_face = normalize_cropped_face(output)
else: # PTH models
output = facerestore_model(cropped_face_t, w=codeformer_weight)[0] if "codeformer" in face_restore_model.lower() else facerestore_model(cropped_face_t)[0]
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
del output
torch.cuda.empty_cache()
except Exception as error:
print(f"\tFailed inference: {error}", file=sys.stderr)
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
if face_restore_visibility < 1:
restored_face = cropped_face * (1 - face_restore_visibility) + restored_face * face_restore_visibility
restored_face = restored_face.astype("uint8")
self.face_helper.add_restored_face(restored_face)
self.face_helper.get_inverse_affine(None)
restored_img = self.face_helper.paste_faces_to_input_image()
restored_img = restored_img[:, :, ::-1]
if original_resolution != restored_img.shape[0:2]:
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)
self.face_helper.clean_all()
# out_images[i] = restored_img
out_images.append(restored_img)
if state.interrupted or model_management.processing_interrupted():
logger.status("Interrupted by User")
return input_image
restored_img_np = np.array(out_images).astype(np.float32) / 255.0
restored_img_tensor = torch.from_numpy(restored_img_np)
result = restored_img_tensor
return result
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):
if face_boost is not None:
self.face_boost_enabled = face_boost["enabled"]
self.boost_model = face_boost["boost_model"]
self.interpolation = face_boost["interpolation"]
self.boost_model_visibility = face_boost["visibility"]
self.boost_cf_weight = face_boost["codeformer_weight"]
self.restore = face_boost["restore_with_main_after"]
else:
self.face_boost_enabled = False
if faces_order is None:
faces_order = self.faces_order
apply_patch(console_log_level)
if not enabled:
return (input_image,face_model)
elif source_image is None and face_model is None:
logger.error("Please provide 'source_image' or `face_model`")
return (input_image,face_model)
if face_model == "none":
face_model = None
script = FaceSwapScript()
pil_images = batch_tensor_to_pil(input_image)
if source_image is not None:
source = tensor_to_pil(source_image)
else:
source = None
p = StableDiffusionProcessingImg2Img(pil_images)
script.process(
p=p,
img=source,
enable=True,
source_faces_index=source_faces_index,
faces_index=input_faces_index,
model=swap_model,
swap_in_source=True,
swap_in_generated=True,
gender_source=detect_gender_source,
gender_target=detect_gender_input,
face_model=face_model,
faces_order=faces_order,
# face boost:
face_boost_enabled=self.face_boost_enabled,
face_restore_model=self.boost_model,
face_restore_visibility=self.boost_model_visibility,
codeformer_weight=self.boost_cf_weight,
interpolation=self.interpolation,
)
result = batched_pil_to_tensor(p.init_images)
if face_model is None:
current_face_model = get_current_faces_model()
face_model_to_provide = current_face_model[0] if (current_face_model is not None and len(current_face_model) > 0) else face_model
else:
face_model_to_provide = face_model
if self.restore or not self.face_boost_enabled:
result = reactor.restore_face(self,result,face_restore_model,face_restore_visibility,codeformer_weight,facedetection)
return (result,face_model_to_provide)
class ReActorPlusOpt:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"enabled": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
"input_image": ("IMAGE",),
"swap_model": (list(model_names().keys()),),
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
"face_restore_model": (get_model_names(get_restorers),),
"face_restore_visibility": ("FLOAT", {"default": 1, "min": 0.1, "max": 1, "step": 0.05}),
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
},
"optional": {
"source_image": ("IMAGE",),
"face_model": ("FACE_MODEL",),
"options": ("OPTIONS",),
"face_boost": ("FACE_BOOST",),
}
}
RETURN_TYPES = ("IMAGE","FACE_MODEL")
FUNCTION = "execute"
CATEGORY = "🌌 ReActor"
def __init__(self):
# self.face_helper = None
self.faces_order = ["large-small", "large-small"]
self.detect_gender_input = "no"
self.detect_gender_source = "no"
self.input_faces_index = "0"
self.source_faces_index = "0"
self.console_log_level = 1
# self.face_size = 512
self.face_boost_enabled = False
self.restore = True
self.boost_model = None
self.interpolation = "Bicubic"
self.boost_model_visibility = 1
self.boost_cf_weight = 0.5
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):
if options is not None:
self.faces_order = [options["input_faces_order"], options["source_faces_order"]]
self.console_log_level = options["console_log_level"]
self.detect_gender_input = options["detect_gender_input"]
self.detect_gender_source = options["detect_gender_source"]
self.input_faces_index = options["input_faces_index"]
self.source_faces_index = options["source_faces_index"]
if face_boost is not None:
self.face_boost_enabled = face_boost["enabled"]
self.restore = face_boost["restore_with_main_after"]
else:
self.face_boost_enabled = False
result = reactor.execute(
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
)
return result
class LoadFaceModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"face_model": (get_model_names(get_facemodels),),
}
}
RETURN_TYPES = ("FACE_MODEL",)
FUNCTION = "load_model"
CATEGORY = "🌌 ReActor"
def load_model(self, face_model):
self.face_model = face_model
self.face_models_path = FACE_MODELS_PATH
if self.face_model != "none":
face_model_path = os.path.join(self.face_models_path, self.face_model)
out = load_face_model(face_model_path)
else:
out = None
return (out, )
class BuildFaceModel:
def __init__(self):
self.output_dir = FACE_MODELS_PATH
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"save_mode": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
"send_only": ("BOOLEAN", {"default": False, "label_off": "NO", "label_on": "YES"}),
"face_model_name": ("STRING", {"default": "default"}),
"compute_method": (["Mean", "Median", "Mode"], {"default": "Mean"}),
},
"optional": {
"images": ("IMAGE",),
"face_models": ("FACE_MODEL",),
}
}
RETURN_TYPES = ("FACE_MODEL",)
FUNCTION = "blend_faces"
OUTPUT_NODE = True
CATEGORY = "🌌 ReActor"
def build_face_model(self, image: Image.Image, det_size=(640, 640)):
logging.StreamHandler.terminator = "\n"
if image is None:
error_msg = "Please load an Image"
logger.error(error_msg)
return error_msg
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
face_model = analyze_faces(image, det_size)
if len(face_model) == 0:
print("")
det_size_half = half_det_size(det_size)
face_model = analyze_faces(image, det_size_half)
if face_model is not None and len(face_model) > 0:
print("...........................................................", end=" ")
if face_model is not None and len(face_model) > 0:
return face_model[0]
else:
no_face_msg = "No face found, please try another image"
# logger.error(no_face_msg)
return no_face_msg
def blend_faces(self, save_mode, send_only, face_model_name, compute_method, images=None, face_models=None):
global BLENDED_FACE_MODEL
blended_face: Face = BLENDED_FACE_MODEL
if send_only and blended_face is None:
send_only = False
if (images is not None or face_models is not None) and not send_only:
faces = []
embeddings = []
apply_patch(1)
if images is not None:
images_list: List[Image.Image] = batch_tensor_to_pil(images)
n = len(images_list)
for i,image in enumerate(images_list):
logging.StreamHandler.terminator = " "
logger.status(f"Building Face Model {i+1} of {n}...")
face = self.build_face_model(image)
if isinstance(face, str):
logger.error(f"No faces found in image {i+1}, skipping")
continue
else:
print(f"{int(((i+1)/n)*100)}%")
faces.append(face)
embeddings.append(face.embedding)
elif face_models is not None:
n = len(face_models)
for i,face_model in enumerate(face_models):
logging.StreamHandler.terminator = " "
logger.status(f"Extracting Face Model {i+1} of {n}...")
face = face_model
if isinstance(face, str):
logger.error(f"No faces found for face_model {i+1}, skipping")
continue
else:
print(f"{int(((i+1)/n)*100)}%")
faces.append(face)
embeddings.append(face.embedding)
logging.StreamHandler.terminator = "\n"
if len(faces) > 0:
# compute_method_name = "Mean" if compute_method == 0 else "Median" if compute_method == 1 else "Mode"
logger.status(f"Blending with Compute Method '{compute_method}'...")
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)
blended_face = Face(
bbox=faces[0].bbox,
kps=faces[0].kps,
det_score=faces[0].det_score,
landmark_3d_68=faces[0].landmark_3d_68,
pose=faces[0].pose,
landmark_2d_106=faces[0].landmark_2d_106,
embedding=blended_embedding,
gender=faces[0].gender,
age=faces[0].age
)
if blended_face is not None:
BLENDED_FACE_MODEL = blended_face
if save_mode:
face_model_path = os.path.join(FACE_MODELS_PATH, face_model_name + ".safetensors")
save_face_model(blended_face,face_model_path)
# done_msg = f"Face model has been saved to '{face_model_path}'"
# logger.status(done_msg)
logger.status("--Done!--")
# return (blended_face,)
else:
no_face_msg = "Something went wrong, please try another set of images"
logger.error(no_face_msg)
# return (blended_face,)
# logger.status("--Done!--")
if images is None and face_models is None:
logger.error("Please provide `images` or `face_models`")
return (blended_face,)
class SaveFaceModel:
def __init__(self):
self.output_dir = FACE_MODELS_PATH
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"save_mode": ("BOOLEAN", {"default": True, "label_off": "OFF", "label_on": "ON"}),
"face_model_name": ("STRING", {"default": "default"}),
"select_face_index": ("INT", {"default": 0, "min": 0}),
},
"optional": {
"image": ("IMAGE",),
"face_model": ("FACE_MODEL",),
}
}
RETURN_TYPES = ()
FUNCTION = "save_model"
OUTPUT_NODE = True
CATEGORY = "🌌 ReActor"
def save_model(self, save_mode, face_model_name, select_face_index, image=None, face_model=None, det_size=(640, 640)):
if save_mode and image is not None:
source = tensor_to_pil(image)
source = cv2.cvtColor(np.array(source), cv2.COLOR_RGB2BGR)
apply_patch(1)
logger.status("Building Face Model...")
face_model_raw = analyze_faces(source, det_size)
if len(face_model_raw) == 0:
det_size_half = half_det_size(det_size)
face_model_raw = analyze_faces(source, det_size_half)
try:
face_model = face_model_raw[select_face_index]
except:
logger.error("No face(s) found")
return face_model_name
logger.status("--Done!--")
if save_mode and (face_model != "none" or face_model is not None):
face_model_path = os.path.join(self.output_dir, face_model_name + ".safetensors")
save_face_model(face_model,face_model_path)
if image is None and face_model is None:
logger.error("Please provide `face_model` or `image`")
return face_model_name
class RestoreFace:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"facedetection": (["retinaface_resnet50", "retinaface_mobile0.25", "YOLOv5l", "YOLOv5n"],),
"model": (get_model_names(get_restorers),),
"visibility": ("FLOAT", {"default": 1, "min": 0.0, "max": 1, "step": 0.05}),
"codeformer_weight": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1, "step": 0.05}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "🌌 ReActor"
# def __init__(self):
# self.face_helper = None
# self.face_size = 512
def execute(self, image, model, visibility, codeformer_weight, facedetection):
result = reactor.restore_face(self,image,model,visibility,codeformer_weight,facedetection)
return (result,)
class MaskHelper:
def __init__(self):
# self.threshold = 0.5
# self.dilation = 10
# self.crop_factor = 3.0
# self.drop_size = 1
self.labels = "all"
self.detailer_hook = None
self.device_mode = "AUTO"
self.detection_hint = "center-1"
# self.sam_dilation = 0
# self.sam_threshold = 0.93
# self.bbox_expansion = 0
# self.mask_hint_threshold = 0.7
# self.mask_hint_use_negative = "False"
# self.force_resize_width = 0
# self.force_resize_height = 0
# self.resize_behavior = "source_size"
@classmethod
def INPUT_TYPES(s):
bboxs = ["bbox/"+x for x in folder_paths.get_filename_list("ultralytics_bbox")]
segms = ["segm/"+x for x in folder_paths.get_filename_list("ultralytics_segm")]
sam_models = [x for x in folder_paths.get_filename_list("sams") if 'hq' not in x]
return {
"required": {
"image": ("IMAGE",),
"swapped_image": ("IMAGE",),
"bbox_model_name": (bboxs + segms, ),
"bbox_threshold": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
"bbox_dilation": ("INT", {"default": 10, "min": -512, "max": 512, "step": 1}),
"bbox_crop_factor": ("FLOAT", {"default": 3.0, "min": 1.0, "max": 100, "step": 0.1}),
"bbox_drop_size": ("INT", {"min": 1, "max": 8192, "step": 1, "default": 10}),
"sam_model_name": (sam_models, ),
"sam_dilation": ("INT", {"default": 0, "min": -512, "max": 512, "step": 1}),
"sam_threshold": ("FLOAT", {"default": 0.93, "min": 0.0, "max": 1.0, "step": 0.01}),
"bbox_expansion": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}),
"mask_hint_threshold": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
"mask_hint_use_negative": (["False", "Small", "Outter"], ),
"morphology_operation": (["dilate", "erode", "open", "close"],),
"morphology_distance": ("INT", {"default": 0, "min": 0, "max": 128, "step": 1}),
"blur_radius": ("INT", {"default": 9, "min": 0, "max": 48, "step": 1}),
"sigma_factor": ("FLOAT", {"default": 1.0, "min": 0.01, "max": 3., "step": 0.01}),
},
"optional": {
"mask_optional": ("MASK",),
}
}
RETURN_TYPES = ("IMAGE","MASK","IMAGE","IMAGE")
RETURN_NAMES = ("IMAGE","MASK","MASK_PREVIEW","SWAPPED_FACE")
FUNCTION = "execute"
CATEGORY = "🌌 ReActor"
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):
# images = [image[i:i + 1, ...] for i in range(image.shape[0])]
images = image
if mask_optional is None:
bbox_model_path = folder_paths.get_full_path("ultralytics", bbox_model_name)
bbox_model = subcore.load_yolo(bbox_model_path)
bbox_detector = subcore.UltraBBoxDetector(bbox_model)
segs = bbox_detector.detect(images, bbox_threshold, bbox_dilation, bbox_crop_factor, bbox_drop_size, self.detailer_hook)
if isinstance(self.labels, list):
self.labels = str(self.labels[0])
if self.labels is not None and self.labels != '':
self.labels = self.labels.split(',')
if len(self.labels) > 0:
segs, _ = masking_segs.filter(segs, self.labels)
# segs, _ = masking_segs.filter(segs, "all")
sam_modelname = folder_paths.get_full_path("sams", sam_model_name)
if 'vit_h' in sam_model_name:
model_kind = 'vit_h'
elif 'vit_l' in sam_model_name:
model_kind = 'vit_l'
else:
model_kind = 'vit_b'
sam = sam_model_registry[model_kind](checkpoint=sam_modelname)
size = os.path.getsize(sam_modelname)
sam.safe_to = core.SafeToGPU(size)
device = model_management.get_torch_device()
sam.safe_to.to_device(sam, device)
sam.is_auto_mode = self.device_mode == "AUTO"
combined_mask, _ = core.make_sam_mask_segmented(sam, segs, images, self.detection_hint, sam_dilation, sam_threshold, bbox_expansion, mask_hint_threshold, mask_hint_use_negative)
else:
combined_mask = mask_optional
# *** MASK TO IMAGE ***:
mask_image = combined_mask.reshape((-1, 1, combined_mask.shape[-2], combined_mask.shape[-1])).movedim(1, -1).expand(-1, -1, -1, 3)
# *** MASK MORPH ***:
mask_image = core.tensor2mask(mask_image)
if morphology_operation == "dilate":
mask_image = self.dilate(mask_image, morphology_distance)
elif morphology_operation == "erode":
mask_image = self.erode(mask_image, morphology_distance)
elif morphology_operation == "open":
mask_image = self.erode(mask_image, morphology_distance)
mask_image = self.dilate(mask_image, morphology_distance)
elif morphology_operation == "close":
mask_image = self.dilate(mask_image, morphology_distance)
mask_image = self.erode(mask_image, morphology_distance)
# *** MASK BLUR ***:
if len(mask_image.size()) == 3:
mask_image = mask_image.unsqueeze(3)
mask_image = mask_image.permute(0, 3, 1, 2)
kernel_size = blur_radius * 2 + 1
sigma = sigma_factor * (0.6 * blur_radius - 0.3)
mask_image_final = self.gaussian_blur(mask_image, kernel_size, sigma).permute(0, 2, 3, 1)
if mask_image_final.size()[3] == 1:
mask_image_final = mask_image_final[:, :, :, 0]
# *** CUT BY MASK ***:
if len(swapped_image.shape) < 4:
C = 1
else:
C = swapped_image.shape[3]
# We operate on RGBA to keep the code clean and then convert back after
swapped_image = core.tensor2rgba(swapped_image)
mask = core.tensor2mask(mask_image_final)
# Scale the mask to be a matching size if it isn't
B, H, W, _ = swapped_image.shape
mask = torch.nn.functional.interpolate(mask.unsqueeze(1), size=(H, W), mode='nearest')[:,0,:,:]
MB, _, _ = mask.shape
if MB < B:
assert(B % MB == 0)
mask = mask.repeat(B // MB, 1, 1)
# masks_to_boxes errors if the tensor is all zeros, so we'll add a single pixel and zero it out at the end
is_empty = ~torch.gt(torch.max(torch.reshape(mask,[MB, H * W]), dim=1).values, 0.)
mask[is_empty,0,0] = 1.
boxes = masks_to_boxes(mask)
mask[is_empty,0,0] = 0.
min_x = boxes[:,0]
min_y = boxes[:,1]
max_x = boxes[:,2]
max_y = boxes[:,3]
width = max_x - min_x + 1
height = max_y - min_y + 1
use_width = int(torch.max(width).item())
use_height = int(torch.max(height).item())
# if self.force_resize_width > 0:
# use_width = self.force_resize_width
# if self.force_resize_height > 0:
# use_height = self.force_resize_height
alpha_mask = torch.ones((B, H, W, 4))
alpha_mask[:,:,:,3] = mask
swapped_image = swapped_image * alpha_mask
cutted_image = torch.zeros((B, use_height, use_width, 4))
for i in range(0, B):
if not is_empty[i]:
ymin = int(min_y[i].item())
ymax = int(max_y[i].item())
xmin = int(min_x[i].item())
xmax = int(max_x[i].item())
single = (swapped_image[i, ymin:ymax+1, xmin:xmax+1,:]).unsqueeze(0)
resized = torch.nn.functional.interpolate(single.permute(0, 3, 1, 2), size=(use_height, use_width), mode='bicubic').permute(0, 2, 3, 1)
cutted_image[i] = resized[0]
# Preserve our type unless we were previously RGB and added non-opaque alpha due to the mask size
if C == 1:
cutted_image = core.tensor2mask(cutted_image)
elif C == 3 and torch.min(cutted_image[:,:,:,3]) == 1:
cutted_image = core.tensor2rgb(cutted_image)
# *** PASTE BY MASK ***:
image_base = core.tensor2rgba(images)
image_to_paste = core.tensor2rgba(cutted_image)
mask = core.tensor2mask(mask_image_final)
# Scale the mask to be a matching size if it isn't
B, H, W, C = image_base.shape
MB = mask.shape[0]
PB = image_to_paste.shape[0]
if B < PB:
assert(PB % B == 0)
image_base = image_base.repeat(PB // B, 1, 1, 1)
B, H, W, C = image_base.shape
if MB < B:
assert(B % MB == 0)
mask = mask.repeat(B // MB, 1, 1)
elif B < MB:
assert(MB % B == 0)
image_base = image_base.repeat(MB // B, 1, 1, 1)
if PB < B:
assert(B % PB == 0)
image_to_paste = image_to_paste.repeat(B // PB, 1, 1, 1)
mask = torch.nn.functional.interpolate(mask.unsqueeze(1), size=(H, W), mode='nearest')[:,0,:,:]
MB, MH, MW = mask.shape
# masks_to_boxes errors if the tensor is all zeros, so we'll add a single pixel and zero it out at the end
is_empty = ~torch.gt(torch.max(torch.reshape(mask,[MB, MH * MW]), dim=1).values, 0.)
mask[is_empty,0,0] = 1.
boxes = masks_to_boxes(mask)
mask[is_empty,0,0] = 0.
min_x = boxes[:,0]
min_y = boxes[:,1]
max_x = boxes[:,2]
max_y = boxes[:,3]
mid_x = (min_x + max_x) / 2
mid_y = (min_y + max_y) / 2
target_width = max_x - min_x + 1
target_height = max_y - min_y + 1
result = image_base.detach().clone()
face_segment = mask_image_final
for i in range(0, MB):
if is_empty[i]:
continue
else:
image_index = i
source_size = image_to_paste.size()
SB, SH, SW, _ = image_to_paste.shape
# Figure out the desired size
width = int(target_width[i].item())
height = int(target_height[i].item())
# if self.resize_behavior == "keep_ratio_fill":
# target_ratio = width / height
# actual_ratio = SW / SH
# if actual_ratio > target_ratio:
# width = int(height * actual_ratio)
# elif actual_ratio < target_ratio:
# height = int(width / actual_ratio)
# elif self.resize_behavior == "keep_ratio_fit":
# target_ratio = width / height
# actual_ratio = SW / SH
# if actual_ratio > target_ratio:
# height = int(width / actual_ratio)
# elif actual_ratio < target_ratio:
# width = int(height * actual_ratio)
# elif self.resize_behavior == "source_size" or self.resize_behavior == "source_size_unmasked":
width = SW
height = SH
# Resize the image we're pasting if needed
resized_image = image_to_paste[i].unsqueeze(0)
# if SH != height or SW != width:
# resized_image = torch.nn.functional.interpolate(resized_image.permute(0, 3, 1, 2), size=(height,width), mode='bicubic').permute(0, 2, 3, 1)
pasting = torch.ones([H, W, C])
ymid = float(mid_y[i].item())
ymin = int(math.floor(ymid - height / 2)) + 1
ymax = int(math.floor(ymid + height / 2)) + 1
xmid = float(mid_x[i].item())
xmin = int(math.floor(xmid - width / 2)) + 1
xmax = int(math.floor(xmid + width / 2)) + 1
_, source_ymax, source_xmax, _ = resized_image.shape
source_ymin, source_xmin = 0, 0
if xmin < 0:
source_xmin = abs(xmin)
xmin = 0
if ymin < 0:
source_ymin = abs(ymin)
ymin = 0
if xmax > W:
source_xmax -= (xmax - W)
xmax = W
if ymax > H:
source_ymax -= (ymax - H)
ymax = H
pasting[ymin:ymax, xmin:xmax, :] = resized_image[0, source_ymin:source_ymax, source_xmin:source_xmax, :]
pasting[:, :, 3] = 1.
pasting_alpha = torch.zeros([H, W])
pasting_alpha[ymin:ymax, xmin:xmax] = resized_image[0, source_ymin:source_ymax, source_xmin:source_xmax, 3]
# if self.resize_behavior == "keep_ratio_fill" or self.resize_behavior == "source_size_unmasked":
# # If we explicitly want to fill the area, we are ok with extending outside
# paste_mask = pasting_alpha.unsqueeze(2).repeat(1, 1, 4)
# else:
# paste_mask = torch.min(pasting_alpha, mask[i]).unsqueeze(2).repeat(1, 1, 4)
paste_mask = torch.min(pasting_alpha, mask[i]).unsqueeze(2).repeat(1, 1, 4)
result[image_index] = pasting * paste_mask + result[image_index] * (1. - paste_mask)
face_segment = result
face_segment[...,3] = mask[i]
result = rgba2rgb_tensor(result)