Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +69 -0
- elon.webp +0 -0
- requirements.txt +16 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
torch.set_float32_matmul_precision("high")
|
11 |
+
|
12 |
+
model = AutoModelForImageSegmentation.from_pretrained(
|
13 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
14 |
+
).to(device)
|
15 |
+
|
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 remove_background(image):
|
26 |
+
im = load_img(image, output_type="pil").convert("RGB")
|
27 |
+
image_size = im.size
|
28 |
+
|
29 |
+
input_image = transform_image(im).unsqueeze(0).to(device)
|
30 |
+
with torch.no_grad():
|
31 |
+
preds = model(input_image)[-1].sigmoid().cpu()
|
32 |
+
|
33 |
+
mask = transforms.ToPILImage()(preds[0].squeeze()).resize(image_size)
|
34 |
+
im.putalpha(mask)
|
35 |
+
|
36 |
+
return im, im.copy()
|
37 |
+
|
38 |
+
slider1 = ImageSlider(label="Arka Plan Kaldırıldı", type="pil")
|
39 |
+
slider2 = ImageSlider(label="Arka Plan Kaldırıldı", type="pil")
|
40 |
+
image_input = gr.Image(label="Resim Yükle")
|
41 |
+
url_input = gr.Textbox(label="Resim URL'si")
|
42 |
+
|
43 |
+
chameleon = load_img("elon.webp", output_type="pil")
|
44 |
+
url_example = "https://dadanizm.com/wp-content/uploads/2022/11/elon-musk-992x2656.jpeg"
|
45 |
+
|
46 |
+
tab1 = gr.Interface(
|
47 |
+
fn=remove_background,
|
48 |
+
inputs=image_input,
|
49 |
+
outputs=slider1,
|
50 |
+
examples=[chameleon],
|
51 |
+
api_name="image"
|
52 |
+
)
|
53 |
+
|
54 |
+
tab2 = gr.Interface(
|
55 |
+
fn=remove_background,
|
56 |
+
inputs=url_input,
|
57 |
+
outputs=slider2,
|
58 |
+
examples=[url_example],
|
59 |
+
api_name="url"
|
60 |
+
)
|
61 |
+
|
62 |
+
demo = gr.TabbedInterface(
|
63 |
+
[tab1, tab2],
|
64 |
+
["Resim", "URL"],
|
65 |
+
title="Resim Arka Planını Kaldır"
|
66 |
+
)
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
demo.launch(show_error=True)
|
elon.webp
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
accelerate
|
3 |
+
opencv-python
|
4 |
+
spaces
|
5 |
+
pillow
|
6 |
+
numpy
|
7 |
+
timm
|
8 |
+
kornia
|
9 |
+
prettytable
|
10 |
+
typing
|
11 |
+
scikit-image
|
12 |
+
huggingface_hub
|
13 |
+
transformers>=4.39.1
|
14 |
+
gradio
|
15 |
+
gradio_imageslider
|
16 |
+
loadimg>=0.1.1
|