shemayons commited on
Commit
76b4917
·
verified ·
1 Parent(s): 269c413

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +104 -13
  2. inference.py +173 -0
  3. ironman.jpg +0 -0
  4. load_image.py +35 -0
README.md CHANGED
@@ -1,13 +1,104 @@
1
- ---
2
- title: Background Removal Tool
3
- emoji: 👀
4
- colorFrom: blue
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 5.12.0
8
- app_file: app.py
9
- pinned: false
10
- short_description: A tool to remove image backgrounds with precision
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Background Removal Tool
3
+ emoji: 👀
4
+ colorFrom: blue
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 5.12.0
8
+ app_file: app.py
9
+ pinned: false
10
+ short_description: A tool to remove image backgrounds with precision
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ # Background Removal Tool
16
+
17
+ 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.
18
+
19
+ ---
20
+
21
+ ## Features
22
+
23
+ 1. **Two Segmentation Models**:
24
+ - `BiRefNet`: Efficient and robust segmentation model.
25
+ - `RMBG-2.0`: Advanced model for refined background removal.
26
+ 2. **Multiple Input Methods**:
27
+ - Upload images directly from your system.
28
+ - Provide an image URL for processing.
29
+ - Upload and save the processed image as a PNG file with transparency.
30
+ 3. **Customizable**: Switch between models for different use cases.
31
+ 4. **Fast and GPU-Powered**: Leverages CUDA for faster processing on GPUs.
32
+
33
+ ---
34
+
35
+ ## Requirements
36
+
37
+ - Python 3.8+
38
+ - A GPU-enabled environment for CUDA support (optional but recommended).
39
+ - Installed Python libraries:
40
+ - `gradio`
41
+ - `torch`
42
+ - `transformers`
43
+ - `torchvision`
44
+ - `Pillow`
45
+ - `numpy`
46
+
47
+ Install dependencies using:
48
+ ```bash
49
+ pip install gradio torch torchvision transformers Pillow numpy
50
+ ```
51
+
52
+ ---
53
+
54
+ ## Usage
55
+
56
+ ### Run the Application
57
+ Execute the script using:
58
+ ```bash
59
+ python inference.py
60
+ ```
61
+
62
+ ### Interface
63
+
64
+ #### Tab 1: Image Upload
65
+ 1. Upload an image from your local system.
66
+ 2. Select a model (`BiRefNet` or `RMBG-2.0`).
67
+ 3. View and download the processed image with the background removed.
68
+
69
+ #### Tab 2: URL Input
70
+ 1. Paste the URL of an image.
71
+ 2. Select a model (`BiRefNet` or `RMBG-2.0`).
72
+ 3. View and download the processed image with the background removed.
73
+
74
+ #### Tab 3: File Output
75
+ 1. Upload an image file.
76
+ 2. Select a model (`BiRefNet` or `RMBG-2.0`).
77
+ 3. Get the path to the processed PNG file with transparency.
78
+
79
+ ### Example
80
+ - Use the provided example image (`ironman.jpg`) to test the tool.
81
+
82
+
83
+ ---
84
+
85
+ ## How It Works
86
+
87
+ 1. **Model Loading**:
88
+ - Loads pre-trained segmentation models from Hugging Face.
89
+ 2. **Image Preprocessing**:
90
+ - Resizes and normalizes the input image.
91
+ 3. **Background Removal**:
92
+ - The model generates a mask for the image background.
93
+ - The mask is applied to create a transparent background.
94
+ 4. **Output**:
95
+ - Processed image is displayed or saved with an alpha channel.
96
+
97
+ ---
98
+
99
+
100
+ ## Contributing
101
+ Feel free to submit issues or pull requests for improvements or bug fixes.
102
+
103
+ ---
104
+
inference.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ from load_image import load_img
4
+ import spaces
5
+ from transformers import AutoModelForImageSegmentation
6
+ import torch
7
+ from torchvision import transforms
8
+ from PIL import Image
9
+ import os
10
+ import numpy as np
11
+
12
+ torch.set_float32_matmul_precision(["high", "highest"][0])
13
+
14
+ # load 2 models
15
+
16
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
17
+ "ZhengPeng7/BiRefNet", trust_remote_code=True
18
+ ).to("cuda")
19
+
20
+
21
+ RMBG2 = AutoModelForImageSegmentation.from_pretrained(
22
+ "briaai/RMBG-2.0", trust_remote_code=True
23
+ ).to("cuda")
24
+
25
+ # Keep them in a dict to switch easily
26
+ models_dict = {
27
+ "BiRefNet": birefnet,
28
+ "RMBG-2.0": RMBG2,
29
+ }
30
+
31
+ # Transform
32
+
33
+ transform_image = transforms.Compose(
34
+ [
35
+ transforms.Resize((1024, 1024)),
36
+ transforms.ToTensor(),
37
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
38
+ ]
39
+ )
40
+
41
+ @spaces.GPU
42
+ def process(image: Image.Image, model_choice: str):
43
+ """
44
+ Runs inference to remove the background (adds alpha)
45
+ with the chosen segmentation model.
46
+ """
47
+ # Select the model
48
+ current_model = models_dict[model_choice]
49
+
50
+ # Prepare image
51
+ image_size = image.size
52
+ input_images = transform_image(image).unsqueeze(0).to("cuda")
53
+
54
+ # Inference
55
+ with torch.no_grad():
56
+ # Each model returns a list of preds in its forward,
57
+ # so we take the last element, apply sigmoid, and move to CPU
58
+ preds = current_model(input_images)[-1].sigmoid().cpu()
59
+
60
+ # Convert single-channel pred to a PIL mask
61
+ pred = preds[0].squeeze()
62
+ pred_pil = transforms.ToPILImage()(pred)
63
+
64
+ # Resize the mask back to original image size
65
+ mask = pred_pil.resize(image_size)
66
+
67
+ # Add alpha channel to the original
68
+ image.putalpha(mask)
69
+ return image
70
+
71
+ def fn(source: str, model_choice: str):
72
+ """
73
+ Used by Tab 1 & Tab 2 to produce a processed image with alpha.
74
+ - 'source' is either a file path (type="filepath") or
75
+ a URL string (textbox).
76
+ - 'model_choice' is the user's selection from the radio.
77
+ """
78
+ # Load from local path or URL
79
+ im = load_img(source, output_type="pil")
80
+ im = im.convert("RGB")
81
+
82
+ # Process
83
+ processed_image = process(im, model_choice)
84
+ return processed_image
85
+
86
+ def process_file(file_path: str, model_choice: str):
87
+ """
88
+ For Tab 3 (file output).
89
+ - Accepts a local path, returns path to a new .png with alpha channel.
90
+ - 'model_choice' is also passed in for selecting the model.
91
+ """
92
+ name_path = file_path.rsplit(".", 1)[0] + ".png"
93
+ im = load_img(file_path, output_type="pil")
94
+ im = im.convert("RGB")
95
+
96
+ # Run the chosen model
97
+ transparent = process(im, model_choice)
98
+ transparent.save(name_path)
99
+ return name_path
100
+
101
+
102
+ # GRadio UI
103
+
104
+ model_selector_1 = gr.Radio(
105
+ choices=["BiRefNet", "RMBG-2.0"],
106
+ value="BiRefNet",
107
+ label="Select Model"
108
+ )
109
+ model_selector_2 = gr.Radio(
110
+ choices=["BiRefNet", "RMBG-2.0"],
111
+ value="BiRefNet",
112
+ label="Select Model"
113
+ )
114
+ model_selector_3 = gr.Radio(
115
+ choices=["BiRefNet", "RMBG-2.0"],
116
+ value="BiRefNet",
117
+ label="Select Model"
118
+ )
119
+
120
+ # Outputs for tabs 1 & 2: single processed image
121
+ processed_img_upload = gr.Image(label="Processed Image (Upload)", type="pil")
122
+ processed_img_url = gr.Image(label="Processed Image (URL)", type="pil")
123
+
124
+ # For uploading local files
125
+ image_upload = gr.Image(label="Upload an image", type="filepath")
126
+ image_file_upload = gr.Image(label="Upload an image", type="filepath")
127
+
128
+ # For Tab 2 (URL input)
129
+ url_input = gr.Textbox(label="Paste an image URL")
130
+
131
+ # For Tab 3 (file output)
132
+ output_file = gr.File(label="Output PNG File")
133
+
134
+ # Tab 1: local image -> processed image
135
+ tab1 = gr.Interface(
136
+ fn=fn,
137
+ inputs=[image_upload, model_selector_1],
138
+ outputs=processed_img_upload,
139
+ examples=[["ironman.jpg", "BiRefNet/RMBG"]],
140
+ api_name="image",
141
+ description="Upload an image and choose your background removal model."
142
+ )
143
+
144
+ # Tab 2: URL input -> processed image
145
+ tab2 = gr.Interface(
146
+ fn=fn,
147
+ inputs=[url_input, model_selector_2],
148
+ outputs=processed_img_url,
149
+ api_name="text",
150
+ description="Paste an image URL and choose your background removal model."
151
+ )
152
+
153
+ # Tab 3: file output -> returns path to .png
154
+ tab3 = gr.Interface(
155
+ fn=process_file,
156
+ inputs=[image_file_upload, model_selector_3],
157
+ outputs=output_file,
158
+ examples=[["ironman.jpg", "BiRefNet/RMBG"]],
159
+ api_name="png",
160
+ description="Upload an image, choose a model, and get a transparent PNG."
161
+ )
162
+
163
+ # Combine all tabs
164
+ demo = gr.TabbedInterface(
165
+ [tab1, tab2, tab3],
166
+ ["Image Upload", "URL Input", "File Output"],
167
+ title="Background Removal Tool"
168
+ )
169
+
170
+ if __name__ == "__main__":
171
+ demo.launch(show_error=True, share=True)
172
+
173
+
ironman.jpg ADDED
load_image.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import requests
4
+ from io import BytesIO
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ def load_img(source, output_type="pil"):
9
+ """
10
+ Load an image from a local file path or a URL.
11
+
12
+ Parameters:
13
+ - source (str): A file path or a URL.
14
+ - output_type (str): The output format: "pil" (PIL Image) or "numpy" (NumPy array).
15
+
16
+ Returns:
17
+ - PIL.Image.Image or numpy.ndarray depending on output_type.
18
+ """
19
+
20
+ # Determine if `source` is a local file path or a URL
21
+ if os.path.exists(source):
22
+ # Local file
23
+ img = Image.open(source)
24
+ else:
25
+ # Assume source is a URL
26
+ response = requests.get(source)
27
+ response.raise_for_status()
28
+ img = Image.open(BytesIO(response.content))
29
+
30
+ if output_type == "pil":
31
+ return img
32
+ elif output_type == "numpy":
33
+ return np.array(img)
34
+ else:
35
+ raise ValueError(f"Unknown output_type: {output_type}")