Spaces:
Runtime error
Runtime error
JinHyeong99
commited on
Commit
ยท
5b982d8
1
Parent(s):
5e1d0b4
app.py
CHANGED
@@ -1,35 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
3 |
from PIL import Image
|
|
|
4 |
import torch
|
5 |
|
6 |
# ๋ชจ๋ธ๊ณผ feature extractor ๋ก๋
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
outputs = model(**inputs)
|
16 |
-
logits = outputs.logits
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
return result
|
25 |
|
26 |
-
#
|
|
|
|
|
|
|
27 |
demo = gr.Interface(
|
28 |
-
fn=
|
29 |
-
inputs=gr.inputs.Image(type=
|
30 |
-
outputs=
|
31 |
-
|
|
|
32 |
)
|
33 |
|
34 |
# ์ธํฐํ์ด์ค ์คํ
|
35 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
3 |
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
import torch
|
6 |
|
7 |
# ๋ชจ๋ธ๊ณผ feature extractor ๋ก๋
|
8 |
+
model_name = "nvidia/segformer-b0-finetuned-ade-512-512"
|
9 |
+
model = SegformerForSemanticSegmentation.from_pretrained(model_name)
|
10 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(model_name)
|
11 |
|
12 |
+
def segment_image(image):
|
13 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ
|
14 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model(**inputs)
|
|
|
|
|
17 |
|
18 |
+
# ๋ง์คํฌ ์์ฑ
|
19 |
+
upsampled_logits = torch.nn.functional.interpolate(
|
20 |
+
outputs.logits, size=image.size[::-1], mode="bilinear", align_corners=False
|
21 |
+
)
|
22 |
+
upsampled_predictions = upsampled_logits.argmax(dim=1)
|
23 |
+
mask = upsampled_predictions.squeeze().numpy()
|
24 |
|
25 |
+
# ๊ฒฐ๊ณผ ๋ฐํ
|
26 |
+
return Image.fromarray(np.uint8(mask * 255))
|
|
|
27 |
|
28 |
+
# ์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก
|
29 |
+
example_images = ["image1.jpg", "image2.jpg", "image3.jpg"]
|
30 |
+
|
31 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
32 |
demo = gr.Interface(
|
33 |
+
fn=segment_image,
|
34 |
+
inputs=gr.inputs.Image(type="pil"),
|
35 |
+
outputs="image",
|
36 |
+
title="๋จธ์ ๋ฌ๋ 7์ฃผ์ฐจ ๊ณผ์ _3",
|
37 |
+
examples=example_images
|
38 |
)
|
39 |
|
40 |
# ์ธํฐํ์ด์ค ์คํ
|
41 |
+
demo.launch()
|