Skip to content

Commit 0a7048f

Browse files
committed
(minor) Simplify GroundedSAMInvocation._merge_masks(...).
1 parent e8ecf5e commit 0a7048f

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

invokeai/app/invocations/grounded_sam.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def invoke(self, context: InvocationContext) -> ImageOutput:
7474

7575
detections = self._filter_detections(detections)
7676
masks = [detection.mask for detection in detections]
77-
combined_mask = self._merge_masks(masks)
77+
# masks contains binary values of 0 or 1, so we merge them via max-reduce.
78+
combined_mask = np.maximum.reduce(masks)
7879

7980
# Map [0, 1] to [0, 255].
8081
mask_np = combined_mask * 255
@@ -188,10 +189,3 @@ def _filter_detections(self, detections: list[DetectionResult]) -> list[Detectio
188189
return [max(detections, key=lambda x: x.score)]
189190
else:
190191
raise ValueError(f"Invalid mask filter: {self.mask_filter}")
191-
192-
def _merge_masks(self, masks: list[npt.NDArray[np.uint8]]) -> npt.NDArray[np.uint8]:
193-
"""Merge multiple masks into a single mask."""
194-
# Merge all masks together.
195-
stacked_mask = np.stack(masks, axis=0)
196-
combined_mask = np.max(stacked_mask, axis=0)
197-
return combined_mask

0 commit comments

Comments
 (0)