| 6 | |
| 7 | @VariantSupport() |
| 8 | class BatchImageLoopOpen: |
| 9 | def __init__(self): |
| 10 | pass |
| 11 | |
| 12 | @classmethod |
| 13 | def INPUT_TYPES(cls): |
| 14 | inputs = { |
| 15 | "required": { |
| 16 | "segmented_images": ("IMAGE", {"forceInput": True}), |
| 17 | "segmented_masks": ("MASK", {"forceInput": True}), |
| 18 | }, |
| 19 | "hidden": { |
| 20 | "unique_id": "UNIQUE_ID", |
| 21 | "iteration_count": ("INT", {"default": 0}), |
| 22 | "previous_image": ("IMAGE",), # 新增:接收上一次循环的图片 |
| 23 | } |
| 24 | } |
| 25 | return inputs |
| 26 | |
| 27 | RETURN_TYPES = tuple(["FLOW_CONTROL", "IMAGE", "MASK", "INT", "INT"]) |
| 28 | RETURN_NAMES = tuple(["FLOW_CONTROL", "current_image", "current_mask", "max_iterations", "iteration_count"]) |
| 29 | FUNCTION = "while_loop_open" |
| 30 | CATEGORY = "CyberEveLoop🐰" |
| 31 | |
| 32 | def standardize_input(self, images, masks): |
| 33 | """ |
| 34 | 标准化输入格式 |
| 35 | images: 确保是4D tensor [B,H,W,C] |
| 36 | masks: 确保是3D tensor [B,H,W] |
| 37 | 如果images是单张图片,会扩展到与masks相同的批次大小 |
| 38 | """ |
| 39 | # 处理masks(先处理masks以获取批次大小) |
| 40 | if isinstance(masks, list): |
| 41 | masks = torch.cat(masks, dim=0) |
| 42 | if len(masks.shape) == 2: # [H,W] -> [1,H,W] |
| 43 | masks = masks.unsqueeze(0) |
| 44 | assert len(masks.shape) == 3, f"Masks must be 3D [B,H,W], got shape {masks.shape}" |
| 45 | |
| 46 | # 处理images |
| 47 | if isinstance(images, list): |
| 48 | images = torch.cat(images, dim=0) |
| 49 | if len(images.shape) == 3: # [H,W,C] -> [1,H,W,C] |
| 50 | images = images.unsqueeze(0) |
| 51 | assert len(images.shape) == 4, f"Images must be 4D [B,H,W,C], got shape {images.shape}" |
| 52 | |
| 53 | # 检查是否需要扩展images |
| 54 | if images.shape[0] == 1 and masks.shape[0] > 1: |
| 55 | print(f"Expanding single image to match mask batch size: {masks.shape[0]}") |
| 56 | images = images.expand(masks.shape[0], -1, -1, -1) |
| 57 | |
| 58 | # 确保batch维度相同 |
| 59 | assert images.shape[0] == masks.shape[0], \ |
| 60 | f"Batch size mismatch: images {images.shape[0]} vs masks {masks.shape[0]}" |
| 61 | |
| 62 | return images, masks |
| 63 | |
| 64 | |
| 65 | def resize_to_match(self, image, target_shape): |
nothing calls this directly
no outgoing calls
no test coverage detected