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