Spaces:
Runtime error
Runtime error
JinHyeong99
commited on
Commit
ยท
a87c3be
1
Parent(s):
91062c2
app.py
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
|
|
| 1 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
image3 = Image.open(image3)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# ๋ชจ๋ธ๊ณผ feature extractor ๋ก๋
|
| 7 |
+
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 8 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 9 |
|
| 10 |
+
# ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ๋ ํจ์
|
| 11 |
+
def predict(image):
|
| 12 |
+
# ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ธ์ ๋ง๊ฒ ๋ณํ
|
| 13 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
|
| 17 |
+
# ๊ฒฐ๊ณผ๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํ (์: ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค ์ ํ)
|
| 18 |
+
result = torch.argmax(logits, dim=1)
|
| 19 |
+
result = result.squeeze().cpu().numpy()
|
| 20 |
|
| 21 |
+
# ์ฌ๊ธฐ์์๋ ๋จ์ํ๋ฅผ ์ํด ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
|
| 22 |
+
# ์ค์ ๋ก๋ ๊ฒฐ๊ณผ๋ฅผ ์ ์ ํ ํ์์ผ๋ก ๋ณํํด์ผ ํ ์ ์์ต๋๋ค.
|
| 23 |
+
return result
|
|
|
|
| 24 |
|
| 25 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=predict,
|
| 28 |
+
inputs=gr.inputs.Image(shape=(400, 600)),
|
| 29 |
+
outputs=gr.outputs.Image(),
|
| 30 |
+
examples=["image1.jpg", "image2.jpg", "image3.jpg"] # ์ธ ๊ฐ์ ์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# ์ธํฐํ์ด์ค ์คํ
|
| 34 |
+
demo.launch()
|