5Grains commited on
Commit
fdbfd80
·
1 Parent(s): 05b21a1

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -203
app.py DELETED
@@ -1,203 +0,0 @@
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
-
11
- feature_extractor = SegformerFeatureExtractor.from_pretrained("prem-timsina/segformer-b0-finetuned-food")
12
- model = TFSegformerForSemanticSegmentation.from_pretrained("prem-timsina/segformer-b0-finetuned-food")
13
- from_pt = True
14
- def ade_palette():
15
- """ADE20K palette that maps each class to RGB values."""
16
- return [
17
- [255, 0, 0],
18
- [255, 127, 127],
19
- [255, 127, 0],
20
- [255, 255, 0],
21
- [127, 255, 0],
22
- [0, 255, 0],
23
- [127, 255, 127],
24
- [0, 255, 127],
25
- [0, 255, 255],
26
- [0, 127, 255],
27
- [0, 0, 255],
28
- [127, 127, 255],
29
- [127, 0, 255],
30
- [255, 0, 255],
31
- [255, 0, 127],
32
- [0, 0, 0],
33
- [127, 127, 127],
34
- [255, 255, 255],
35
- [255, 0, 0],
36
- [255, 127, 127],
37
- [255, 127, 0],
38
- [255, 255, 0],
39
- [127, 255, 0],
40
- [0, 255, 0],
41
- [127, 255, 127],
42
- [0, 255, 127],
43
- [0, 255, 255],
44
- [0, 127, 255],
45
- [0, 0, 255],
46
- [127, 127, 255],
47
- [127, 0, 255],
48
- [255, 0, 255],
49
- [255, 0, 127],
50
- [0, 0, 0],
51
- [127, 127, 127],
52
- [255, 255, 255],
53
- [255, 0, 0],
54
- [255, 127, 127],
55
- [255, 127, 0],
56
- [255, 255, 0],
57
- [127, 255, 0],
58
- [0, 255, 0],
59
- [127, 255, 127],
60
- [0, 255, 127],
61
- [0, 255, 255],
62
- [0, 127, 255],
63
- [0, 0, 255],
64
- [127, 127, 255],
65
- [127, 0, 255],
66
- [255, 0, 255],
67
- [255, 0, 127],
68
- [0, 0, 0],
69
- [127, 127, 127],
70
- [255, 255, 255],
71
- [255, 0, 0],
72
- [255, 127, 127],
73
- [255, 127, 0],
74
- [255, 255, 0],
75
- [127, 255, 0],
76
- [0, 255, 0],
77
- [127, 255, 127],
78
- [0, 255, 127],
79
- [0, 255, 255],
80
- [0, 127, 255],
81
- [0, 0, 255],
82
- [127, 127, 255],
83
- [127, 0, 255],
84
- [255, 0, 255],
85
- [255, 0, 127],
86
- [0, 0, 0],
87
- [127, 127, 127],
88
- [255, 255, 255],
89
- [255, 0, 0],
90
- [255, 127, 127],
91
- [255, 127, 0],
92
- [255, 255, 0],
93
- [127, 255, 0],
94
- [0, 255, 0],
95
- [127, 255, 127],
96
- [0, 255, 127],
97
- [0, 255, 255],
98
- [0, 127, 255],
99
- [0, 0, 255],
100
- [127, 127, 255],
101
- [127, 0, 255],
102
- [255, 0, 255],
103
- [255, 0, 127],
104
- [0, 0, 0],
105
- [127, 127, 127],
106
- [255, 255, 255],
107
- [255, 0, 0],
108
- [255, 127, 127],
109
- [255, 127, 0],
110
- [255, 255, 0],
111
- [127, 255, 0],
112
- [0, 255, 0],
113
- [127, 255, 127],
114
- [0, 255, 127],
115
- [0, 255, 255],
116
- [0, 127, 255],
117
- [0, 0, 255],
118
- [127, 127, 255],
119
- [127, 0, 255],
120
- [255, 0, 255],
121
- [255, 0, 127],
122
- [0, 0, 0],
123
- [127, 127, 127],
124
- [255, 255, 255],
125
- [255, 0, 0],
126
- [255, 127, 127],
127
- [255, 127, 0],
128
- [255, 255, 0],
129
- [127, 255, 0],
130
- [0, 255, 0]
131
- ]
132
-
133
- labels_list = []
134
-
135
- with open(r'labels.txt', 'r') as fp:
136
- for line in fp:
137
- labels_list.append(line[:-1])
138
-
139
- colormap = np.asarray(ade_palette())
140
-
141
- def label_to_color_image(label):
142
- if label.ndim != 2:
143
- raise ValueError("Expect 2-D input label")
144
-
145
- if np.max(label) >= len(colormap):
146
- raise ValueError("label value too large.")
147
- return colormap[label]
148
-
149
- def draw_plot(pred_img, seg):
150
- fig = plt.figure(figsize=(20, 15))
151
-
152
- grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
153
-
154
- plt.subplot(grid_spec[0])
155
- plt.imshow(pred_img)
156
- plt.axis('off')
157
- LABEL_NAMES = np.asarray(labels_list)
158
- FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
159
- FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
160
-
161
- unique_labels = np.unique(seg.numpy().astype("uint8"))
162
- ax = plt.subplot(grid_spec[1])
163
- plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
164
- ax.yaxis.tick_right()
165
- plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
166
- plt.xticks([], [])
167
- ax.tick_params(width=0.0, labelsize=25)
168
- return fig
169
-
170
- def sepia(input_img):
171
- input_img = Image.fromarray(input_img)
172
-
173
- inputs = feature_extractor(images=input_img, return_tensors="tf")
174
- outputs = model(**inputs)
175
- logits = outputs.logits
176
-
177
- logits = tf.transpose(logits, [0, 2, 3, 1])
178
- logits = tf.image.resize(
179
- logits, input_img.size[::-1]
180
- ) # We reverse the shape of `image` because `image.size` returns width and height.
181
- seg = tf.math.argmax(logits, axis=-1)[0]
182
-
183
- color_seg = np.zeros(
184
- (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
185
- ) # height, width, 3
186
- for label, color in enumerate(colormap):
187
- color_seg[seg.numpy() == label, :] = color
188
-
189
- # Show image + mask
190
- pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
191
- pred_img = pred_img.astype(np.uint8)
192
-
193
- fig = draw_plot(pred_img, seg)
194
- return fig
195
-
196
- demo = gr.Interface(fn=sepia,
197
- inputs=gr.Image(shape=(400, 600)),
198
- outputs=['plot'],
199
- examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg", ],
200
- allow_flagging='never')
201
-
202
-
203
- demo.launch()