forked from open-mmlab/mmdetection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamsters_utils.py
323 lines (269 loc) · 11.2 KB
/
hamsters_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from random import randint
import cv2
import mmcv
import numpy as np
import os
import torch
import torch.distributed as dist
import torch.nn as nn
from mmcv.runner import auto_fp16
from mmcv.utils import print_log
from mmcv.image import imread, imwrite
from mmdet.utils import get_root_logger
class PostProcessor():
def __init__(self,
classes,
score_thr=0.3,
thickness=1,
font_scale=0.5,
win_name='',
wait_time=0):
self.num_classes = len(classes)
self.classes = classes
self.score_thr = score_thr
self.thickness = thickness
self.font_scale = font_scale
self.win_name = win_name
self.wait_time = wait_time
self.colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0), (0, 255, 255),
(255, 0, 255), (255, 255, 255), (0, 0, 0), (255, 0, 128), (0, 191, 255),
(10, 255, 128), (191, 255, 0), (255, 191, 0), (255, 128, 10), (50, 152, 89)]
self.makeColors()
self.iitpID = 1
self.iitpJson = {'annotations':[]}
self.box_real_class = [0, 1, 1, 2, 3, 4, 5, 6, 5, 6]
def makeColors(self):
if len(self.colors) >= self.num_classes:
return
else:
while len(self.colors) < self.num_classes:
self.colors.append((randint(20, 230), randint(20, 230), randint(20, 230)))
return
def saveResult(self,
img,
result,
show=False,
out_file=None):
img, bboxes, labels = self.extractInfo(img, result, show=False, out_file=out_file)
# draw bounding boxes
return self.imshow_det_bboxes(img, bboxes, labels, show=show, out_file=out_file)
def labelChanger(self, labels):
appliedLabels = []
for i in labels:
if i == 8:
if 3 in labels or 4 in labels:
i = 3
if i == 9:
if 3 in labels:
i = 3
elif 4 in labels:
i = 0
i = self.box_real_class[i]
i += 1
appliedLabels.append(i)
return appliedLabels
def saveIitp(self, img, imgPath, result):
_, bboxes, labels = self.extractInfo(img, result, show=False, out_file=None)
bboxes, labels = self.iitpProcess(bboxes, labels)
if len(labels) < 1:
return False
return self.annoMaker(imgPath, bboxes, labels)
def annoMaker(self, imgPath, bboxes, labels, labelChanger=True):
anno = {}
anno['id'] = self.iitpID
self.iitpID += 1
if labelChanger:
labels = self.labelChanger(labels)
fileName = imgPath.split('/')[-1]
anno['file_name'] = fileName
anno['object'] = []
for box, label in zip(bboxes, labels):
anno['object'].append({
'box': box,
'label': 'c'+str(label)
})
self.iitpJson['annotations'].append(anno)
return labels
def iitpProcess(self, bboxes, labels):
assert bboxes.ndim == 2
assert labels.ndim == 1
assert bboxes.shape[0] == labels.shape[0]
assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
if self.score_thr > 0:
assert bboxes.shape[1] == 5
scores = bboxes[:, -1]
inds = scores > self.score_thr
bboxes = bboxes[inds, :]
labels = labels[inds]
processedBoxes = []
for box in bboxes:
box = box.tolist()
box.pop()
box = list(map(int, box))
processedBoxes.append(box)
return processedBoxes, labels
def bb_intersection_over_union(self, bboxes, labels, box_scores):
# determine the (x, y)-coordinates of the intersection rectangle
best_indexes = []
for i in range(0, len(bboxes) - 1):
best_iou = -1
best_list = []
for j in range(i + 1 , len(bboxes)):
xA = max(bboxes[i][0], bboxes[j][0])
yA = max(bboxes[i][1], bboxes[j][1])
xB = min(bboxes[i][2], bboxes[j][2])
yB = min(bboxes[i][3], bboxes[j][3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (bboxes[i][2] - bboxes[i][0] + 1) * (bboxes[i][3] - bboxes[i][1] + 1)
boxBArea = (bboxes[j][2] - bboxes[j][0] + 1) * (bboxes[j][3] - bboxes[j][1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
if iou > best_iou:
best_iou = iou
best_list = [i , j, best_iou]
best_indexes.append(best_list)
index = []
for best_index in best_indexes:
if best_index[2] > 0.98: # best_iou
if box_scores[best_index[0]] > box_scores[best_index[1]]:
index.append(best_index[1])
else :
index.append(best_index[0])
index = set(index)
index = sorted(list(index), reverse=True)
for i in index :
if box_scores[i] < 0.35:
bboxes = np.delete(bboxes, i, axis = 0)
labels = np.delete(labels, i, axis = 0)
box_scores = np.delete(box_scores, i, axis = 0)
# return the intersection over union value
return bboxes, labels, box_scores
def cropBoxes(self, img, result, out_file=None):
img, bboxes, labels = self.extractInfo(img, result, show=False, out_file=out_file)
assert bboxes.ndim == 2
assert labels.ndim == 1
assert bboxes.shape[0] == labels.shape[0]
assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
img = imread(img)
box_scores = []
if self.score_thr > 0:
assert bboxes.shape[1] == 5
scores = bboxes[:, -1]
inds = scores > self.score_thr
bboxes = bboxes[inds, :]
labels = labels[inds]
box_scores = scores[inds]
img = np.ascontiguousarray(img)
croppedImgs = []
out_label = []
if len(labels) > 1:
bboxes, labels, box_scores = self.bb_intersection_over_union(bboxes, labels, box_scores)
# path to save cropped image if save
# splitPath = out_file.split('/')
# fileName = splitPath.pop(-1).split('.')[0]
for idx, (bbox, label) in enumerate(zip(bboxes, labels)):
# !!!!!!!!!!! ~ Except class cap(8) or label(9) ~ !!!!!!!!!!!!
if label != 8 and label != 9:
bbox_int = bbox.astype(np.int32)
heightRange = (bbox_int[1], bbox_int[3])
widthRange = (bbox_int[0], bbox_int[2])
dst = img.copy()
center_x = int(int(bbox_int[0]) - int(bbox_int[0])*0.15)
center_y = int(int(bbox_int[1]) - int(bbox_int[0])*0.15)
width = int(int(bbox_int[2]) + int(bbox_int[2])*0.15)
height = int(int(bbox_int[3]) + int(bbox_int[3])*0.15)
dst = dst[center_y:height, center_x:width]
# dst = dst[bbox_int[1]:bbox_int[3], bbox_int[0]:bbox_int[2]]
croppedImgs.append(dst)
out_label.append(label)
# save cropped image
# out_file = splitPath.copy()
# out_file.append(fileName+'_'+str(idx)+'.jpg')
# out_file = '/'.join(out_file)
# if out_file is not None:
# imwrite(dst, out_file)
out_label = self.labelChanger(out_label)
return croppedImgs, out_label
def extractInfo(self,
img,
result,
show=False,
out_file=None):
img = mmcv.imread(img)
img = img.copy()
if isinstance(result, tuple):
bbox_result, segm_result = result
if isinstance(segm_result, tuple):
segm_result = segm_result[0] # ms rcnn
# print('check msrcnn : ', len(segm_result))
else:
bbox_result, segm_result = result, None
bboxes = np.vstack(bbox_result)
labels = [
np.full(bbox.shape[0], i, dtype=np.int32)
for i, bbox in enumerate(bbox_result)
]
labels = np.concatenate(labels)
# draw segmentation masks
if segm_result is not None and len(labels) > 0: # non empty
# print('check segm_result is not None')
segms = mmcv.concat_list(segm_result)
inds = np.where(bboxes[:, -1] > self.score_thr)[0]
np.random.seed(42)
color_masks = [
np.random.randint(0, 256, (1, 3), dtype=np.uint8)
for _ in range(max(labels) + 1)
]
for i in inds:
i = int(i)
color_mask = color_masks[labels[i]]
mask = segms[i].astype(bool)
img[mask] = img[mask] * 0.5 + color_mask * 0.5
# if out_file specified, do not show image in window
if out_file is not None:
show = False
# if not (show or out_file):
# return img
return img, bboxes, labels
def imshow_det_bboxes(self,
img,
bboxes,
labels,
show=True,
out_file=None):
assert bboxes.ndim == 2
assert labels.ndim == 1
assert bboxes.shape[0] == labels.shape[0]
assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
img = imread(img)
if self.score_thr > 0:
assert bboxes.shape[1] == 5
scores = bboxes[:, -1]
inds = scores > self.score_thr
bboxes = bboxes[inds, :]
labels = labels[inds]
img = np.ascontiguousarray(img)
for bbox, label in zip(bboxes, labels):
bbox_int = bbox.astype(np.int32)
left_top = (bbox_int[0], bbox_int[1])
right_bottom = (bbox_int[2], bbox_int[3])
cv2.rectangle(
img, left_top, right_bottom, self.colors[label], thickness=self.thickness)
label_text = self.classes[
label] if self.classes is not None else f'cls {label}'
if len(bbox) > 4:
label_text += f'|{bbox[-1]:.02f}'
cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - (label*2*randint(0, 1))),
cv2.FONT_HERSHEY_COMPLEX, self.font_scale, self.colors[label])
if show:
imshow(img, self.win_name, self.wait_time)
if out_file is not None:
imwrite(img, out_file)
return img