ysalaun commited on
Commit
a36cede
β€’
1 Parent(s): c1b691f

initial commit

Browse files
Files changed (1) hide show
  1. app.py +289 -0
app.py ADDED
@@ -0,0 +1,289 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # gradio for visual demo
4
+ import gradio as gr
5
+
6
+ # transformers for easy access to nnet
7
+ os.system("pip install git+https://github.com/huggingface/transformers.git")
8
+ os.system("pip install datasets")
9
+ os.system("pip install scipy")
10
+ os.system("pip install torch")
11
+
12
+ from transformers import AutoImageProcessor, AutoModelForImageClassification, DPTForDepthEstimation, Mask2FormerForUniversalSegmentation
13
+ import torch
14
+ import numpy as np
15
+ from PIL import Image
16
+ import requests
17
+ from collections import defaultdict
18
+
19
+ palette = np.asarray([
20
+ [0, 0, 0],
21
+ [120, 120, 120],
22
+ [180, 120, 120],
23
+ [6, 230, 230],
24
+ [80, 50, 50],
25
+ [4, 200, 3],
26
+ [120, 120, 80],
27
+ [140, 140, 140],
28
+ [204, 5, 255],
29
+ [230, 230, 230],
30
+ [4, 250, 7],
31
+ [224, 5, 255],
32
+ [235, 255, 7],
33
+ [150, 5, 61],
34
+ [120, 120, 70],
35
+ [8, 255, 51],
36
+ [255, 6, 82],
37
+ [143, 255, 140],
38
+ [204, 255, 4],
39
+ [255, 51, 7],
40
+ [204, 70, 3],
41
+ [0, 102, 200],
42
+ [61, 230, 250],
43
+ [255, 6, 51],
44
+ [11, 102, 255],
45
+ [255, 7, 71],
46
+ [255, 9, 224],
47
+ [9, 7, 230],
48
+ [220, 220, 220],
49
+ [255, 9, 92],
50
+ [112, 9, 255],
51
+ [8, 255, 214],
52
+ [7, 255, 224],
53
+ [255, 184, 6],
54
+ [10, 255, 71],
55
+ [255, 41, 10],
56
+ [7, 255, 255],
57
+ [224, 255, 8],
58
+ [102, 8, 255],
59
+ [255, 61, 6],
60
+ [255, 194, 7],
61
+ [255, 122, 8],
62
+ [0, 255, 20],
63
+ [255, 8, 41],
64
+ [255, 5, 153],
65
+ [6, 51, 255],
66
+ [235, 12, 255],
67
+ [160, 150, 20],
68
+ [0, 163, 255],
69
+ [140, 140, 140],
70
+ [250, 10, 15],
71
+ [20, 255, 0],
72
+ [31, 255, 0],
73
+ [255, 31, 0],
74
+ [255, 224, 0],
75
+ [153, 255, 0],
76
+ [0, 0, 255],
77
+ [255, 71, 0],
78
+ [0, 235, 255],
79
+ [0, 173, 255],
80
+ [31, 0, 255],
81
+ [11, 200, 200],
82
+ [255, 82, 0],
83
+ [0, 255, 245],
84
+ [0, 61, 255],
85
+ [0, 255, 112],
86
+ [0, 255, 133],
87
+ [255, 0, 0],
88
+ [255, 163, 0],
89
+ [255, 102, 0],
90
+ [194, 255, 0],
91
+ [0, 143, 255],
92
+ [51, 255, 0],
93
+ [0, 82, 255],
94
+ [0, 255, 41],
95
+ [0, 255, 173],
96
+ [10, 0, 255],
97
+ [173, 255, 0],
98
+ [0, 255, 153],
99
+ [255, 92, 0],
100
+ [255, 0, 255],
101
+ [255, 0, 245],
102
+ [255, 0, 102],
103
+ [255, 173, 0],
104
+ [255, 0, 20],
105
+ [255, 184, 184],
106
+ [0, 31, 255],
107
+ [0, 255, 61],
108
+ [0, 71, 255],
109
+ [255, 0, 204],
110
+ [0, 255, 194],
111
+ [0, 255, 82],
112
+ [0, 10, 255],
113
+ [0, 112, 255],
114
+ [51, 0, 255],
115
+ [0, 194, 255],
116
+ [0, 122, 255],
117
+ [0, 255, 163],
118
+ [255, 153, 0],
119
+ [0, 255, 10],
120
+ [255, 112, 0],
121
+ [143, 255, 0],
122
+ [82, 0, 255],
123
+ [163, 255, 0],
124
+ [255, 235, 0],
125
+ [8, 184, 170],
126
+ [133, 0, 255],
127
+ [0, 255, 92],
128
+ [184, 0, 255],
129
+ [255, 0, 31],
130
+ [0, 184, 255],
131
+ [0, 214, 255],
132
+ [255, 0, 112],
133
+ [92, 255, 0],
134
+ [0, 224, 255],
135
+ [112, 224, 255],
136
+ [70, 184, 160],
137
+ [163, 0, 255],
138
+ [153, 0, 255],
139
+ [71, 255, 0],
140
+ [255, 0, 163],
141
+ [255, 204, 0],
142
+ [255, 0, 143],
143
+ [0, 255, 235],
144
+ [133, 255, 0],
145
+ [255, 0, 235],
146
+ [245, 0, 255],
147
+ [255, 0, 122],
148
+ [255, 245, 0],
149
+ [10, 190, 212],
150
+ [214, 255, 0],
151
+ [0, 204, 255],
152
+ [20, 0, 255],
153
+ [255, 255, 0],
154
+ [0, 153, 255],
155
+ [0, 41, 255],
156
+ [0, 255, 204],
157
+ [41, 0, 255],
158
+ [41, 255, 0],
159
+ [173, 0, 255],
160
+ [0, 245, 255],
161
+ [71, 0, 255],
162
+ [122, 0, 255],
163
+ [0, 255, 184],
164
+ [0, 92, 255],
165
+ [184, 255, 0],
166
+ [0, 133, 255],
167
+ [255, 214, 0],
168
+ [25, 194, 194],
169
+ [102, 255, 0],
170
+ [92, 0, 255],
171
+ ])
172
+
173
+ depth_image_processor = AutoImageProcessor.from_pretrained("facebook/dpt-dinov2-small-nyu")
174
+ depth_model = DPTForDepthEstimation.from_pretrained("facebook/dpt-dinov2-small-nyu")
175
+
176
+ def compute_depth(img):
177
+ # prepare image for the model
178
+ inputs = depth_image_processor(images=img, return_tensors="pt")
179
+
180
+ with torch.no_grad():
181
+ outputs = depth_model(**inputs)
182
+ predicted_depth = outputs.predicted_depth
183
+
184
+ # interpolate to original size
185
+ prediction = torch.nn.functional.interpolate(
186
+ predicted_depth.unsqueeze(1),
187
+ size=img.size[::-1],
188
+ mode="bicubic",
189
+ align_corners=False,
190
+ )
191
+
192
+ # visualize the prediction
193
+ output = prediction.squeeze().cpu().numpy()
194
+ formatted = (output * 255 / np.max(output)).astype("uint8")
195
+ depth = Image.fromarray(formatted)
196
+ return [depth, "depth"]
197
+
198
+ clas_processor = AutoImageProcessor.from_pretrained('facebook/dinov2-small-imagenet1k-1-layer')
199
+ clas_model = AutoModelForImageClassification.from_pretrained('facebook/dinov2-small-imagenet1k-1-layer')
200
+
201
+ def compute_clas(img):
202
+ inputs = clas_processor(images=img, return_tensors="pt")
203
+ outputs = clas_model(**inputs)
204
+ logits = outputs.logits
205
+ predicted_class_idx = logits.argmax(-1).item()
206
+ return[img, clas_model.config.id2label[predicted_class_idx]]
207
+
208
+ m2f_processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-base-coco-panoptic")
209
+ m2f_model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-base-coco-panoptic")
210
+
211
+ def seg2sem(seg):
212
+ color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
213
+
214
+ handles = []
215
+ for label, color in enumerate(palette):
216
+ color_seg[seg == label, :] = color
217
+ if (seg == label).count_nonzero() > 0:
218
+ handles.append(m2f_model.config.id2label[label])
219
+ handles.append(color)
220
+
221
+ color_seg = color_seg.astype(np.uint8)
222
+ image = Image.fromarray(color_seg)
223
+
224
+ return [image,handles]
225
+
226
+ def seg2pano(seg, segments_info):
227
+ color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
228
+
229
+ handles = []
230
+ for label, color in enumerate(palette):
231
+ color_seg[seg == label, :] = color
232
+
233
+ color_seg = color_seg.astype(np.uint8)
234
+ image = Image.fromarray(color_seg)
235
+
236
+ instances_counter = defaultdict(int)
237
+ handles = []
238
+ for segment in segments_info:
239
+ segment_id = segment['id']
240
+ segment_label_id = segment['label_id']
241
+ segment_label = m2f_model.config.id2label[segment_label_id]
242
+ label = f"{segment_label}-{instances_counter[segment_label_id]}"
243
+ instances_counter[segment_label_id] += 1
244
+ color = palette[segment_id]
245
+ handles.append(label)
246
+ handles.append(color)
247
+ return [image,handles]
248
+
249
+ def compute_m2f_sem_seg(img):
250
+ inputs = m2f_processor(images=img, return_tensors="pt")
251
+
252
+ with torch.no_grad():
253
+ outputs = m2f_model(**inputs)
254
+
255
+ seg = m2f_processor.post_process_semantic_segmentation(
256
+ outputs, target_sizes=[img.size[::-1]]
257
+ )[0]
258
+
259
+ return seg2sem(seg)
260
+
261
+ def compute_m2f_pano_seg(img):
262
+ inputs = m2f_processor(images=img, return_tensors="pt")
263
+
264
+ with torch.no_grad():
265
+ outputs = m2f_model(**inputs)
266
+
267
+ seg = m2f_processor.post_process_panoptic_segmentation(
268
+ outputs, target_sizes=[img.size[::-1]]
269
+ )[0]
270
+
271
+ return seg2pano(seg["segmentation"], seg["segments_info"])
272
+
273
+ labels = ["Dinov2 - Depth", "Dinov2 - Classification", "M2F - Semantic Segmentation", "M2F - Panoptic Segmentation"]
274
+
275
+ # main function
276
+ def detect(img, application):
277
+ if application == labels[0]:
278
+ return compute_depth(img)
279
+ elif application == labels[1]:
280
+ return compute_clas(img)
281
+ elif application == labels[2]:
282
+ return compute_m2f_sem_seg(img)
283
+ elif application == labels[3]:
284
+ return compute_m2f_pano_seg(img)
285
+ return img
286
+
287
+ # visual gradio interface
288
+ iface = gr.Interface(fn=detect, inputs=[gr.Image(type="pil"), gr.Radio(labels, label="Application")], outputs=[gr.Image(type="pil"), gr.Textbox()])
289
+ iface.launch(debug=True)