File size: 2,791 Bytes
76b4917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---

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

1. **Two Segmentation Models**:
   - `BiRefNet`: Efficient and robust segmentation model.
   - `RMBG-2.0`: Advanced model for refined background removal.
2. **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.
3. **Customizable**: Switch between models for different use cases.
4. **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:
```bash

pip install gradio torch torchvision transformers Pillow numpy

```

---

## Usage

### Run the Application
Execute the script using:
```bash

python inference.py

```

### Interface

#### Tab 1: Image Upload
1. Upload an image from your local system.
2. Select a model (`BiRefNet` or `RMBG-2.0`).
3. View and download the processed image with the background removed.

#### Tab 2: URL Input
1. Paste the URL of an image.
2. Select a model (`BiRefNet` or `RMBG-2.0`).
3. View and download the processed image with the background removed.

#### Tab 3: File Output
1. Upload an image file.
2. Select a model (`BiRefNet` or `RMBG-2.0`).
3. 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

1. **Model Loading**:
   - Loads pre-trained segmentation models from Hugging Face.
2. **Image Preprocessing**:
   - Resizes and normalizes the input image.
3. **Background Removal**:
   - The model generates a mask for the image background.
   - The mask is applied to create a transparent background.
4. **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.

---