Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
-
|
6 |
-
# ๋ชจ๋ธ๊ณผ ์ด๋ฏธ์ง ํ๋ก์ธ์ ๋ก๋
|
7 |
-
model_name = "nvidia/mit-b0"
|
8 |
-
image_processor = SegformerImageProcessor.from_pretrained(model_name)
|
9 |
-
model = SegformerForSemanticSegmentation.from_pretrained(model_name)
|
10 |
|
11 |
def remove_background(image):
|
12 |
-
|
13 |
-
|
14 |
-
outputs = model(**inputs)
|
15 |
-
logits = outputs.logits
|
16 |
-
# ๊ฒฐ๊ณผ์์ ๊ฐ์ฅ ๊ฐ๋ฅ์ฑ ๋์ ํด๋์ค ์ ํ
|
17 |
-
upsampled_logits = torch.nn.functional.interpolate(logits, size=image.size[::-1], mode="bilinear", align_corners=False)
|
18 |
-
seg = upsampled_logits.argmax(dim=1)[0]
|
19 |
-
# ๋ฐฐ๊ฒฝ(0)์ด ์๋ ๋ถ๋ถ๋ง ์ถ์ถ
|
20 |
-
mask = seg != 0
|
21 |
-
# ์๋ณธ ์ด๋ฏธ์ง์ ๋ง์คํฌ ์ ์ฉ
|
22 |
-
masked_image = Image.fromarray(image).convert("RGBA")
|
23 |
-
data = masked_image.getdata()
|
24 |
-
newData = []
|
25 |
-
for i, item in enumerate(data):
|
26 |
-
if mask.flatten()[i]:
|
27 |
-
newData.append(item)
|
28 |
-
else:
|
29 |
-
newData.append((255, 255, 255, 0))
|
30 |
-
masked_image.putdata(newData)
|
31 |
-
return masked_image
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# ์ฑ ์คํ
|
37 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from rembg import remove
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def remove_background(image):
|
5 |
+
output_image = remove(image)
|
6 |
+
return output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
iface = gr.Interface(
|
9 |
+
fn=remove_background,
|
10 |
+
inputs=gr.inputs.Image(type="pil"),
|
11 |
+
outputs=gr.outputs.Image(type="pil"),
|
12 |
+
title="๋ฐฐ๊ฒฝ ์ ๊ฑฐ ๋๊ตฌ",
|
13 |
+
description="์ด ๋๊ตฌ๋ ์ด๋ฏธ์ง์์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐํฉ๋๋ค. ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ ์ ์์ต๋๋ค."
|
14 |
+
)
|
15 |
|
|
|
16 |
iface.launch()
|