Upload app.py
Browse files
app.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 |
+
|