Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from gradio_imageslider import ImageSlider
|
4 |
+
from loadimg import load_img
|
5 |
+
import spaces
|
6 |
+
from transformers import AutoModelForImageSegmentation
|
7 |
+
import torch
|
8 |
+
from torchvision import transforms
|
9 |
+
|
10 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
11 |
+
|
12 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
13 |
+
"briaai/RMBG-2.0", trust_remote_code=True
|
14 |
+
)
|
15 |
+
birefnet.to("cpu")
|
16 |
+
transform_image = transforms.Compose(
|
17 |
+
[
|
18 |
+
transforms.Resize((1024, 1024)),
|
19 |
+
transforms.ToTensor(),
|
20 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
21 |
+
]
|
22 |
+
)
|
23 |
+
|
24 |
+
output_folder = 'output_images'
|
25 |
+
if not os.path.exists(output_folder):
|
26 |
+
os.makedirs(output_folder)
|
27 |
+
|
28 |
+
def fn(image):
|
29 |
+
im = load_img(image, output_type="pil")
|
30 |
+
im = im.convert("RGB")
|
31 |
+
origin = im.copy()
|
32 |
+
image = process(im)
|
33 |
+
image_path = os.path.join(output_folder, "no_bg_image.png")
|
34 |
+
image.save(image_path)
|
35 |
+
return (image, origin), image_path
|
36 |
+
|
37 |
+
@spaces.GPU
|
38 |
+
def process(image):
|
39 |
+
image_size = image.size
|
40 |
+
input_images = transform_image(image).unsqueeze(0).to("cpu")
|
41 |
+
|
42 |
+
with torch.no_grad():
|
43 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
44 |
+
pred = preds[0].squeeze()
|
45 |
+
pred_pil = transforms.ToPILImage()(pred)
|
46 |
+
mask = pred_pil.resize(image_size)
|
47 |
+
image.putalpha(mask)
|
48 |
+
return image
|
49 |
+
|
50 |
+
def process_file(f):
|
51 |
+
name_path = f.rsplit(".",1)[0]+".png"
|
52 |
+
im = load_img(f, output_type="pil")
|
53 |
+
im = im.convert("RGB")
|
54 |
+
transparent = process(im)
|
55 |
+
transparent.save(name_path)
|
56 |
+
return name_path
|
57 |
+
|
58 |
+
slider1 = ImageSlider(label="RMBG-2.0", type="pil")
|
59 |
+
slider2 = ImageSlider(label="RMBG-2.0", type="pil")
|
60 |
+
image = gr.Image(label="Upload an image")
|
61 |
+
image2 = gr.Image(label="Upload an image",type="filepath")
|
62 |
+
text = gr.Textbox(label="Paste an image URL")
|
63 |
+
png_file = gr.File(label="output png file")
|
64 |
+
|
65 |
+
|
66 |
+
tab1 = gr.Interface(
|
67 |
+
fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], api_name="image", description="⚠️ Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding."
|
68 |
+
)
|
69 |
+
|
70 |
+
tab2 = gr.Interface(
|
71 |
+
fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], api_name="text", description="⚠️ Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding."
|
72 |
+
)
|
73 |
+
|
74 |
+
tab3 = gr.Interface(
|
75 |
+
process_file, inputs=image2, outputs=png_file, api_name="png", description="⚠️ Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding."
|
76 |
+
)
|
77 |
+
|
78 |
+
demo = gr.TabbedInterface(
|
79 |
+
[tab1, tab2], ["Using Image", "Usling URL"], title="✂️ RMBG Image Background Remover ✂️", theme="Yntec/HaleyCH_Theme_Orange"
|
80 |
+
)
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch(show_error=True, debug=True)
|