LogicGoInfotechSpaces commited on
Commit
2b0f7aa
·
1 Parent(s): 1ab24b1

fix: handle RGB masks correctly - use RGB channels when alpha is all 255, so white pixels become white in mask

Browse files
Files changed (1) hide show
  1. src/core.py +29 -12
src/core.py CHANGED
@@ -444,36 +444,53 @@ def get_args_parser():
444
 
445
  def process_inpaint(image, mask, invert_mask=True):
446
  """
447
- Process inpainting - EXACT copy from reference model.
448
  Reference: https://huggingface.co/spaces/aryadytm/remove-photo-object
449
- Line 444-466 in their src/core.py
450
  """
451
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
452
  original_shape = image.shape
453
  interpolation = cv2.INTER_CUBIC
454
 
455
- #size_limit: Union[int, str] = request.form.get("sizeLimit", "1080")
456
- #if size_limit == "Original":
457
  size_limit = max(image.shape)
458
- #else:
459
- # size_limit = int(size_limit)
460
 
461
  print(f"Origin image shape: {original_shape}")
462
  image = resize_max_size(image, size_limit=size_limit, interpolation=interpolation)
463
  print(f"Resized image shape: {image.shape}")
464
  image = norm_img(image)
465
 
466
- # Reference model's EXACT logic: simple alpha inversion
467
- # Line 460: mask = 255-mask[:,:,3]
468
- # This means: alpha=0 (transparent/drawn) → 255 (white/remove)
469
- # alpha=255 (opaque) → 0 (black/keep)
470
- mask = 255 - mask[:,:,3]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471
 
472
  # Apply invert_mask if user wants opposite behavior
473
  if not invert_mask:
474
- mask = 255 - mask # double invert back
 
475
 
476
  mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
 
 
 
 
 
 
477
  mask = norm_img(mask)
478
 
479
  res_np_img = run(image, mask)
 
444
 
445
  def process_inpaint(image, mask, invert_mask=True):
446
  """
447
+ Process inpainting - handles both alpha-based masks and RGB-based masks.
448
  Reference: https://huggingface.co/spaces/aryadytm/remove-photo-object
 
449
  """
450
  image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
451
  original_shape = image.shape
452
  interpolation = cv2.INTER_CUBIC
453
 
 
 
454
  size_limit = max(image.shape)
 
 
455
 
456
  print(f"Origin image shape: {original_shape}")
457
  image = resize_max_size(image, size_limit=size_limit, interpolation=interpolation)
458
  print(f"Resized image shape: {image.shape}")
459
  image = norm_img(image)
460
 
461
+ # Handle mask: check if we should use alpha channel or RGB channels
462
+ alpha_channel = mask[:,:,3]
463
+ rgb_channels = mask[:,:,:3]
464
+
465
+ # Check if alpha is meaningful (not all 255)
466
+ alpha_mean = alpha_channel.mean()
467
+
468
+ if alpha_mean < 240:
469
+ # Alpha channel is meaningful (has transparent areas)
470
+ # Reference model logic: mask = 255-mask[:,:,3]
471
+ # alpha=0 (transparent) → 255 (white/remove)
472
+ # alpha=255 (opaque) → 0 (black/keep)
473
+ mask = 255 - alpha_channel
474
+ print(f"Using alpha channel: {int((alpha_channel < 128).sum())} transparent pixels → white")
475
+ else:
476
+ # Alpha is mostly opaque (255), use RGB channels instead
477
+ # RGB masks: white (255) = remove, black (0) = keep
478
+ gray = cv2.cvtColor(rgb_channels, cv2.COLOR_RGB2GRAY)
479
+ mask = (gray > 128).astype(np.uint8) * 255
480
+ print(f"Using RGB channels: {int((mask > 128).sum())} white pixels (to remove)")
481
 
482
  # Apply invert_mask if user wants opposite behavior
483
  if not invert_mask:
484
+ mask = 255 - mask # invert: white becomes black, black becomes white
485
+ print(f"Applied invert_mask=False: inverted mask")
486
 
487
  mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
488
+
489
+ # Debug: log final mask statistics
490
+ mask_nonzero = int((mask > 128).sum())
491
+ mask_total = mask.shape[0] * mask.shape[1]
492
+ print(f"Final mask before normalization: {mask_nonzero}/{mask_total} pixels marked for removal ({100*mask_nonzero/mask_total:.2f}%)")
493
+
494
  mask = norm_img(mask)
495
 
496
  res_np_img = run(image, mask)