-
Notifications
You must be signed in to change notification settings - Fork 2
/
CResults.py
393 lines (311 loc) · 12.3 KB
/
CResults.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import json
from copy import deepcopy
import numpy as np
import cv2 as cv
class Bbox():
def __init__(self, lBoxXYXY: list):
self.MinX = float(lBoxXYXY[0])
self.MinY = float(lBoxXYXY[1])
self.MaxX = float(lBoxXYXY[2])
self.MaxY = float(lBoxXYXY[3])
self.Xc = (self.MinX + self.MaxX) / 2
self.Yc = (self.MinY + self.MaxY) / 2
def copy(self):
return deepcopy(self)
def scale_by(self, fScaleX: float, fScaleY: float):
"""
Scale BBOX by a factor
"""
self.MinX = self.MinX*fScaleX
self.MinY = self.MinY*fScaleY
self.MaxX = self.MaxX*fScaleX
self.MaxY = self.MaxY*fScaleY
self.Xc = self.Xc*fScaleX
self.Yc = self.Yc*fScaleY
return self
def offset_by(self, fOffsetX: float, fOffsetY: float):
"""
Offset BBOX by a factor
"""
self.MinX = self.MinX+fOffsetX
self.MinY = self.MinY+fOffsetY
self.MaxX = self.MaxX+fOffsetX
self.MaxY = self.MaxY+fOffsetY
self.Xc = self.Xc+fOffsetX
self.Yc = self.Yc+fOffsetY
return self
def round(self):
"""
Round BBOX coordinates
"""
self.MinX = int(round(self.MinX))
self.MinY = int(round(self.MinY))
self.MaxX = int(round(self.MaxX))
self.MaxY = int(round(self.MaxY))
self.Xc = int(round(self.Xc))
self.Yc = int(round(self.Yc))
return self
def get_xywh(self):
"""
Returns: [xmin, ymin, width, height]
"""
return [self.MinX, self.MinY, self.MaxX - self.MinX, self.MaxY - self.MinY]
def get_xywh_yolo(self):
"""
Returns: [xc, yc, width, height]
"""
return [self.Xc, self.Yc, self.MaxX - self.MinX, self.MaxY - self.MinY]
def get_xyxy(self):
"""
Returns: [xmin, ymin, xmax, ymax]
"""
return [self.MinX, self.MinY, self.MaxX, self.MaxY]
def get_center(self):
"""
Returns BBOX center
"""
return [self.Xc, self.Yc]
class Polygon():
def __init__(self, aPolygon: np.ndarray):
if len(aPolygon.shape) == 2:
aPolygon = aPolygon.reshape(-1,1,2)
self.aPolygon = aPolygon.astype(float)
def exists(self):
return len(self.aPolygon)>0
def copy(self):
return deepcopy(self)
def approx(self, fEps: float):
"""
Approximate polygon
"""
if self.exists():
fPeri = cv.arcLength(self.aPolygon, True)
self.aPolygon = cv.approxPolyDP(self.aPolygon, fEps * fPeri, True)
return self
def scale_by(self, fScaleX: float, fScaleY: float):
"""
Scale BBOX by a factor
"""
if self.exists():
self.aPolygon[:,0,0] *= fScaleX
self.aPolygon[:,0,1] *= fScaleY
return self
def offset_by(self, fOffsetX: float, fOffsetY: float):
"""
Offset BBOX by a factor
"""
if self.exists():
self.aPolygon[:,0,0] += fOffsetX
self.aPolygon[:,0,1] += fOffsetY
return self
def round(self):
"""
Round BBOX coordinates
"""
if self.exists():
self.aPolygon = np.round(self.aPolygon).astype(np.int32)
return self
def get_array(self):
"""
Returns polygon array
"""
return self.aPolygon
def get_yolo(self):
"""
Returns polygon in YOLO format (flatten)
"""
self.aPolygon.flatten()
class Prediction():
def __init__(self, sClass: str, iClass: int, fScore: float, lBoxXYXY: list[float] | list[int], aPolygon: np.ndarray = np.array([])):
self.BoundingBox = Bbox(lBoxXYXY)
self.Polygon = Polygon(aPolygon)
self.sClass = sClass
self.iClass = iClass
self.fScore = fScore
def copy(self):
return deepcopy(self)
def approx_polygon(self, fEps: float = 0.0080):
self.Polygon.approx(fEps)
return self
def set_polygon(self, aPolygon: np.ndarray):
self.Polygon = Polygon(aPolygon)
return self
def get_bbox(self):
return self.BoundingBox.copy()
def get_polygon(self):
return self.Polygon.copy()
def merge(self, Prediction2: Polygon):
#Merge scores
self.fScore = (self.fScore + float(Prediction2.fScore))/2.0
# Merge polygons
aTmpMask = np.logical_or(self.Polygon.get_mask(), Prediction2.get_polygon().get_mask()).astype(np.uint8)*255
aCntr = cv.findContours(aTmpMask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[0][0].astype(float)
aCntr[:,0,0] /= aTmpMask.shape[1]
aCntr[:,0,1] /= aTmpMask.shape[0]
self.Polygon = Polygon(aCntr)
# Get BBOX
self.BoundingBox = Bbox([
aCntr[:,:,0].min(),
aCntr[:,:,1].min(),
aCntr[:,:,0].max(),
aCntr[:,:,1].max(),
])
return self
def cut(self, Prediction2: Polygon, iDilate: int = 0):
AREA_THRESH_ACCEPT_NONE = 250
AREA_THRESH_ACCEPT_ALL = 2500
# Get polygons
aTmpMask = Prediction2.get_polygon().get_mask()
if iDilate>0: aTmpMask = cv.dilate(aTmpMask, cv.getStructuringElement(cv.MORPH_ELLIPSE, (iDilate,iDilate)))
aTmpMask = np.logical_and(self.Polygon.get_mask(), np.logical_not(aTmpMask)).astype(np.uint8)*255
lContours, lBBOXes = [],[]
for aCntr in cv.findContours(aTmpMask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[0]:
aCntr = aCntr.astype(float)
# Filter contours
fArea = cv.contourArea(aCntr)
if fArea<AREA_THRESH_ACCEPT_NONE:
continue
elif fArea<AREA_THRESH_ACCEPT_ALL:
(x,y),(w,h),ang = cv.fitEllipse(aCntr)
if min(w,h)/max(w,h) < 0.35: continue # stretched
else:
pass
aCntr[:,0,0] /= aTmpMask.shape[1]
aCntr[:,0,1] /= aTmpMask.shape[0]
lContours.append(aCntr)
lBBOXes.append([
aCntr[:,:,0].min(),
aCntr[:,:,1].min(),
aCntr[:,:,0].max(),
aCntr[:,:,1].max(),
])
return [self.new(self.sClass, self.fScore, _BBOX, _Poly) for _Poly, _BBOX in zip(lContours, lBBOXes)]
@classmethod
def from_dict(self, dcData: dict):
return self(
sClass = dcData['class'],
fScore = dcData['score'],
lBoxXYXY = dcData['bbox'],
aPolygon = np.array(dcData['polygon'], dtype=float).reshape(-1,1,2)
).approx_polygon()
@classmethod
def new(self, sClass: str, fScore: float, lBoxXYXY: list, aPolygon: np.ndarray = []):
return self(
sClass = sClass,
fScore = fScore,
lBoxXYXY = lBoxXYXY,
aPolygon = aPolygon
)
def to_dict(self):
return {
"class": self.sClass,
"score": self.fScore,
"bbox": self.BoundingBox.get_xyxy(),
"polygon": self.Polygon.get_array().tolist() if self.Polygon.exists() else []
}
class ImageResults():
def __init__(self, sImageID: str, tImageShape: tuple, lPredictions: list[Prediction], fInferenceTime: float = None):
self.sImageID = sImageID
self.tImageShape = tImageShape
self.fInferenceTime = fInferenceTime
self.load_predictions(lPredictions)
def get_inference_time(self):
return self.fInferenceTime
def get_n_predictions(self):
return len(self.lPredictions)
def get_n_predictions_by_class(self, sClass: str):
return len([pred for pred in self.get_predictions() if pred.sClass==sClass])
def load_predictions(self, lPredictions: list[Prediction]):
self.lPredictions = lPredictions
def set_prediction_by_index(self, iIndex: int, cPred: Prediction):
if iIndex < len(self.lPredictions):
self.lPredictions[iIndex] = cPred
def add_predictions(self, lPredictions: list[Prediction]):
self.lPredictions += lPredictions
def remove_predictions_by_index(self, lPredictions: list[int]):
self.lPredictions = [pred for i,pred in enumerate(self.get_predictions()) if i not in lPredictions]
def list_results(self):
print(f"Image \'{self.sImageID}\' - {self.get_n_predictions()} detections.")
for i, pred in enumerate(self.get_predictions()):
print(f"\t{i+1}) {pred.sClass} with score {pred.fScore:.2f}")
def to_dict(self):
return {
"id": self.sImageID,
"width": self.tImageShape[1],
"height": self.tImageShape[0],
"predictions": [pred.to_dict() for pred in self.lPredictions]
}
def to_json(self, sPath: str):
with open(sPath.split('.')[0]+'.json', 'w') as f:
json.dump(self.to_dict(), f)
f.close()
def get_coco_detection(self, iImageID: int):
return [{
"image_id": int(iImageID),
"category_id": int(pred.iClass),
"score": float(pred.fScore),
"bbox": pred.BoundingBox.copy().round().get_xywh(),
"segmentation": pred.Polygon.get_array().reshape(-1,2).astype(float) if pred.Polygon.exists() else []
} for pred in self.lPredictions]
def get_yolo_detection(self):
lResults = []
for pred in self.lPredictions:
if pred.Polygon.exists():
sTmpLine = f"{int(pred.iClass)}"
for p in pred.get_polygon().scale_by(1.0/self.tImageShape[1],1.0/self.tImageShape[0]).get_yolo():
sTmpLine += f"{float(p):.5f}"
lResults.append(sTmpLine)
else:
lBbox = pred.BoundingBox.scale_by(1.0/self.tImageShape[1],1.0/self.tImageShape[0]).get_xywh_yolo()
lResults.append(f"{int(pred.iClass)} {lBbox[0]:.5f} {lBbox[1]:.5f} {lBbox[2]:.5f} {lBbox[3]:.5f}")
return lResults
def get_predictions(self) -> list[Prediction]:
return deepcopy(self.lPredictions)
def get_predictions_by_class(self, sClass: str):
return [pred for pred in self.get_predictions() if pred.sClass==sClass]
def get_data_visualisation(self, tTargetShape: tuple = None):
if tTargetShape is None: tTargetShape = self.tImageShape
return [
{
"bbox": pred.BoundingBox.copy().scale_by(tTargetShape[1], tTargetShape[0]).round().get_xyxy(),
"polygon": pred.Polygon.copy().scale_by(tTargetShape[1], tTargetShape[0]).round().get_array(),
"class": pred.sClass,
"score": pred.fScore
}
for pred in self.get_predictions()
]
def get_data_mask_generation(self, tTargetShape: tuple = None):
if tTargetShape is None: tTargetShape = self.tImageShape
return [
pred.Polygon.copy().scale_by(tTargetShape[1], tTargetShape[0]).round().get_array()
for pred in self.get_predictions()
]
def combine_predictions(self, fRCAthresh: float = 0.5):
"""
Combine predictions
"""
lPredictionsCombined = []
for sClass in ('added', 'removed'):
lBuffer = [pred for pred in self.get_predictions() if pred.sClass==sClass]
i,j = 0,1
while len(lBuffer)>1:
if j>=len(lBuffer):
i+=1
j=0
if i>=len(lBuffer) and not bIsChange:
break
i %= len(lBuffer)
j %= len(lBuffer)
if i != j:
fCommonAreaRelative = lBuffer[i].Polygon.get_common_area_realtive_to_min(lBuffer[j].Polygon)
if fCommonAreaRelative>fRCAthresh:
lBuffer[i].merge(lBuffer[j])
lBuffer.pop(j)
bIsChange = True
if j<i: i-=1
continue
bIsChange = False
j+=1
lPredictionsCombined += lBuffer
self.load_predictions(lPredictionsCombined)
return self