Spaces:
Runtime error
Runtime error
Commit
ยท
0f6c099
1
Parent(s):
e3aa933
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,80 @@
|
|
1 |
-
|
2 |
from PIL import Image
|
3 |
-
import
|
4 |
-
|
|
|
5 |
|
6 |
-
# Hugging Face ๋ชจ๋ธ
|
7 |
model_name = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
image = Image.open(BytesIO(requests.get(image_url).content))
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
outputs = model(**inputs)
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from transformers import AutoFeatureExtractor, TFAutoModelForSemanticSegmentation
|
6 |
|
7 |
+
# Hugging Face ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์
|
8 |
model_name = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"
|
9 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
10 |
+
model = TFAutoModelForSemanticSegmentation.from_pretrained(model_name)
|
11 |
|
12 |
+
def label_to_color_image(label, colormap):
|
13 |
+
color_seg = np.zeros(
|
14 |
+
(label.shape[0], label.shape[1], 3), dtype=np.uint8
|
15 |
+
) # height, width, 3
|
16 |
+
for i in range(len(colormap)):
|
17 |
+
color_seg[label.numpy() == i, :] = colormap[i]
|
18 |
+
return color_seg
|
19 |
|
20 |
+
def draw_plot(pred_img, seg, colormap, labels_list):
|
21 |
+
# your existing draw_plot function, unchanged
|
|
|
22 |
|
23 |
+
def huggingface_model(input_img):
|
24 |
+
input_img = Image.fromarray(input_img)
|
25 |
|
26 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
27 |
+
outputs = model(**inputs)
|
28 |
+
logits = outputs.logits
|
29 |
|
30 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
31 |
+
logits = tf.image.resize(
|
32 |
+
logits, input_img.size[::-1]
|
33 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
34 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
35 |
+
|
36 |
+
# Define the colormap for the cityscapes dataset
|
37 |
+
colormap = [
|
38 |
+
[128, 64, 128],
|
39 |
+
[244, 35, 232],
|
40 |
+
[70, 70, 70],
|
41 |
+
[102, 102, 156],
|
42 |
+
[190, 153, 153],
|
43 |
+
[153, 153, 153],
|
44 |
+
[250, 170, 30],
|
45 |
+
[220, 220, 0],
|
46 |
+
[107, 142, 35],
|
47 |
+
[152, 251, 152],
|
48 |
+
[0, 130, 180],
|
49 |
+
[220, 20, 60],
|
50 |
+
[255, 0, 0],
|
51 |
+
[0, 0, 142],
|
52 |
+
[0, 0, 70],
|
53 |
+
[0, 60, 100],
|
54 |
+
[0, 80, 100],
|
55 |
+
[0, 0, 230],
|
56 |
+
[119, 11, 32],
|
57 |
+
]
|
58 |
+
|
59 |
+
color_seg = label_to_color_image(seg, colormap)
|
60 |
+
|
61 |
+
# Show image + mask
|
62 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
63 |
+
pred_img = pred_img.astype(np.uint8)
|
64 |
+
|
65 |
+
# Draw plot
|
66 |
+
fig = draw_plot(pred_img, seg, colormap, labels_list)
|
67 |
+
return fig
|
68 |
+
|
69 |
+
# ์ฌ๋ฌ๋ถ์ด ๊ฐ์ง labels.txt ํ์ผ์ ๋ด์ฉ์ labels_list์ ํ ๋นํ์ธ์.
|
70 |
+
labels_list = ["label1", "label2", ...]
|
71 |
+
|
72 |
+
demo = gr.Interface(
|
73 |
+
fn=huggingface_model,
|
74 |
+
inputs=gr.Image(shape=(1024, 1024)), # ์
๋ ฅ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ ๋ชจ๋ธ์ ์
๋ ฅ ํฌ๊ธฐ์ ๋ง๊ฒ ์กฐ์ ํด์ผ ํฉ๋๋ค.
|
75 |
+
outputs=["plot"],
|
76 |
+
examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg"],
|
77 |
+
allow_flagging='never'
|
78 |
+
)
|
79 |
+
|
80 |
+
demo.launch()
|