|
import os |
|
import numpy as np |
|
from urllib.request import urlopen |
|
import torchvision.transforms as transforms |
|
|
|
from .utils.file_util import node_path |
|
from .utils.seg_clothes import * |
|
from rembg import remove |
|
|
|
class segformer_remove_bg: |
|
def __init__(self): |
|
pass |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": |
|
{ |
|
"source":("IMAGE", {"default": "","multiline": False}) |
|
} |
|
} |
|
RETURN_TYPES = ("IMAGE","BOOLEAN") |
|
RETURN_NAMES = ("image","open") |
|
OUTPUT_NODE = True |
|
FUNCTION = "sample" |
|
CATEGORY = "CXH" |
|
def sample(self,source): |
|
pil_image = tensor2pil(source) |
|
|
|
o_image = remove(pil_image) |
|
|
|
r= pil2tensor(o_image) |
|
|
|
return r,True |
|
|
|
|
|
class segformer_agnostic: |
|
def __init__(self): |
|
pass |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": |
|
{ |
|
"source":("IMAGE", {"default": "","multiline": False}), |
|
"mask":("MASK", {"default": "","multiline": False}), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("IMAGE","BOOLEAN") |
|
RETURN_NAMES = ("mark_image","open") |
|
OUTPUT_NODE = True |
|
FUNCTION = "sample" |
|
CATEGORY = "CXH" |
|
|
|
def sample(self,source,mask): |
|
|
|
pil_image = tensor2pil(source) |
|
mask_pil = tensor2pil(mask) |
|
|
|
|
|
mask_np = np.array(mask_pil.convert('L')) |
|
_, binary_mask = cv2.threshold(mask_np, 240, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
kernel = np.ones((5, 5), np.uint8) |
|
dilation = cv2.dilate(binary_mask, kernel, iterations=1) |
|
erosion = cv2.erode(dilation, kernel, iterations=1) |
|
|
|
|
|
|
|
|
|
|
|
pil_image_np = np.array(pil_image) |
|
|
|
|
|
pil_image_np[erosion == 255] = [128, 128, 128] |
|
|
|
|
|
result_pil = Image.fromarray(pil_image_np).convert("RGB") |
|
|
|
|
|
return pil2tensor(result_pil),True |
|
|
|
|
|
class segformer_clothes: |
|
|
|
def __init__(self): |
|
pass |
|
|
|
|
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": |
|
{ |
|
"image":("IMAGE", {"default": "","multiline": False}), |
|
"Face": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Hat": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Hair": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Upper_clothes": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Skirt": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Pants": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Dress": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Belt": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"shoe": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"leg": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"arm": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Bag": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
"Scarf": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("IMAGE","BOOLEAN") |
|
RETURN_NAMES = ("mask_image","open") |
|
OUTPUT_NODE = True |
|
FUNCTION = "sample" |
|
CATEGORY = "CXH" |
|
|
|
def sample(self,image,Face,Hat,Hair,Upper_clothes,Skirt,Pants,Dress,Belt,shoe,leg,arm,Bag,Scarf): |
|
|
|
pred_seg,cloth = get_segmentation(image) |
|
labels_to_keep = [0] |
|
|
|
|
|
if not Hat: |
|
labels_to_keep.append(1) |
|
if not Hair: |
|
labels_to_keep.append(2) |
|
if not Upper_clothes: |
|
labels_to_keep.append(4) |
|
if not Skirt: |
|
labels_to_keep.append(5) |
|
if not Pants: |
|
labels_to_keep.append(6) |
|
if not Dress: |
|
labels_to_keep.append(7) |
|
if not Belt: |
|
labels_to_keep.append(8) |
|
if not shoe: |
|
labels_to_keep.append(9) |
|
labels_to_keep.append(10) |
|
if not Face: |
|
labels_to_keep.append(11) |
|
if not leg: |
|
labels_to_keep.append(12) |
|
labels_to_keep.append(13) |
|
if not arm: |
|
labels_to_keep.append(14) |
|
labels_to_keep.append(15) |
|
if not Bag: |
|
labels_to_keep.append(16) |
|
if not Scarf: |
|
labels_to_keep.append(17) |
|
|
|
mask = np.isin(pred_seg, labels_to_keep).astype(np.uint8) |
|
|
|
|
|
mask_image = Image.fromarray(mask * 255) |
|
mask_image = mask_image.convert("RGB") |
|
mask_image = pil2tensor(mask_image) |
|
|
|
|
|
|
|
return (mask_image,True) |