forked from doombeaker/ComfyUI-Inspyrenet-Rembg
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInspyrenet_Rembg.py
More file actions
117 lines (94 loc) · 3.36 KB
/
Copy pathInspyrenet_Rembg.py
File metadata and controls
117 lines (94 loc) · 3.36 KB
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
from PIL import Image
import torch
import numpy as np
from transparent_background import Remover
from tqdm import tqdm
import os
from folder_paths import models_dir, add_model_folder_path, get_folder_paths
# KEEP Naming convention from https://github.com/plemeri/transparent-background/blob/main/transparent_background/Remover.py
# ckpt_dir\ckpt_name
# USE 'ComfyUI\models\transparent-background' like https://github.com/chflame163/ComfyUI_LayerStyle#TransparentBackgroundUltra
# Define the directory for Inspyrenet models
ckpt_dir = os.path.join(models_dir, "transparent-background")
# Ensure the Inspyrenet directory is registered in the paths
try:
if ckpt_dir not in get_folder_paths("transparent-background"):
raise KeyError
except KeyError:
add_model_folder_path("transparent-background", ckpt_dir)
# Check if the inspyrinet_dir exists and is empty
if not os.path.exists(ckpt_dir) or not os.listdir(ckpt_dir):
# Ensure the directory exists
os.makedirs(ckpt_dir, exist_ok=True)
# use
ckpt_name = os.path.join(ckpt_dir, "ckpt_base.pth")
# Tensor to PIL
def tensor2pil(image):
return Image.fromarray(
np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
)
# Convert PIL to Tensor
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
class InspyrenetRemover:
@classmethod
def INPUT_TYPES(s):
return {
"required": {"torchscript_jit": (["default", "on"],)},
}
RETURN_TYPES = ("REMOVER",)
FUNCTION = "init_remover"
CATEGORY = "InspyreNet"
def init_remover(self, torchscript_jit):
if torchscript_jit == "default":
remover = Remover(ckpt=ckpt_name)
else:
remover = Remover(jit=True, ckpt=ckpt_name,)
return (remover,)
class InspyrenetRembg:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {"remover": ("REMOVER",), "image": ("IMAGE",),},
}
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "remove_background"
CATEGORY = "InspyreNet"
def remove_background(self, remover, image):
img_list = []
for img in tqdm(image, "Inspyrenet Rembg"):
mid = remover.process(tensor2pil(img), type="rgba")
out = pil2tensor(mid)
img_list.append(out)
img_stack = torch.cat(img_list, dim=0)
mask = img_stack[:, :, :, 3]
return (img_stack, mask)
class InspyrenetRembgAdvanced:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"remover": ("REMOVER",),
"image": ("IMAGE",),
"threshold": (
"FLOAT",
{"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01},
),
},
}
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "remove_background"
CATEGORY = "InspyreNet"
def remove_background(self, remover, image, threshold):
img_list = []
for img in tqdm(image, "Inspyrenet Rembg"):
mid = remover.process(tensor2pil(img), type="rgba", threshold=threshold)
out = pil2tensor(mid)
img_list.append(out)
img_stack = torch.cat(img_list, dim=0)
mask = img_stack[:, :, :, 3]
return (img_stack, mask)