-
Notifications
You must be signed in to change notification settings - Fork 10
/
anot_utils.py
379 lines (309 loc) · 12.3 KB
/
anot_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
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
import cv2
import numpy as np
import os
import xml.etree.ElementTree as ET
from lxml import etree
from torch import Tensor
import torch
DEFAULT_ENCODING = 'utf-8'
# Read Classes.txt
def read_txt_lines(path_to_txt):
with open(path_to_txt, 'r') as f:
lines = f.readlines()
lines = [line.rstrip() for line in lines]
return lines
# Conver into Class Names
def findClass(class_id, class_names):
class_name = class_names[int(class_id)-1]
return class_name
# Remove Class from Annotaion
def remove_class(class_list, id, remove_list):
name = class_list[id]
return name in set(remove_list)
# to get bounding box from ONNX model
def findBBox(onnx_session, img, img_resize, threshold, class_name_list, remove_list, keep_list):
# onnx session
input_name = onnx_session.get_inputs()[0].name
# Image
h, w, c = img.shape
img_resized = cv2.resize(img, (img_resize, img_resize))
img_data = np.reshape(img_resized, (1, img_resize, img_resize, 3))
img_data = img_data.astype('uint8')
ort_inputs = {input_name: img_data}
ort_outs = onnx_session.run(None, ort_inputs)
bbox_list = []
class_list = []
confidence = []
c = 0
for i in ort_outs[4][0]:
if i > threshold:
# Remove specfic classes from Annotation
if len(remove_list)>0:
if not remove_class(class_name_list, int(ort_outs[2][0][c]), remove_list):
bbox = ort_outs[1][0][c]
ymin = (bbox[0])
xmin = (bbox[1])
ymax = (bbox[2])
xmax = (bbox[3])
# cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
xmin, ymin, xmax, ymax = int(
xmin*w), int(ymin*h), int(xmax*w), int(ymax*h)
bbox_list.append([xmin, ymin, xmax, ymax])
# Detection Classes
class_list.append(ort_outs[2][0][c])
# confidence
confidence.append(i)
# Keep specfic classes from Annotation
elif len(keep_list)>0:
if remove_class(class_name_list, int(ort_outs[2][0][c]), keep_list):
bbox = ort_outs[1][0][c]
ymin = (bbox[0])
xmin = (bbox[1])
ymax = (bbox[2])
xmax = (bbox[3])
# cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
xmin, ymin, xmax, ymax = int(
xmin*w), int(ymin*h), int(xmax*w), int(ymax*h)
bbox_list.append([xmin, ymin, xmax, ymax])
# Detection Classes
class_list.append(ort_outs[2][0][c])
# confidence
confidence.append(i)
# All classes
else:
bbox = ort_outs[1][0][c]
ymin = (bbox[0])
xmin = (bbox[1])
ymax = (bbox[2])
xmax = (bbox[3])
# cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
xmin, ymin, xmax, ymax = int(
xmin*w), int(ymin*h), int(xmax*w), int(ymax*h)
bbox_list.append([xmin, ymin, xmax, ymax])
# Detection Classes
class_list.append(ort_outs[2][0][c])
# confidence
confidence.append(i)
c += 1
return bbox_list, class_list, confidence
# function to convert XML file
def save_xml(folder_name, file_name, path_txt, width_n, height_n, depth_n, obj_list, class_list, class_names):
data = ET.Element('annotation')
folder = ET.SubElement(data, 'folder')
filename = ET.SubElement(data, 'filename')
path = ET.SubElement(data, 'path')
folder.text = f'{folder_name}'
filename.text = f"{file_name}"
path.text = f"{path_txt}"
source = ET.SubElement(data, 'source')
database = ET.SubElement(source, 'database')
database.text = 'Unknown'
size = ET.SubElement(data, 'size')
width = ET.SubElement(size, 'width')
height = ET.SubElement(size, 'height')
depth = ET.SubElement(size, 'depth')
width.text = f'{width_n}'
height.text = f'{height_n}'
depth.text = f'{depth_n}'
segmented = ET.SubElement(data, 'segmented')
segmented.text = '0'
# Object
for obj, class_id in zip(obj_list, class_list):
object = ET.SubElement(data, 'object')
name = ET.SubElement(object, 'name')
pose = ET.SubElement(object, 'pose')
truncated = ET.SubElement(object, 'truncated')
difficult = ET.SubElement(object, 'difficult')
bndbox = ET.SubElement(object, 'bndbox')
# BBox
xmin = ET.SubElement(bndbox, 'xmin')
ymin = ET.SubElement(bndbox, 'ymin')
xmax = ET.SubElement(bndbox, 'xmax')
ymax = ET.SubElement(bndbox, 'ymax')
name.text = f'{findClass(class_id, class_names)}'
pose.text = 'Unspecified'
truncated.text = '0'
difficult.text = '0'
xmin.text = f'{obj[0]}'
ymin.text = f'{obj[1]}'
xmax.text = f'{obj[2]}'
ymax.text = f'{obj[3]}'
# Save
sample_xml = ET.tostring(data, 'utf8')
root = etree.fromstring(sample_xml)
xml_str = etree.tostring(root, pretty_print=True, encoding=DEFAULT_ENCODING).replace(
" ".encode(), "\t".encode())
path_to_dir = os.path.split(path_txt)[0]
xml_file_name = os.path.splitext(file_name)[0] + '.xml'
path_to_save = os.path.join(path_to_dir, xml_file_name)
with open(f'{path_to_save}', 'w') as file:
file.write(xml_str.decode('utf8'))
print(f'Successfully Created {xml_file_name}')
# Function to convert YOLO (.txt) format
def save_yolo(folder_name, file_name, w, h, bbox_list, class_list):
txt_name = os.path.splitext(file_name)[0] + '.txt'
path_to_save = os.path.join(folder_name, txt_name)
out_file = open(path_to_save, 'w')
for box, class_index in zip(bbox_list, class_list):
x_min = box[0]
y_min = box[1]
x_max = box[2]
y_max = box[3]
x_center = float((x_min + x_max)) / 2 / w
y_center = float((y_min + y_max)) / 2 / h
width = float((x_max - x_min)) / w
height = float((y_max - y_min)) / h
# Save
out_file.write("%d %.6f %.6f %.6f %.6f\n" %
(int(class_index-1), x_center, y_center, width, height))
# print(f'Successfully Created {txt_name}')
# YOLOv7
def get_BBoxYOLOv7(img, yolo_model, detect_conf, class_name_list, remove_list, keep_list):
# Load YOLOv7 model on Image
results = yolo_model(img)
# Bounding Box
box = results.pandas().xyxy[0]
bbox_list = []
confidence = []
class_ids = []
# Class
class_list = box['class'].tolist()
# save_yolo function need class index starting from 1 NOT Zero
new_list = [x+1 for x in class_list]
for i, id in zip(box.index, new_list):
xmin, ymin, xmax, ymax, conf = int(box['xmin'][i]), int(box['ymin'][i]), int(box['xmax'][i]), \
int(box['ymax'][i]), box['confidence'][i]
# detect_conf
if conf > detect_conf:
# Remove specfic classes from Annotation
if len(remove_list)>0:
if not remove_class(class_name_list, int(id-1), remove_list):
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(id)
# Confidence
confidence.append(conf)
# Keep specfic classes from Annotation
elif len(keep_list)>0:
if remove_class(class_name_list, int(id-1), keep_list):
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(id)
# Confidence
confidence.append(conf)
# All classes
else:
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(id)
# Confidence
confidence.append(conf)
return bbox_list, class_ids, confidence
# YOLOv8
def get_BBoxYOLOv8(img, yolo_model, detect_conf, class_name_list, remove_list, keep_list):
bbox_list = []
confidence = []
class_ids = []
# Load YOLOv8 model on Image
results = yolo_model(img)
for result in results:
bboxs = result.boxes.xyxy
conf = result.boxes.conf
cls = result.boxes.cls
for bbox, cnf, cs in zip(bboxs, conf, cls):
xmin = int(bbox[0])
ymin = int(bbox[1])
xmax = int(bbox[2])
ymax = int(bbox[3])
# detect_conf
if cnf > detect_conf:
# Remove specfic classes from Annotation
if len(remove_list)>0:
if not remove_class(class_name_list, int(cs), remove_list):
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
# Keep specfic classes from Annotation
elif len(keep_list)>0:
if remove_class(class_name_list, int(cs), keep_list):
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
# All classes
else:
# BBox
bbox_list.append([xmin, ymin, xmax, ymax])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
return bbox_list, class_ids, confidence
# YOLOv8
def get_BBoxYOLONAS(img, yolo_model, detect_conf, remove_list, keep_list):
bbox_list = []
confidence = []
class_ids = []
# Load YOLOv8 model on Image
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
preds = next(yolo_model.predict(img_rgb)._images_prediction_lst)
class_name_list = preds.class_names
dp = preds.prediction
bboxes, confs, labels = np.array(dp.bboxes_xyxy), dp.confidence, dp.labels.astype(int)
for box, cnf, cs in zip(bboxes, confs, labels):
# detect_conf
if cnf > detect_conf:
# Remove specfic classes from Annotation
if len(remove_list)>0:
if not remove_class(class_name_list, int(cs), remove_list):
# BBox
bbox_list.append(box[:4])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
# Keep specfic classes from Annotation
elif len(keep_list)>0:
if remove_class(class_name_list, int(cs), keep_list):
# BBox
bbox_list.append(box[:4])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
# All classes
else:
# BBox
bbox_list.append(box[:4])
# class
class_ids.append(int(cs+1))
# Confidence
confidence.append(cnf)
return bbox_list, class_ids, confidence, class_name_list
# Convert CXCYWH to XYXY
def box_cxcywh_to_xyxy(boxes: Tensor) -> Tensor:
"""
Converts bounding boxes from (cx, cy, w, h) format to (x1, y1, x2, y2) format.
(cx, cy) refers to center of bounding box
(w, h) are width and height of bounding box
Args:
boxes (Tensor[N, 4]): boxes in (cx, cy, w, h) format which will be converted.
Returns:
boxes (Tensor(N, 4)): boxes in (x1, y1, x2, y2) format.
"""
# We need to change all 4 of them so some temporary variable is needed.
cx, cy, w, h = boxes.unbind(-1)
x1 = cx - 0.5 * w
y1 = cy - 0.5 * h
x2 = cx + 0.5 * w
y2 = cy + 0.5 * h
boxes = torch.stack((x1, y1, x2, y2), dim=-1)
return boxes