LogicGoInfotechSpaces commited on
Commit
87c6da8
·
1 Parent(s): a9b6d73

fix(api): remove black paint detection - just auto-detect pink segments, everything else kept automatically

Browse files
Files changed (1) hide show
  1. api/main.py +21 -12
api/main.py CHANGED
@@ -335,7 +335,7 @@ def inpaint_multipart(
335
  log.info("Successfully created black/white mask: %d white pixels (to remove), %d black pixels (to keep)",
336
  nonzero, binmask.shape[0] * binmask.shape[1] - nonzero)
337
  else:
338
- mask_rgba = _load_rgba_mask_from_image(m)
339
 
340
  # When mask_is_painted=true, we create white=remove masks, so invert_mask should be False
341
  # (white pixels should stay white to indicate removal)
@@ -371,8 +371,10 @@ def remove_pink_segments(
371
  _: None = Depends(bearer_auth),
372
  ) -> Dict[str, str]:
373
  """
374
- Simple endpoint: just upload an image with pink/magenta segments you want to remove.
375
- The API automatically detects pink areas and removes them, keeping everything else.
 
 
376
  """
377
  log.info(f"Simple remove-pink: processing image {image.filename}")
378
 
@@ -380,14 +382,18 @@ def remove_pink_segments(
380
  img = Image.open(image.file).convert("RGBA")
381
  img_rgb = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2RGB)
382
 
383
- # Auto-detect pink/magenta segments
 
 
 
 
384
  magenta_detected = (
385
  (img_rgb[:, :, 0] > 240) & # Red: high
386
  (img_rgb[:, :, 1] < 30) & # Green: low
387
  (img_rgb[:, :, 2] > 240) # Blue: high
388
  ).astype(np.uint8) * 255
389
 
390
- # Clean up the mask
391
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
392
  binmask = cv2.morphologyEx(magenta_detected, cv2.MORPH_CLOSE, kernel, iterations=2)
393
  binmask = cv2.morphologyEx(binmask, cv2.MORPH_OPEN, kernel, iterations=1)
@@ -400,7 +406,7 @@ def remove_pink_segments(
400
  magenta_strict = np.all(img_rgb == [255, 0, 255], axis=2).astype(np.uint8) * 255
401
  binmask = cv2.morphologyEx(magenta_strict, cv2.MORPH_CLOSE, kernel, iterations=3)
402
  nonzero = int((binmask > 0).sum())
403
- log.info(f"Strict detection: {nonzero} pixels")
404
 
405
  if nonzero < 50:
406
  log.error("No pink segments detected! Returning original image.")
@@ -413,14 +419,17 @@ def remove_pink_segments(
413
  "error": "No pink/magenta segments detected. Please paint areas to remove with magenta/pink color (RGB 255,0,255)."
414
  }
415
 
416
- # Create mask: white = pink areas (remove), black = everything else (keep)
 
 
417
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
418
- mask_rgba[:, :, 0] = binmask # R
419
- mask_rgba[:, :, 1] = binmask # G
420
- mask_rgba[:, :, 2] = binmask # B
421
- mask_rgba[:, :, 3] = 255 # Alpha
422
 
423
- log.info(f"Created mask: {nonzero} white pixels (remove), {binmask.shape[0]*binmask.shape[1]-nonzero} black pixels (keep)")
 
424
 
425
  # Process with invert_mask=False because white pixels = remove (standard)
426
  result = process_inpaint(np.array(img), mask_rgba, invert_mask=False)
 
335
  log.info("Successfully created black/white mask: %d white pixels (to remove), %d black pixels (to keep)",
336
  nonzero, binmask.shape[0] * binmask.shape[1] - nonzero)
337
  else:
338
+ mask_rgba = _load_rgba_mask_from_image(m)
339
 
340
  # When mask_is_painted=true, we create white=remove masks, so invert_mask should be False
341
  # (white pixels should stay white to indicate removal)
 
371
  _: None = Depends(bearer_auth),
372
  ) -> Dict[str, str]:
373
  """
374
+ Simple endpoint: upload an image with pink/magenta segments to remove.
375
+ - Pink/Magenta segments automatically removed (white in mask)
376
+ - Everything else → automatically kept (black in mask)
377
+ Just paint pink/magenta on areas you want to remove, upload the image, and it works!
378
  """
379
  log.info(f"Simple remove-pink: processing image {image.filename}")
380
 
 
382
  img = Image.open(image.file).convert("RGBA")
383
  img_rgb = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2RGB)
384
 
385
+ # Auto-detect pink/magenta segments to remove
386
+ # Pink/Magenta → white in mask (remove)
387
+ # Everything else (natural image colors, including dark areas) → black in mask (keep)
388
+
389
+ # Detect pink/magenta paint (RGB: R>240, G<30, B>240)
390
  magenta_detected = (
391
  (img_rgb[:, :, 0] > 240) & # Red: high
392
  (img_rgb[:, :, 1] < 30) & # Green: low
393
  (img_rgb[:, :, 2] > 240) # Blue: high
394
  ).astype(np.uint8) * 255
395
 
396
+ # Clean up the pink mask
397
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
398
  binmask = cv2.morphologyEx(magenta_detected, cv2.MORPH_CLOSE, kernel, iterations=2)
399
  binmask = cv2.morphologyEx(binmask, cv2.MORPH_OPEN, kernel, iterations=1)
 
406
  magenta_strict = np.all(img_rgb == [255, 0, 255], axis=2).astype(np.uint8) * 255
407
  binmask = cv2.morphologyEx(magenta_strict, cv2.MORPH_CLOSE, kernel, iterations=3)
408
  nonzero = int((binmask > 0).sum())
409
+ log.info(f"Strict detection: {nonzero} pink pixels")
410
 
411
  if nonzero < 50:
412
  log.error("No pink segments detected! Returning original image.")
 
419
  "error": "No pink/magenta segments detected. Please paint areas to remove with magenta/pink color (RGB 255,0,255)."
420
  }
421
 
422
+ # Create mask:
423
+ # - White = pink areas (remove)
424
+ # - Black = everything else (keep) - automatically, no black painting needed
425
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
426
+ mask_rgba[:, :, 0] = binmask # R: white where pink detected
427
+ mask_rgba[:, :, 1] = binmask # G: white where pink detected
428
+ mask_rgba[:, :, 2] = binmask # B: white where pink detected
429
+ mask_rgba[:, :, 3] = 255 # Alpha: fully opaque
430
 
431
+ total_pixels = binmask.shape[0] * binmask.shape[1]
432
+ log.info(f"Created mask: {nonzero} white pixels (remove/pink), {total_pixels - nonzero} black pixels (keep - everything else automatically)")
433
 
434
  # Process with invert_mask=False because white pixels = remove (standard)
435
  result = process_inpaint(np.array(img), mask_rgba, invert_mask=False)