Spaces:
Runtime error
Runtime error
NegiTurkey
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_imageslider import ImageSlider
|
3 |
+
from loadimg import load_img
|
4 |
+
import spaces
|
5 |
+
from transformers import AutoModelForImageSegmentation
|
6 |
+
import torch
|
7 |
+
from torchvision import transforms
|
8 |
+
import zipfile
|
9 |
+
|
10 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
11 |
+
|
12 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
13 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
14 |
+
)
|
15 |
+
birefnet.to("cuda")
|
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 |
+
@spaces.GPU
|
25 |
+
def fn(image):
|
26 |
+
im = load_img(image, output_type="pil")
|
27 |
+
im = im.convert("RGB")
|
28 |
+
image_size = im.size
|
29 |
+
origin = im.copy()
|
30 |
+
input_images = transform_image(im).unsqueeze(0).to("cuda")
|
31 |
+
|
32 |
+
with torch.no_grad():
|
33 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
34 |
+
pred = preds[0].squeeze()
|
35 |
+
pred_pil = transforms.ToPILImage()(pred)
|
36 |
+
mask = pred_pil.resize(image_size)
|
37 |
+
im.putalpha(mask)
|
38 |
+
|
39 |
+
output_file_path = os.path.join("output_images", "output_image_single.png")
|
40 |
+
im.save(output_file_path)
|
41 |
+
|
42 |
+
return (im, origin)
|
43 |
+
|
44 |
+
@spaces.GPU
|
45 |
+
def fn_url(url):
|
46 |
+
im = load_img(url, output_type="pil")
|
47 |
+
im = im.convert("RGB")
|
48 |
+
origin = im.copy()
|
49 |
+
image_size = im.size
|
50 |
+
input_images = transform_image(im).unsqueeze(0).to("cuda")
|
51 |
+
|
52 |
+
with torch.no_grad():
|
53 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
54 |
+
pred = preds[0].squeeze()
|
55 |
+
pred_pil = transforms.ToPILImage()(pred)
|
56 |
+
mask = pred_pil.resize(image_size)
|
57 |
+
im.putalpha(mask)
|
58 |
+
|
59 |
+
output_file_path = os.path.join("output_images", "output_image_url.png")
|
60 |
+
im.save(output_file_path)
|
61 |
+
|
62 |
+
return [im, origin]
|
63 |
+
|
64 |
+
@spaces.GPU
|
65 |
+
def batch_fn(images):
|
66 |
+
output_paths = []
|
67 |
+
for idx, image_path in enumerate(images):
|
68 |
+
im = load_img(image_path, output_type="pil")
|
69 |
+
im = im.convert("RGB")
|
70 |
+
image_size = im.size
|
71 |
+
input_images = transform_image(im).unsqueeze(0).to("cuda")
|
72 |
+
|
73 |
+
with torch.no_grad():
|
74 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
75 |
+
pred = preds[0].squeeze()
|
76 |
+
pred_pil = transforms.ToPILImage()(pred)
|
77 |
+
mask = pred_pil.resize(image_size)
|
78 |
+
im.putalpha(mask)
|
79 |
+
|
80 |
+
output_file_path = os.path.join("output_images", f"output_image_batch_{idx + 1}.png")
|
81 |
+
im.save(output_file_path)
|
82 |
+
output_paths.append(output_file_path)
|
83 |
+
|
84 |
+
zip_file_path = os.path.join("output_images", "processed_images.zip")
|
85 |
+
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
86 |
+
for file in output_paths:
|
87 |
+
zipf.write(file, os.path.basename(file))
|
88 |
+
|
89 |
+
return zip_file_path
|
90 |
+
|
91 |
+
batch_image = gr.File(label="Upload multiple images", type="filepath", file_count="multiple") # 複数画像のアップロードを許可
|
92 |
+
|
93 |
+
slider1 = ImageSlider(label="Processed Image", type="pil")
|
94 |
+
slider2 = ImageSlider(label="Processed Image from URL", type="pil")
|
95 |
+
image = gr.Image(label="Upload an image")
|
96 |
+
text = gr.Textbox(label="Paste an image URL")
|
97 |
+
|
98 |
+
chameleon = load_img("chameleon.jpg", output_type="pil")
|
99 |
+
url = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
100 |
+
|
101 |
+
tab1 = gr.Interface(
|
102 |
+
fn, inputs=image, outputs=slider1, examples=[chameleon], api_name="image"
|
103 |
+
)
|
104 |
+
|
105 |
+
tab2 = gr.Interface(fn_url, inputs=text, outputs=slider2, examples=[url], api_name="text")
|
106 |
+
|
107 |
+
tab3 = gr.Interface(
|
108 |
+
batch_fn,
|
109 |
+
inputs=batch_image,
|
110 |
+
outputs=gr.File(label="Download Processed Files"),
|
111 |
+
api_name="batch",
|
112 |
+
css="""
|
113 |
+
#component-37 {
|
114 |
+
display: none;
|
115 |
+
}
|
116 |
+
"""
|
117 |
+
)
|
118 |
+
|
119 |
+
demo = gr.TabbedInterface(
|
120 |
+
[tab1, tab2, tab3], ["image", "text", "batch"], title="Multi Birefnet for Background Removal"
|
121 |
+
)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
demo.launch()
|