Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
from transformers import AutoModelForImageSegmentation, AutoFeatureExtractor
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# ๋ชจ๋ธ๊ณผ ํน์ง ์ถ์ถ๊ธฐ ๋ก๋
|
8 |
+
model_name = "your_model_name_here" # ๋ฐฐ๊ฒฝ ์ ๊ฑฐ์ ์ ํฉํ ๋ชจ๋ธ ์ด๋ฆ์ผ๋ก ๋์ฒดํ์ธ์.
|
9 |
+
model = AutoModelForImageSegmentation.from_pretrained(model_name)
|
10 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
11 |
+
|
12 |
+
def remove_background(image):
|
13 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
14 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
15 |
+
|
16 |
+
# ๋ชจ๋ธ ์์ธก
|
17 |
+
outputs = model(**inputs)
|
18 |
+
# ์ฌ๊ธฐ์๋ ์์๋ก, ์ค์ ๋ชจ๋ธ์ ์ถ๋ ฅ์ ์ฒ๋ฆฌํ์ฌ ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ ๋ก์ง์ด ํ์ํฉ๋๋ค.
|
19 |
+
# ์ด ๋ถ๋ถ์ ์ ํํ ๋ชจ๋ธ๊ณผ ๊ทธ ์ถ๋ ฅ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋๋ค.
|
20 |
+
|
21 |
+
# PIL ์ด๋ฏธ์ง๋ก ๋ณํ (์์)
|
22 |
+
mask = outputs['mask'][0] # ๋ชจ๋ธ ์ถ๋ ฅ์์ ๋ง์คํฌ ์ถ์ถ
|
23 |
+
mask = mask.mul(255).byte().cpu().numpy()
|
24 |
+
image_pil = Image.fromarray(mask).convert("RGB")
|
25 |
+
|
26 |
+
return image_pil
|
27 |
+
|
28 |
+
# Gradio ์ธํฐํ์ด์ค
|
29 |
+
iface = gr.Interface(fn=remove_background, inputs=gr.inputs.Image(type="pil"), outputs="image", title="๋ฐฐ๊ฒฝ ์ ๊ฑฐ")
|
30 |
+
|
31 |
+
# ์ธํฐํ์ด์ค ์คํ
|
32 |
+
iface.launch()
|