Skip to content

Commit 424573d

Browse files
authored
Merge branch 'kijai:main' into main
2 parents 9fd47ee + 9d7af91 commit 424573d

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

nodes/batchcrop_nodes.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,19 @@ def INPUT_TYPES(cls):
710710
def visualizebbox(self, bboxes, images, line_width, bbox_format):
711711
image_list = []
712712
for image, bbox in zip(images, bboxes):
713-
if bbox_format == "xywh":
714-
x_min, y_min, width, height = bbox
715-
elif bbox_format == "xyxy":
716-
x_min, y_min, x_max, y_max = bbox
717-
width = x_max - x_min
718-
height = y_max - y_min
713+
# Ensure bbox is a sequence of 4 values
714+
if isinstance(bbox, (list, tuple, np.ndarray)) and len(bbox) == 4:
715+
if bbox_format == "xywh":
716+
x_min, y_min, width, height = bbox
717+
elif bbox_format == "xyxy":
718+
x_min, y_min, x_max, y_max = bbox
719+
width = x_max - x_min
720+
height = y_max - y_min
721+
else:
722+
raise ValueError(f"Unknown bbox_format: {bbox_format}")
719723
else:
720-
raise ValueError(f"Unknown bbox_format: {bbox_format}")
724+
print("Invalid bbox:", bbox)
725+
continue
721726

722727
# Ensure bbox coordinates are integers
723728
x_min = int(x_min)

nodes/image_nodes.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,29 @@ def __init__(cls):
37513751
try:
37523752
cls.vhs_nodes = importlib.import_module("comfyui-videohelpersuite.videohelpersuite")
37533753
except ImportError:
3754-
raise ImportError("This node requires ComfyUI-VideoHelperSuite to be installed.")
3754+
# Fallback to sys.modules search for Windows compatibility
3755+
import sys
3756+
vhs_module = None
3757+
for module_name in sys.modules:
3758+
if 'videohelpersuite' in module_name and 'videohelpersuite' in sys.modules[module_name].__dict__:
3759+
vhs_module = sys.modules[module_name]
3760+
break
3761+
3762+
if vhs_module is None:
3763+
# Try direct access to the videohelpersuite submodule
3764+
for module_name in sys.modules:
3765+
if module_name.endswith('videohelpersuite'):
3766+
vhs_module = sys.modules[module_name]
3767+
break
3768+
3769+
if vhs_module is not None:
3770+
cls.vhs_nodes = vhs_module
3771+
else:
3772+
raise ImportError("This node requires ComfyUI-VideoHelperSuite to be installed.")
3773+
3774+
except ImportError:
3775+
raise ImportError("This node requires ComfyUI-VideoHelperSuite to be installed.")
3776+
37553777
@classmethod
37563778
def INPUT_TYPES(s):
37573779
return {

nodes/mask_nodes.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from ..utility.utility import tensor2pil, pil2tensor
1818

1919
script_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20+
device = model_management.get_torch_device()
21+
offload_device = model_management.unet_offload_device()
2022

2123
class BatchCLIPSeg:
2224

@@ -997,6 +999,7 @@ def INPUT_TYPES(cls):
997999
- fill_holes: fill holes in the mask (slow)"""
9981000

9991001
def expand_mask(self, mask, expand, tapered_corners, flip_input, blur_radius, incremental_expandrate, lerp_alpha, decay_factor, fill_holes=False):
1002+
import kornia.morphology as morph
10001003
alpha = lerp_alpha
10011004
decay = decay_factor
10021005
if flip_input:
@@ -1010,30 +1013,45 @@ def expand_mask(self, mask, expand, tapered_corners, flip_input, blur_radius, in
10101013
previous_output = None
10111014
current_expand = expand
10121015
for m in growmask:
1013-
output = m.numpy().astype(np.float32)
1014-
for _ in range(abs(round(current_expand))):
1015-
if current_expand < 0:
1016-
output = scipy.ndimage.grey_erosion(output, footprint=kernel)
1016+
output = m.unsqueeze(0).unsqueeze(0).to(device) # Add batch and channel dims for kornia
1017+
if abs(round(current_expand)) > 0:
1018+
# Create kernel - kornia expects kernel on same device as input
1019+
if tapered_corners:
1020+
kernel = torch.tensor([[0, 1, 0],
1021+
[1, 1, 1],
1022+
[0, 1, 0]], dtype=torch.float32, device=output.device)
10171023
else:
1018-
output = scipy.ndimage.grey_dilation(output, footprint=kernel)
1024+
kernel = torch.tensor([[1, 1, 1],
1025+
[1, 1, 1],
1026+
[1, 1, 1]], dtype=torch.float32, device=output.device)
1027+
1028+
for _ in range(abs(round(current_expand))):
1029+
if current_expand < 0:
1030+
output = morph.erosion(output, kernel)
1031+
else:
1032+
output = morph.dilation(output, kernel)
1033+
1034+
output = output.squeeze(0).squeeze(0) # Remove batch and channel dims
1035+
10191036
if current_expand < 0:
10201037
current_expand -= abs(incremental_expandrate)
10211038
else:
10221039
current_expand += abs(incremental_expandrate)
1040+
10231041
if fill_holes:
1042+
# For fill_holes, you might need to keep using scipy or implement GPU version
10241043
binary_mask = output > 0
1025-
output = scipy.ndimage.binary_fill_holes(binary_mask)
1026-
output = output.astype(np.float32) * 255
1027-
output = torch.from_numpy(output)
1044+
output_np = binary_mask.cpu().numpy()
1045+
filled = scipy.ndimage.binary_fill_holes(output_np)
1046+
output = torch.from_numpy(filled.astype(np.float32)).to(output.device)
1047+
10281048
if alpha < 1.0 and previous_output is not None:
1029-
# Interpolate between the previous and current frame
10301049
output = alpha * output + (1 - alpha) * previous_output
10311050
if decay < 1.0 and previous_output is not None:
1032-
# Add the decayed previous output to the current frame
10331051
output += decay * previous_output
10341052
output = output / output.max()
10351053
previous_output = output
1036-
out.append(output)
1054+
out.append(output.cpu())
10371055

10381056
if blur_radius != 0:
10391057
# Convert the tensor list to PIL images, apply blur, and convert back

0 commit comments

Comments
 (0)