ComfyUI/comfy_extras/nodes/nodes_skeletonize.py
doctorpangloss 95d47276e9 Improve tests and distributed error notifications
- Tests now perform faster
 - Tests will run on supported GPU platforms
 - Configuration has known issues related to setting up a working
   directory for an embedded client
 - Introduce a Skeletonize node that solves many problems with Canny
 - Improve behavior of exception reporting
2024-07-04 10:16:02 -07:00

53 lines
1.6 KiB
Python

import torch
from skimage.morphology import skeletonize, thin
import comfy.model_management
class SkeletonizeThin:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"image": ("IMAGE",),
"binary_threshold": ("FLOAT", {"default": 0.5, "min": 0.01, "max": 0.99, "step": 0.01}),
"approach": (["skeletonize", "thinning"], {}),
}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process_image"
CATEGORY = "image/preprocessors"
def process_image(self, image, binary_threshold, approach):
use_skeletonize = approach == "skeletonize"
use_thinning = approach == "thinning"
device = comfy.model_management.intermediate_device()
if len(image.shape) == 3:
image = image.unsqueeze(0)
batch_size, height, width, channels = image.shape
if channels == 3:
image = torch.mean(image, dim=-1, keepdim=True)
binary = (image > binary_threshold).float()
results = []
for img in binary:
img_np = img.squeeze().cpu().numpy()
if use_skeletonize:
result = skeletonize(img_np)
elif use_thinning:
result = thin(img_np)
else:
result = img_np
result = torch.from_numpy(result).float().to(device)
result = result.unsqueeze(-1).repeat(1, 1, 3)
results.append(result)
final_result = torch.stack(results).to(comfy.model_management.intermediate_device())
return (final_result,)
NODE_CLASS_MAPPINGS = {
"SkeletonizeThin": SkeletonizeThin,
}