A newer version of the Gradio SDK is available:
5.20.1
metadata
title: Background Removal Tool
emoji: π
colorFrom: blue
colorTo: blue
sdk: gradio
sdk_version: 5.12.0
app_file: app.py
pinned: false
short_description: A tool to remove image backgrounds with precision
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
Background Removal Tool
This is a deep learning-powered Background Removal Tool that uses image segmentation models to remove backgrounds from images and add transparency (alpha channel). It features a user-friendly interface built with Gradio to interact with the tool via image uploads, URLs, or file outputs.
Features
- Two Segmentation Models:
BiRefNet
: Efficient and robust segmentation model.RMBG-2.0
: Advanced model for refined background removal.
- Multiple Input Methods:
- Upload images directly from your system.
- Provide an image URL for processing.
- Upload and save the processed image as a PNG file with transparency.
- Customizable: Switch between models for different use cases.
- Fast and GPU-Powered: Leverages CUDA for faster processing on GPUs.
Requirements
- Python 3.8+
- A GPU-enabled environment for CUDA support (optional but recommended).
- Installed Python libraries:
gradio
torch
transformers
torchvision
Pillow
numpy
Install dependencies using:
pip install gradio torch torchvision transformers Pillow numpy
Usage
Run the Application
Execute the script using:
python inference.py
Interface
Tab 1: Image Upload
- Upload an image from your local system.
- Select a model (
BiRefNet
orRMBG-2.0
). - View and download the processed image with the background removed.
Tab 2: URL Input
- Paste the URL of an image.
- Select a model (
BiRefNet
orRMBG-2.0
). - View and download the processed image with the background removed.
Tab 3: File Output
- Upload an image file.
- Select a model (
BiRefNet
orRMBG-2.0
). - Get the path to the processed PNG file with transparency.
Example
- Use the provided example image (
ironman.jpg
) to test the tool.
How It Works
- Model Loading:
- Loads pre-trained segmentation models from Hugging Face.
- Image Preprocessing:
- Resizes and normalizes the input image.
- Background Removal:
- The model generates a mask for the image background.
- The mask is applied to create a transparent background.
- Output:
- Processed image is displayed or saved with an alpha channel.
Contributing
Feel free to submit issues or pull requests for improvements or bug fixes.