Upload app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,97 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
)
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from matplotlib import gridspec
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import tensorflow as tf
|
8 |
+
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
9 |
+
|
10 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
11 |
+
"nvidia/segformer-b5-finetuned-ade-640-640"
|
12 |
+
)
|
13 |
+
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
14 |
+
"nvidia/segformer-b5-finetuned-ade-640-640"
|
15 |
)
|
16 |
+
|
17 |
+
def ade_palette():
|
18 |
+
"""ADE20K palette that maps each class to RGB values."""
|
19 |
+
return [
|
20 |
+
[255, 0, 0],
|
21 |
+
[255, 255, 0],
|
22 |
+
[255, 255, 255],
|
23 |
+
[0, 255, 0],
|
24 |
+
[0, 0, 255],
|
25 |
+
]
|
26 |
+
|
27 |
+
labels_list = []
|
28 |
+
|
29 |
+
with open(r'labels.txt', 'r') as fp:
|
30 |
+
for line in fp:
|
31 |
+
labels_list.append(line[:-1])
|
32 |
+
|
33 |
+
colormap = np.asarray(ade_palette())
|
34 |
+
|
35 |
+
def label_to_color_image(label):
|
36 |
+
if label.ndim != 2:
|
37 |
+
raise ValueError("Expect 2-D input label")
|
38 |
+
|
39 |
+
if np.max(label) >= len(colormap):
|
40 |
+
raise ValueError("label value too large.")
|
41 |
+
return colormap[label]
|
42 |
+
|
43 |
+
def draw_plot(pred_img, seg):
|
44 |
+
fig = plt.figure(figsize=(20, 15))
|
45 |
+
|
46 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
47 |
+
|
48 |
+
plt.subplot(grid_spec[0])
|
49 |
+
plt.imshow(pred_img)
|
50 |
+
plt.axis('off')
|
51 |
+
LABEL_NAMES = np.asarray(labels_list)
|
52 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
53 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
54 |
+
|
55 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
56 |
+
ax = plt.subplot(grid_spec[1])
|
57 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
58 |
+
ax.yaxis.tick_right()
|
59 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
60 |
+
plt.xticks([], [])
|
61 |
+
ax.tick_params(width=0.0, labelsize=25)
|
62 |
+
return fig
|
63 |
+
|
64 |
+
def sepia(input_img):
|
65 |
+
input_img = Image.fromarray(input_img)
|
66 |
+
|
67 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
68 |
+
outputs = model(**inputs)
|
69 |
+
logits = outputs.logits
|
70 |
+
|
71 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
72 |
+
logits = tf.image.resize(
|
73 |
+
logits, input_img.size[::-1]
|
74 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
75 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
76 |
+
|
77 |
+
color_seg = np.zeros(
|
78 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
79 |
+
) # height, width, 3
|
80 |
+
for label, color in enumerate(colormap):
|
81 |
+
color_seg[seg.numpy() == label, :] = color
|
82 |
+
|
83 |
+
# Show image + mask
|
84 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
85 |
+
pred_img = pred_img.astype(np.uint8)
|
86 |
+
|
87 |
+
fig = draw_plot(pred_img, seg)
|
88 |
+
return fig
|
89 |
+
|
90 |
+
demo = gr.Interface(fn=sepia,
|
91 |
+
inputs=gr.Image(shape=(400, 600)),
|
92 |
+
outputs=['plot'],
|
93 |
+
examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg"],
|
94 |
+
allow_flagging='never')
|
95 |
+
|
96 |
+
|
97 |
+
demo.launch()
|