saliacoel commited on
Commit
2a5d451
·
verified ·
1 Parent(s): 14a4916

Upload apply_segment_3.py

Browse files
Files changed (1) hide show
  1. apply_segment_3.py +365 -0
apply_segment_3.py ADDED
@@ -0,0 +1,365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hashlib
3
+ from typing import List, Tuple
4
+
5
+ import numpy as np
6
+ import torch
7
+ import torch.nn.functional as F
8
+ from PIL import Image, ImageOps
9
+
10
+
11
+ # ============================================================
12
+ # Standalone assets helpers (no external utils required)
13
+ # Expects: <this_file_dir>/assets/images/*.png
14
+ # ============================================================
15
+
16
+ _ASSETS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "assets", "images")
17
+
18
+
19
+ def list_pngs() -> List[str]:
20
+ if not os.path.isdir(_ASSETS_DIR):
21
+ return []
22
+ files = []
23
+ for f in os.listdir(_ASSETS_DIR):
24
+ if f.lower().endswith(".png") and os.path.isfile(os.path.join(_ASSETS_DIR, f)):
25
+ files.append(f)
26
+ return sorted(files)
27
+
28
+
29
+ def safe_path(filename: str) -> str:
30
+ # Prevent path traversal, force within _ASSETS_DIR
31
+ candidate = os.path.join(_ASSETS_DIR, filename)
32
+ real_assets = os.path.realpath(_ASSETS_DIR)
33
+ real_candidate = os.path.realpath(candidate)
34
+ if not real_candidate.startswith(real_assets + os.sep) and real_candidate != real_assets:
35
+ raise ValueError("Unsafe path (path traversal detected).")
36
+ return real_candidate
37
+
38
+
39
+ def file_hash(filename: str) -> str:
40
+ path = safe_path(filename)
41
+ h = hashlib.sha256()
42
+ with open(path, "rb") as f:
43
+ for chunk in iter(lambda: f.read(1024 * 1024), b""):
44
+ h.update(chunk)
45
+ return h.hexdigest()
46
+
47
+
48
+ def load_image_from_assets(filename: str) -> Tuple[torch.Tensor, torch.Tensor]:
49
+ """
50
+ Loads a PNG from assets/images and returns:
51
+ - image: IMAGE tensor [1,H,W,3] float32 in [0,1]
52
+ - mask: MASK tensor [1,H,W] float32 in [0,1]
53
+
54
+ IMPORTANT: mask follows ComfyUI LoadImage convention:
55
+ if alpha exists: mask = 1 - alpha
56
+ else: mask = 1 - luminance
57
+ """
58
+ path = safe_path(filename)
59
+ i = Image.open(path)
60
+ i = ImageOps.exif_transpose(i)
61
+
62
+ # Match Comfy style handling of mode 'I'
63
+ if i.mode == "I":
64
+ i = i.point(lambda px: px * (1 / 255))
65
+
66
+ # IMAGE output (RGB)
67
+ rgb = i.convert("RGB")
68
+ rgb_np = np.array(rgb).astype(np.float32) / 255.0
69
+ image = torch.from_numpy(rgb_np)[None, ...] # [1,H,W,3]
70
+
71
+ # MASK output
72
+ bands = i.getbands()
73
+ if "A" in bands:
74
+ a = np.array(i.getchannel("A")).astype(np.float32) / 255.0
75
+ alpha = torch.from_numpy(a) # [H,W]
76
+ else:
77
+ # fallback: use luminance as alpha-like signal
78
+ l = np.array(i.convert("L")).astype(np.float32) / 255.0
79
+ alpha = torch.from_numpy(l)
80
+
81
+ mask = 1.0 - alpha # ComfyUI mask convention
82
+ mask = mask.clamp(0.0, 1.0).unsqueeze(0) # [1,H,W]
83
+ return image, mask
84
+
85
+
86
+ # ============================================================
87
+ # Helpers (IMAGE / MASK validation + alpha paste)
88
+ # ============================================================
89
+
90
+ def _as_image(img: torch.Tensor) -> torch.Tensor:
91
+ if not isinstance(img, torch.Tensor):
92
+ raise TypeError("IMAGE must be a torch.Tensor")
93
+ if img.dim() != 4:
94
+ raise ValueError(f"Expected IMAGE shape [B,H,W,C], got {tuple(img.shape)}")
95
+ if img.shape[-1] not in (3, 4):
96
+ raise ValueError(f"Expected IMAGE channels 3 (RGB) or 4 (RGBA), got C={img.shape[-1]}")
97
+ return img
98
+
99
+
100
+ def _as_mask(mask: torch.Tensor) -> torch.Tensor:
101
+ if not isinstance(mask, torch.Tensor):
102
+ raise TypeError("MASK must be a torch.Tensor")
103
+ if mask.dim() == 2:
104
+ mask = mask.unsqueeze(0) # [1,H,W]
105
+ if mask.dim() != 3:
106
+ raise ValueError(f"Expected MASK shape [B,H,W] or [H,W], got {tuple(mask.shape)}")
107
+ return mask
108
+
109
+
110
+ def _ensure_rgba(img: torch.Tensor) -> torch.Tensor:
111
+ img = _as_image(img)
112
+ if img.shape[-1] == 4:
113
+ return img
114
+ B, H, W, _ = img.shape
115
+ alpha = torch.ones((B, H, W, 1), device=img.device, dtype=img.dtype)
116
+ return torch.cat([img, alpha], dim=-1)
117
+
118
+
119
+ def _alpha_over_region(overlay: torch.Tensor, canvas: torch.Tensor, x: int, y: int) -> torch.Tensor:
120
+ """
121
+ Alpha-over paste overlay on top of canvas at (x,y) using overlay alpha.
122
+ Supports RGB/RGBA for both. Returns same channel count as canvas.
123
+ """
124
+ overlay = _as_image(overlay)
125
+ canvas = _as_image(canvas)
126
+
127
+ # Batch handling: allow 1->N expansion
128
+ if overlay.shape[0] != canvas.shape[0]:
129
+ if overlay.shape[0] == 1 and canvas.shape[0] > 1:
130
+ overlay = overlay.expand(canvas.shape[0], *overlay.shape[1:])
131
+ elif canvas.shape[0] == 1 and overlay.shape[0] > 1:
132
+ canvas = canvas.expand(overlay.shape[0], *canvas.shape[1:])
133
+ else:
134
+ raise ValueError(f"Batch mismatch: overlay {overlay.shape[0]} vs canvas {canvas.shape[0]}")
135
+
136
+ B, Hc, Wc, Cc = canvas.shape
137
+ _, Ho, Wo, _ = overlay.shape
138
+
139
+ x = int(x)
140
+ y = int(y)
141
+
142
+ out = canvas.clone()
143
+
144
+ # intersection on canvas
145
+ x0c = max(0, x)
146
+ y0c = max(0, y)
147
+ x1c = min(Wc, x + Wo)
148
+ y1c = min(Hc, y + Ho)
149
+
150
+ if x1c <= x0c or y1c <= y0c:
151
+ return out
152
+
153
+ # corresponding region on overlay
154
+ x0o = x0c - x
155
+ y0o = y0c - y
156
+ x1o = x0o + (x1c - x0c)
157
+ y1o = y0o + (y1c - y0c)
158
+
159
+ canvas_region = out[:, y0c:y1c, x0c:x1c, :]
160
+ overlay_region = overlay[:, y0o:y1o, x0o:x1o, :]
161
+
162
+ canvas_rgba = _ensure_rgba(canvas_region)
163
+ overlay_rgba = _ensure_rgba(overlay_region)
164
+
165
+ over_rgb = overlay_rgba[..., :3].clamp(0.0, 1.0)
166
+ over_a = overlay_rgba[..., 3:4].clamp(0.0, 1.0)
167
+
168
+ under_rgb = canvas_rgba[..., :3].clamp(0.0, 1.0)
169
+ under_a = canvas_rgba[..., 3:4].clamp(0.0, 1.0)
170
+
171
+ # premultiplied alpha composite
172
+ over_pm = over_rgb * over_a
173
+ under_pm = under_rgb * under_a
174
+
175
+ out_a = over_a + under_a * (1.0 - over_a)
176
+ out_pm = over_pm + under_pm * (1.0 - over_a)
177
+
178
+ eps = 1e-6
179
+ out_rgb = torch.where(out_a > eps, out_pm / (out_a + eps), torch.zeros_like(out_pm))
180
+ out_rgb = out_rgb.clamp(0.0, 1.0)
181
+ out_a = out_a.clamp(0.0, 1.0)
182
+
183
+ if Cc == 3:
184
+ out[:, y0c:y1c, x0c:x1c, :] = out_rgb
185
+ else:
186
+ out[:, y0c:y1c, x0c:x1c, :] = torch.cat([out_rgb, out_a], dim=-1)
187
+
188
+ return out
189
+
190
+
191
+ # ============================================================
192
+ # RMBG EXACT MASK COMBINE LOGIC (same as your prior node)
193
+ # torch.maximum + PIL resize (LANCZOS)
194
+ # ============================================================
195
+
196
+ class _AILab_MaskCombiner_Exact:
197
+ def combine_masks(self, mask_1, mode="combine", mask_2=None, mask_3=None, mask_4=None):
198
+ masks = [m for m in [mask_1, mask_2, mask_3, mask_4] if m is not None]
199
+ if len(masks) <= 1:
200
+ return (masks[0] if masks else torch.zeros((1, 64, 64), dtype=torch.float32),)
201
+
202
+ ref_shape = masks[0].shape
203
+ masks = [self._resize_if_needed(m, ref_shape) for m in masks]
204
+
205
+ if mode == "combine":
206
+ result = torch.maximum(masks[0], masks[1])
207
+ for mask in masks[2:]:
208
+ result = torch.maximum(result, mask)
209
+ elif mode == "intersection":
210
+ result = torch.minimum(masks[0], masks[1])
211
+ else:
212
+ result = torch.abs(masks[0] - masks[1])
213
+
214
+ return (torch.clamp(result, 0, 1),)
215
+
216
+ def _resize_if_needed(self, mask, target_shape):
217
+ if mask.shape == target_shape:
218
+ return mask
219
+
220
+ if len(mask.shape) == 2:
221
+ mask = mask.unsqueeze(0)
222
+ elif len(mask.shape) == 4:
223
+ mask = mask.squeeze(1)
224
+
225
+ target_height = target_shape[-2] if len(target_shape) >= 2 else target_shape[0]
226
+ target_width = target_shape[-1] if len(target_shape) >= 2 else target_shape[1]
227
+
228
+ resized_masks = []
229
+ for i in range(mask.shape[0]):
230
+ mask_np = mask[i].cpu().numpy()
231
+ img = Image.fromarray((mask_np * 255).astype(np.uint8))
232
+ img_resized = img.resize((target_width, target_height), Image.LANCZOS)
233
+ mask_resized = np.array(img_resized).astype(np.float32) / 255.0
234
+ resized_masks.append(torch.from_numpy(mask_resized))
235
+
236
+ return torch.stack(resized_masks)
237
+
238
+
239
+ # ============================================================
240
+ # ComfyUI core "Join Image with Alpha" logic (EXACT)
241
+ # (from JoinImageWithAlpha implementation)
242
+ # ============================================================
243
+
244
+ def _resize_mask_comfy(alpha_mask: torch.Tensor, image_shape_hwc: Tuple[int, int, int]) -> torch.Tensor:
245
+ # image_shape_hwc is image.shape[1:] => (H,W,C)
246
+ H = int(image_shape_hwc[0])
247
+ W = int(image_shape_hwc[1])
248
+ return F.interpolate(
249
+ alpha_mask.reshape((-1, 1, alpha_mask.shape[-2], alpha_mask.shape[-1])),
250
+ size=(H, W),
251
+ mode="bilinear",
252
+ ).squeeze(1)
253
+
254
+
255
+ def _join_image_with_alpha_comfy(image: torch.Tensor, alpha: torch.Tensor) -> torch.Tensor:
256
+ """
257
+ EXACT logic:
258
+ batch_size = min(len(image), len(alpha))
259
+ alpha = 1.0 - resize_mask(alpha, image.shape[1:])
260
+ out = cat(image[i][:,:,:3], alpha[i].unsqueeze(2))
261
+ """
262
+ image = _as_image(image)
263
+ alpha = _as_mask(alpha)
264
+
265
+ # Ensure same device/dtype for cat (core node assumes they already match)
266
+ alpha = alpha.to(device=image.device, dtype=image.dtype)
267
+
268
+ batch_size = min(len(image), len(alpha))
269
+ out_images = []
270
+
271
+ alpha_resized = 1.0 - _resize_mask_comfy(alpha, image.shape[1:])
272
+
273
+ for i in range(batch_size):
274
+ out_images.append(torch.cat((image[i][:, :, :3], alpha_resized[i].unsqueeze(2)), dim=2))
275
+
276
+ return torch.stack(out_images)
277
+
278
+
279
+ # ============================================================
280
+ # NODE: apply_segment_3
281
+ # ============================================================
282
+
283
+ class apply_segment_3:
284
+ CATEGORY = "image/salia"
285
+
286
+ @classmethod
287
+ def INPUT_TYPES(cls):
288
+ choices = list_pngs() or ["<no pngs found>"]
289
+ return {
290
+ "required": {
291
+ "mask": ("MASK",),
292
+ "image": (choices, {}), # dropdown asset (used for loaded mask)
293
+ "img": ("IMAGE",), # input image for Join Image with Alpha
294
+ "canvas": ("IMAGE",), # destination canvas
295
+ "x": ("INT", {"default": 0, "min": -100000, "max": 100000, "step": 1}),
296
+ "y": ("INT", {"default": 0, "min": -100000, "max": 100000, "step": 1}),
297
+ }
298
+ }
299
+
300
+ RETURN_TYPES = ("MASK", "MASK", "IMAGE", "IMAGE")
301
+ RETURN_NAMES = ("Inversed_Mask", "Alpha_Mask", "Alpha_Image", "Final_Image")
302
+ FUNCTION = "run"
303
+
304
+ def run(self, mask, image, img, canvas, x, y):
305
+ if image == "<no pngs found>":
306
+ raise FileNotFoundError("No PNGs found in assets/images next to apply_segment_3.py")
307
+
308
+ # --- Step A: invert input mask (exactly your workflow)
309
+ mask_in = _as_mask(mask)
310
+ inversed_mask = 1.0 - mask_in # [B,H,W]
311
+
312
+ # --- Step B: combine_masks_with_loaded(inversed_mask) -> alpha_mask
313
+ # combine_masks_with_loaded does: max(mask, 1 - loaded_mask)
314
+ # loaded_mask comes from loader (Comfy LoadImage-style mask = 1 - alpha)
315
+ # so (1 - loaded_mask) is alpha channel (or "mask" stored as alpha)
316
+ _asset_img, loaded_mask = load_image_from_assets(image)
317
+
318
+ combiner = _AILab_MaskCombiner_Exact()
319
+
320
+ inv_cpu = inversed_mask.detach().cpu()
321
+ loaded_cpu = _as_mask(loaded_mask).detach().cpu()
322
+
323
+ alpha_mask, = combiner.combine_masks(inv_cpu, mode="combine", mask_2=(1.0 - loaded_cpu))
324
+ alpha_mask = torch.clamp(alpha_mask, 0.0, 1.0) # [B,H,W] on CPU
325
+
326
+ # --- Step C: Join Image with Alpha (EXACT comfy core logic)
327
+ alpha_image = _join_image_with_alpha_comfy(img, alpha_mask)
328
+
329
+ # --- Step D: Paste_rect_to_img equivalent (alpha-over)
330
+ canvas = _as_image(canvas)
331
+ alpha_image = alpha_image.to(device=canvas.device, dtype=canvas.dtype)
332
+ final = _alpha_over_region(alpha_image, canvas, x, y)
333
+
334
+ return (inversed_mask, alpha_mask, alpha_image, final)
335
+
336
+ @classmethod
337
+ def IS_CHANGED(cls, mask, image, img, canvas, x, y):
338
+ if image == "<no pngs found>":
339
+ return image
340
+ return file_hash(image)
341
+
342
+ @classmethod
343
+ def VALIDATE_INPUTS(cls, mask, image, img, canvas, x, y):
344
+ if image == "<no pngs found>":
345
+ return "No PNGs found in assets/images next to apply_segment_3.py"
346
+ try:
347
+ path = safe_path(image)
348
+ except Exception as e:
349
+ return str(e)
350
+ if not os.path.isfile(path):
351
+ return f"File not found in assets/images: {image}"
352
+ return True
353
+
354
+
355
+ # ============================================================
356
+ # Node mappings (ONLY this node)
357
+ # ============================================================
358
+
359
+ NODE_CLASS_MAPPINGS = {
360
+ "apply_segment_3": apply_segment_3,
361
+ }
362
+
363
+ NODE_DISPLAY_NAME_MAPPINGS = {
364
+ "apply_segment_3": "apply_segment_3",
365
+ }