Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
from matplotlib import gridspec
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
-
|
7 |
-
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
)
|
13 |
-
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
14 |
-
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
|
15 |
-
)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
[178, 235, 244],
|
24 |
-
[0, 51, 153],
|
25 |
-
[181, 178, 255],
|
26 |
-
[128, 65, 217],
|
27 |
-
[255, 178, 245],
|
28 |
-
[153, 0, 76],
|
29 |
-
[25, 186, 52],
|
30 |
-
[81, 162, 235],
|
31 |
-
[255, 255, 0],
|
32 |
-
[62, 57, 159],
|
33 |
-
[91, 189, 203],
|
34 |
-
[0, 0, 255],
|
35 |
-
[0, 255, 255],
|
36 |
-
[12, 168, 0],
|
37 |
-
[255, 0, 0],
|
38 |
-
[231, 32, 65]
|
39 |
-
]
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
labels_list.append(line[:-1])
|
46 |
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
raise ValueError("Expect 2-D input label")
|
52 |
-
|
53 |
-
if np.max(label) >= len(colormap):
|
54 |
-
raise ValueError("label value too large.")
|
55 |
-
return colormap[label]
|
56 |
-
|
57 |
-
def draw_plot(pred_img, seg):
|
58 |
-
fig = plt.figure(figsize=(20, 15))
|
59 |
-
|
60 |
-
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
61 |
-
|
62 |
-
plt.subplot(grid_spec[0])
|
63 |
-
plt.imshow(pred_img)
|
64 |
-
plt.axis('off')
|
65 |
-
LABEL_NAMES = np.asarray(labels_list)
|
66 |
-
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
67 |
-
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
68 |
-
|
69 |
-
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
70 |
-
ax = plt.subplot(grid_spec[1])
|
71 |
-
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
72 |
-
ax.yaxis.tick_right()
|
73 |
-
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
74 |
-
plt.xticks([], [])
|
75 |
-
ax.tick_params(width=0.0, labelsize=25)
|
76 |
-
return fig
|
77 |
-
|
78 |
-
def sepia(input_img):
|
79 |
-
input_img = Image.fromarray(input_img)
|
80 |
-
|
81 |
-
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
82 |
-
outputs = model(**inputs)
|
83 |
-
logits = outputs.logits
|
84 |
-
|
85 |
-
logits = tf.transpose(logits, [0, 2, 3, 1])
|
86 |
-
logits = tf.image.resize(
|
87 |
-
logits, input_img.size[::-1]
|
88 |
-
)
|
89 |
-
seg = tf.math.argmax(logits, axis=-1)[0]
|
90 |
-
|
91 |
-
color_seg = np.zeros(
|
92 |
-
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
93 |
-
)
|
94 |
-
for label, color in enumerate(colormap):
|
95 |
-
color_seg[seg.numpy() == label, :] = color
|
96 |
-
|
97 |
-
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
98 |
-
pred_img = pred_img.astype(np.uint8)
|
99 |
-
|
100 |
-
fig = draw_plot(pred_img, seg)
|
101 |
-
return fig
|
102 |
-
|
103 |
-
demo = gr.Interface(fn=sepia,
|
104 |
-
inputs=gr.Image(shape=(800, 600)),
|
105 |
-
outputs=['plot'],
|
106 |
-
examples=["cityoutdoor-1.jpg", "cityoutdoor-2.jpg", "cityoutdoor-3.jpg"],
|
107 |
-
allow_flagging='never')
|
108 |
-
|
109 |
-
|
110 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
+
from tensorflow.keras.models import load_model
|
|
|
5 |
|
6 |
+
# ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ์ ๋ถ๋ฌ์ต๋๋ค. ์ฌ๊ธฐ์์๋ ๊ฐ๋จํ ์์ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
|
7 |
+
model = load_model('nvidia/segformer-b1-finetuned-cityscapes-1024-1024') # ๋ชจ๋ธ ๊ฒฝ๋ก๋ฅผ ์ ์ ํ๊ฒ ์์ ํ์ธ์.
|
8 |
|
9 |
+
# ๋ชจ๋ธ ์
๋ ฅ ํฌ๊ธฐ๋ฅผ ํ์ธํฉ๋๋ค.
|
10 |
+
input_size = model.input_shape[1:3]
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# ๋ชจ๋ธ ์์ธก ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
|
13 |
+
def classify_image(img):
|
14 |
+
# ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ธ ์
๋ ฅ ํฌ๊ธฐ์ ๋ง๊ฒ ์กฐ์ ํฉ๋๋ค.
|
15 |
+
img = img.resize(input_size)
|
16 |
+
img_array = np.array(img) / 255.0 # ์ด๋ฏธ์ง๋ฅผ 0์์ 1 ์ฌ์ด๋ก ์ ๊ทํํฉ๋๋ค.
|
17 |
+
img_array = np.expand_dims(img_array, axis=0) # ๋ฐฐ์น ์ฐจ์์ ์ถ๊ฐํฉ๋๋ค.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# ๋ชจ๋ธ๋ก ์์ธก์ ์ํํฉ๋๋ค.
|
20 |
+
predictions = model.predict(img_array)
|
21 |
+
|
22 |
+
# ์์ธก ๊ฒฐ๊ณผ ์ค์์ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ์ ํํฉ๋๋ค.
|
23 |
+
predicted_label = np.argmax(predictions)
|
24 |
|
25 |
+
# ๋ผ๋ฒจ์ ๋ฐํํฉ๋๋ค.
|
26 |
+
return predicted_label
|
|
|
27 |
|
28 |
+
# Gradio UI๋ฅผ ์์ฑํฉ๋๋ค.
|
29 |
+
iface = gr.Interface(fn=classify_image, inputs="image", outputs="label", live=True)
|
30 |
|
31 |
+
# Gradio UI๋ฅผ ์์ํฉ๋๋ค.
|
32 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|