LogicGoInfotechSpaces commited on
Commit
c96c733
·
1 Parent(s): a170026

fix: add debug logs for mask processing; verify output changes to diagnose removal issues

Browse files
Files changed (2) hide show
  1. api/main.py +4 -0
  2. src/core.py +14 -1
api/main.py CHANGED
@@ -193,6 +193,10 @@ def inpaint(req: InpaintRequest, _: None = Depends(bearer_auth)) -> Dict[str, st
193
  img_rgba = _load_rgba_image(file_store[req.image_id]["path"])
194
  mask_img = Image.open(file_store[req.mask_id]["path"]) # may be RGB/gray/RGBA
195
  mask_rgba = _load_rgba_mask_from_image(mask_img)
 
 
 
 
196
 
197
  if req.passthrough:
198
  result = np.array(img_rgba.convert("RGB"))
 
193
  img_rgba = _load_rgba_image(file_store[req.image_id]["path"])
194
  mask_img = Image.open(file_store[req.mask_id]["path"]) # may be RGB/gray/RGBA
195
  mask_rgba = _load_rgba_mask_from_image(mask_img)
196
+
197
+ # Debug: check mask before processing
198
+ white_pixels = int((mask_rgba[:,:,0] > 128).sum())
199
+ log.info(f"Inpaint request: mask has {white_pixels} white pixels, invert_mask={req.invert_mask}")
200
 
201
  if req.passthrough:
202
  result = np.array(img_rgba.convert("RGB"))
src/core.py CHANGED
@@ -486,9 +486,14 @@ def process_inpaint(image, mask, invert_mask=True):
486
  else:
487
  mask = np.maximum(mask, alpha_channel) # opaque → white
488
 
489
- # Apply invert_mask if needed (for special cases where black=remove)
 
 
490
  if not invert_mask:
491
  mask = 255 - mask
 
 
 
492
 
493
  mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
494
 
@@ -504,7 +509,15 @@ def process_inpaint(image, mask, invert_mask=True):
504
  # Final check: ensure we have some pixels to remove
505
  mask_final_pixels = int((mask > 0.5).sum())
506
  print(f"After normalization: {mask_final_pixels} pixels marked for removal (value > 0.5)")
 
 
 
 
507
 
508
  res_np_img = run(image, mask)
 
 
 
 
509
 
510
  return cv2.cvtColor(res_np_img, cv2.COLOR_BGR2RGB)
 
486
  else:
487
  mask = np.maximum(mask, alpha_channel) # opaque → white
488
 
489
+ # Apply invert_mask if needed
490
+ # When invert_mask=False: black pixels (0) should become white (255) to remove
491
+ # When invert_mask=True (default): white pixels (255) stay white to remove (standard)
492
  if not invert_mask:
493
  mask = 255 - mask
494
+ print(f"Applied invert_mask=False: inverted mask - {int((mask > 128).sum())} pixels now marked for removal")
495
+ else:
496
+ print(f"Using invert_mask=True: {int((mask > 128).sum())} white pixels will be removed (standard)")
497
 
498
  mask = resize_max_size(mask, size_limit=size_limit, interpolation=interpolation)
499
 
 
509
  # Final check: ensure we have some pixels to remove
510
  mask_final_pixels = int((mask > 0.5).sum())
511
  print(f"After normalization: {mask_final_pixels} pixels marked for removal (value > 0.5)")
512
+
513
+ if mask_final_pixels < 10:
514
+ print("WARNING: Very few pixels marked for removal! The mask might be empty or inverted.")
515
+ print("Check your mask format: white pixels (255) should indicate areas to remove when invert_mask=True")
516
 
517
  res_np_img = run(image, mask)
518
+
519
+ # Debug: verify output changed
520
+ diff_pixels = int(np.sum(np.abs(res_np_img.astype(np.float32) - cv2.cvtColor(image, cv2.COLOR_RGBA2RGB).astype(np.float32)) > 5))
521
+ print(f"Output check: {diff_pixels} pixels differ from input (should be > 0 if removal worked)")
522
 
523
  return cv2.cvtColor(res_np_img, cv2.COLOR_BGR2RGB)