K00B404 commited on
Commit
e6634bc
·
verified ·
1 Parent(s): 499c21c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.backends.mkldnn.enabled = True # Enable CPU optimizations
11
+
12
+ # Load model on CPU
13
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
14
+ "briaai/RMBG-2.0", trust_remote_code=True
15
+ )
16
+ birefnet.to("cpu")
17
+
18
+ # Reduce image size for efficiency
19
+ transform_image = transforms.Compose(
20
+ [
21
+ transforms.Resize((512, 512)), # Reduced from 1024x1024
22
+ transforms.ToTensor(),
23
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
24
+ ]
25
+ )
26
+
27
+ output_folder = 'output_images'
28
+ os.makedirs(output_folder, exist_ok=True)
29
+
30
+ def fn(image):
31
+ im = load_img(image, output_type="pil").convert("RGB")
32
+ origin = im.copy()
33
+ image = process(im)
34
+ image_path = os.path.join(output_folder, "no_bg_image.png")
35
+ image.save(image_path)
36
+ return (image, origin), image_path
37
+
38
+ def process(image):
39
+ image_size = image.size
40
+ input_images = transform_image(image).unsqueeze(0).to("cpu")
41
+
42
+ with torch.inference_mode(): # Efficient inference mode
43
+ preds = birefnet(input_images)[-1].sigmoid()
44
+
45
+ pred = preds[0].squeeze()
46
+ pred_pil = transforms.ToPILImage()(pred)
47
+ mask = pred_pil.resize(image_size)
48
+ image.putalpha(mask)
49
+ return image
50
+
51
+ def process_file(f):
52
+ name_path = f.rsplit(".", 1)[0] + ".png"
53
+ im = load_img(f, output_type="pil").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
+ chameleon = load_img("giraffe.jpg", output_type="pil")
66
+ url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
67
+
68
+ tab1 = gr.Interface(
69
+ fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], examples=[chameleon], api_name="image"
70
+ )
71
+ tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url], api_name="text")
72
+ tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["giraffe.jpg"], api_name="png")
73
+
74
+ demo = gr.TabbedInterface(
75
+ [tab1, tab2], ["input image", "input url"], title=(
76
+ "RMBG-2.0 for background removal <br>"
77
+ "<span style='font-size:16px; font-weight:300;'>"
78
+ "Background removal model developed by "
79
+ "<a href='https://bria.ai' target='_blank'>BRIA.AI</a>, trained on a carefully selected dataset,<br> "
80
+ "and is available as an open-source model for non-commercial use.</span><br>"
81
+ "<span style='font-size:16px; font-weight:500;'> For testing upload your image and wait.<br>"
82
+ "<a href='https://go.bria.ai/3ZCBTLH' target='_blank'>Commercial use license</a> | "
83
+ "<a href='https://huggingface.co/briaai/RMBG-2.0' target='_blank'>Model card</a> | "
84
+ "<a href='https://blog.bria.ai/brias-new-state-of-the-art-remove-background-2.0-outperforms-the-competition' target='_blank'>Blog</a>"
85
+ "</span>")
86
+ )
87
+
88
+ if __name__ == "__main__":
89
+ demo.launch(show_api=True,show_error=True)
90
+