Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mask merging and resizing #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,50 @@ def view_text(self, text):
# Parse the combined JSON string
return {"ui": {"text": text}, "result": (text,)}


class UltralyticsMergeMasks:
def __init__(self):
pass

@classmethod
def INPUT_TYPES(s):
return {
"required": {
"masks": ("MASKS", )
},
"optional": {
"target_image": ("IMAGE", {"default": None})
}
}
RETURN_TYPES = ("MASK",)
FUNCTION = "merge_mask"
CATEGORY = "Ultralytics/MergeMasks"

def merge_mask(self, masks, target_image=None):
if not masks or not masks.xy:
raise ValueError("No valid masks provided")

print(f"mask origin_shape:{masks.orig_shape}")

width, height = masks.orig_shape

b_mask = np.zeros((height, width), np.uint8)

for xy in masks.xy:
contour = xy.astype(np.int32).reshape(-1, 1, 2)
cv2.drawContours(b_mask, [contour], -1, 255, cv2.FILLED)

if (target_image is not None):
height = target_image.shape[1]
width = target_image.shape[2]
b_mask = cv2.resize(b_mask, (width, height), interpolation=cv2.INTER_NEAREST)

print(f"b_mask unique: {np.unique(b_mask)} shape:{b_mask.shape}")

tensor_masks = torch.from_numpy(b_mask)
return (tensor_masks.unsqueeze(0),)


NODE_CLASS_MAPPINGS = {
"UltralyticsModelLoader": UltralyticsModelLoader,
"UltralyticsInference": UltralyticsInference,
Expand All @@ -951,6 +995,7 @@ def view_text(self, text):
"ImageResizeAdvanced": ImageResizeAdvanced,
"BBoxVisNode": BBoxVisNode,
"ViewText": ViewText,
"UltralyticsMergeMasks": UltralyticsMergeMasks
}

NODE_DISPLAY_NAME_MAPPINGS = {
Expand All @@ -966,4 +1011,5 @@ def view_text(self, text):
"ImageResizeAdvanced": "Image Resize Advanced",
"BBoxVisNode": "BBox Visualization",
"ViewText": "View Text",
"UltralyticsMergeMasks": "Ultralystics Merge Mask"
}