ComfyUI/comfy_extras/nodes/nodes_skeletonize.py
doctorpangloss f1a096b3e1 Merges new frontend
- fixes bfloat16 on cpu to numpy issues
 - extensions should go into comfy/web/extensions/javascript
2024-08-16 15:46:11 -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().float().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,
}