danielgatis / rembg

Rembg is a tool to remove images background
MIT License
16.46k stars 1.84k forks source link

[BUG] ...improper removal of background #617

Closed Striker2k closed 4 months ago

Striker2k commented 6 months ago

Describe the bug 1] When user is using id card, rembg is unable to crop inage

To Reproduce Steps to reproduce the behavior:

  1. Give model image of person with id card
  2. make sure that the background is bright

Expected behavior Image should remove background properly.

Images Input images to reproduce. IMG-20240401-WA0004 IMG-20240401-WA0005

OS Version: Android Version 12

Rembg version: v2.0.54

Striker2k commented 6 months ago

Code :

import rembg import cv2 from PIL import Image

def remove_background(image_path, output_path): input_image = cv2.imread(image_path) input_image_rgb = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB) output_image = rembg. remove(input_image_rgb) output_image_gray = cv2.cvtColor(output_image, cv2.COLORRGB2GRAY" contours, = cv2.findContours(output_image_gray, cv2.RETR_EXTERNAL, cV2.CHAIN_APPROX_SIMPLE)

if len(contours) > 0:
    largest_contour = max(contours, key=cv2.contourArea)
    x, y, w, h = cv2.boundingRect(largest_contour)
    x1, y1, x2, y2 = x, y, x+w, y+h
    cropped_output_image = output_image[y1:y2, x1:x2]
    cropped_output_image_pil = Image.fromarray(cropped_output_image)

    cropped_output_image_pil.save(output_path)
    return output_path
else:
    print("Error: No foreground detected.")
    return None

image_path = "Media.jpeg" output_path = "error.png" remove_background(image_path, output_path)

Error.png / output 20240402_125300.jpg

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 4 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

Striker2k commented 4 months ago

Any update regarding this?