Files
kadirnar-ComfyUI-Transformers/huggingface_tasks/computer_vision/image_segmentation/node.py
T

46 lines
1.5 KiB
Python

from transformers import pipeline as hf_pipeline
from PIL import Image
import numpy as np
import torch
image_segmentation_model_name_list = [
"mattmdjaga/segformer_b2_clothes",
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024",
]
class ImageSegmentationPipeline:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"category_name": ("STRING", {"default": ""}),
"model_name": (image_segmentation_model_name_list, {"default": image_segmentation_model_name_list[0]}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("mask",)
FUNCTION = "image_segmentation_pipeline"
CATEGORY = "Transformers/ComputerVision/ImageSegmentation"
def image_segmentation_pipeline(self, image, category_name, model_name):
img = 255.0 * image[0].cpu().numpy()
pil_image = Image.fromarray(np.clip(img, 0, 255).astype(np.uint8))
pipe = hf_pipeline(task="image-segmentation", model=model_name)
result = pipe(pil_image)
for item in result:
if item["label"] == category_name:
mask = item["mask"]
arr = np.array(mask).astype(np.float32) / 255.0
if len(arr.shape) == 2:
arr = np.stack([arr, arr, arr], axis=-1)
return (torch.from_numpy(arr)[None,],)
h, w = pil_image.size[1], pil_image.size[0]
empty = torch.zeros(1, h, w, 3)
return (empty,)