PSW99 commited on
Commit
7031f4a
1 Parent(s): fc9a955
Files changed (7) hide show
  1. app.py +132 -0
  2. image1.jpg +0 -0
  3. image2.jpg +0 -0
  4. image3.jpg +0 -0
  5. image4.jpg +0 -0
  6. labels.txt +35 -0
  7. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "segments-tobias/segformer-b0-finetuned-segments-sidewalk"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "segments-tobias/segformer-b0-finetuned-segments-sidewalk"
15
+ )
16
+
17
+
18
+ def ade_palette():
19
+ """ADE20K palette that maps each class to RGB values."""
20
+ return [
21
+ [204, 87, 92],
22
+ [112, 185, 212],
23
+ [45, 189, 106],
24
+ [234, 123, 67],
25
+ [78, 56, 123],
26
+ [210, 32, 89],
27
+ [90, 180, 56],
28
+ [155, 102, 200],
29
+ [33, 147, 176],
30
+ [255, 183, 76],
31
+ [67, 123, 89],
32
+ [190, 60, 45],
33
+ [134, 112, 200],
34
+ [56, 45, 189],
35
+ [200, 56, 123],
36
+ [87, 92, 204],
37
+ [120, 56, 123],
38
+ [45, 78, 123],
39
+ [156, 200, 56],
40
+ [32, 90, 210],
41
+ [56, 123, 67],
42
+ [180, 56, 123],
43
+ [123, 67, 45],
44
+ [45, 134, 200],
45
+ [67, 56, 123],
46
+ [78, 123, 67],
47
+ [32, 210, 90],
48
+ [45, 56, 189],
49
+ [123, 56, 123],
50
+ [56, 156, 200],
51
+ [189, 56, 45],
52
+ [112, 200, 56],
53
+ [56, 123, 45],
54
+ [200, 32, 90],
55
+ [123, 45, 78],
56
+ ]
57
+
58
+
59
+ labels_list = []
60
+
61
+ with open(r'labels.txt', 'r') as fp:
62
+ for line in fp:
63
+ labels_list.append(line[:-1])
64
+
65
+ colormap = np.asarray(ade_palette())
66
+
67
+
68
+ def label_to_color_image(label):
69
+ if label.ndim != 2:
70
+ raise ValueError("Expect 2-D input label")
71
+
72
+ if np.max(label) >= len(colormap):
73
+ raise ValueError("label value too large.")
74
+ return colormap[label]
75
+
76
+
77
+ def draw_plot(pred_img, seg):
78
+ fig = plt.figure(figsize=(20, 15))
79
+
80
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
81
+
82
+ plt.subplot(grid_spec[0])
83
+ plt.imshow(pred_img)
84
+ plt.axis('off')
85
+ LABEL_NAMES = np.asarray(labels_list)
86
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
87
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
88
+
89
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
90
+ ax = plt.subplot(grid_spec[1])
91
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
92
+ ax.yaxis.tick_right()
93
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
94
+ plt.xticks([], [])
95
+ ax.tick_params(width=0.0, labelsize=25)
96
+ return fig
97
+
98
+
99
+ def sepia(input_img):
100
+ input_img = Image.fromarray(input_img)
101
+
102
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
103
+ outputs = model(**inputs)
104
+ logits = outputs.logits
105
+
106
+ logits = tf.transpose(logits, [0, 2, 3, 1])
107
+ logits = tf.image.resize(
108
+ logits, input_img.size[::-1]
109
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
110
+ seg = tf.math.argmax(logits, axis=-1)[0]
111
+
112
+ color_seg = np.zeros(
113
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
114
+ ) # height, width, 3
115
+ for label, color in enumerate(colormap):
116
+ color_seg[seg.numpy() == label, :] = color
117
+
118
+ # Show image + mask
119
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
120
+ pred_img = pred_img.astype(np.uint8)
121
+
122
+ fig = draw_plot(pred_img, seg)
123
+ return fig
124
+
125
+
126
+ demo = gr.Interface(fn=sepia,
127
+ inputs=gr.Image(shape=(400, 600)),
128
+ outputs=['plot'],
129
+ examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg"],
130
+ allow_flagging='never')
131
+
132
+ demo.launch()
image1.jpg ADDED
image2.jpg ADDED
image3.jpg ADDED
image4.jpg ADDED
labels.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ unlabeled
2
+ flat-road
3
+ flat-sidewalk
4
+ flat-crosswalk
5
+ flat-cyclinglane
6
+ flat-parkingdriveway
7
+ flat-railtrack
8
+ flat-curb
9
+ human-person
10
+ human-rider
11
+ vehicle-car
12
+ vehicle-truck
13
+ vehicle-bus
14
+ vehicle-tramtrain
15
+ vehicle-motorcycle
16
+ vehicle-bicycle
17
+ vehicle-caravan
18
+ vehicle-cartrailer
19
+ construction-building
20
+ construction-door
21
+ construction-wall
22
+ construction-fenceguardrail
23
+ construction-bridge
24
+ construction-tunnel
25
+ construction-stairs
26
+ object-pole
27
+ object-trafficsign
28
+ object-trafficlight
29
+ nature-vegetation
30
+ nature-terrain
31
+ sky
32
+ void-ground
33
+ void-dynamic
34
+ void-static
35
+ void-unclea
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib