-
Notifications
You must be signed in to change notification settings - Fork 23
/
panda_utils.py
501 lines (451 loc) · 19.7 KB
/
panda_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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# --------------------------------------------------------
# Basic functions which are useful for process PANDA data
# Written by Wang Xueyang ([email protected]), Version 20200321
# Inspired from DOTA dataset devkit (https://github.com/CAPTAIN-WHU/DOTA_devkit)
# --------------------------------------------------------
import os
import json
import glob
import random
CATEGORY = {
'visible body': 1,
'full body': 2,
'head': 3,
'vehicle': 4
}
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0])
def GetFileFromThisRootDir(dir, ext=None):
allfiles = []
needExtFilter = (ext != None)
for root, dirs, files in os.walk(dir):
for filespath in files:
filepath = os.path.join(root, filespath)
extension = os.path.splitext(filepath)[1][1:]
if needExtFilter and extension in ext:
allfiles.append(filepath)
elif not needExtFilter:
allfiles.append(filepath)
return allfiles
def restrain_between_0_1(values_list):
return_list = []
for value in values_list:
if value < 0:
new_value = 0
elif value > 1:
new_value = 1
else:
new_value = value
return_list.append(new_value)
return return_list
def RectDict2List(rectdict, imgwidth, imgheight, scale, mode='tlbr'):
x1, y1, x2, y2 = restrain_between_0_1([rectdict['tl']['x'], rectdict['tl']['y'],
rectdict['br']['x'], rectdict['br']['y']])
xmin = int(x1 * imgwidth * scale)
ymin = int(y1 * imgheight * scale)
xmax = int(x2 * imgwidth * scale)
ymax = int(y2 * imgheight * scale)
if mode == 'tlbr':
return xmin, ymin, xmax, ymax
elif mode == 'tlwh':
return xmin, ymin, xmax - xmin, ymax - ymin
def List2RectDict(bbox, imgwidth, imgheight, scale, mode='tlwh'):
if mode == 'tlbr':
xmin, ymin, xmax, ymax = bbox
elif mode == 'tlwh':
xmin, ymin, w, h = bbox
xmax, ymax = xmin + w, ymin + h
rectdict = {
'tl': {
'x': xmin / imgwidth / scale,
'y': ymin / imgheight / scale
},
'br': {
'x': xmax / imgwidth / scale,
'y': ymax / imgheight / scale
}
}
return rectdict
def parse_panda_rect(annopath, annomode, showwidth):
"""
parse the panda ground truth for visualization
"""
images = {}
print('Loading annotation json file: {}'.format(annopath))
with open(annopath, 'r') as load_f:
annodict = json.load(load_f)
if annomode == 'person':
for (imagename, imagedict) in annodict.items():
parsed = []
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
scale = showwidth / imgwidth
for object_dict in imagedict['objects list']:
objcate = object_dict['category']
if objcate == 'person':
personpose = object_dict['riding type'] if object_dict['pose'] == 'riding' else object_dict['pose']
fullrect = RectDict2List(object_dict['rects']['full body'], imgwidth, imgheight, scale)
visiblerect = RectDict2List(object_dict['rects']['visible body'], imgwidth, imgheight, scale)
headrect = RectDict2List(object_dict['rects']['head'], imgwidth, imgheight, scale)
parsed.append({
'ignore': False,
'cate': personpose,
'fullrect': fullrect,
'visiblerect': visiblerect,
'headrect': headrect
})
else:
rect = RectDict2List(object_dict['rect'], imgwidth, imgheight, scale)
parsed.append({
'ignore': True,
'cate': objcate,
'rect': rect,
})
images[imagename] = parsed
elif annomode == 'vehicle':
for (imagename, imagedict) in annodict.items():
parsed = []
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
scale = showwidth / imgwidth
for object_dict in imagedict['objects list']:
objcate = object_dict['category']
rect = RectDict2List(object_dict['rect'], imgwidth, imgheight, scale)
if objcate == 'vehicles':
parsed.append({
'ignore': True,
'cate': objcate,
'rect': rect,
})
else:
parsed.append({
'ignore': False,
'cate': objcate,
'rect': rect,
})
images[imagename] = parsed
elif annomode == 'headbbox':
for (imagename, imagedict) in annodict.items():
parsed = []
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
scale = showwidth / imgwidth
for object_dict in imagedict['objects list']:
rect = RectDict2List(object_dict['rect'], imgwidth, imgheight, scale)
parsed.append(rect)
images[imagename] = parsed
elif annomode == 'headpoint':
for (imagename, imagedict) in annodict.items():
parsed = []
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
scale = showwidth / imgwidth
for object_dict in imagedict['objects list']:
x = int(object_dict['rect']['x'] * imgwidth * scale)
y = int(object_dict['rect']['y'] * imgheight * scale)
parsed.append((x, y))
images[imagename] = parsed
return images
def GT2DetRes(gtpath, outdetpath):
"""
transfer format: groundtruth to detection results
:param gtpath: the path to groundtruth json file
:param outdetpath:the path to output detection result json file
:return:
"""
print('Loading groundtruth json file: {}'.format(gtpath))
with open(gtpath, 'r') as load_f:
gt = json.load(load_f)
outputlist = []
for (imgname, imgdict) in gt.items():
imageid = imgdict['image id']
imgwidth = imgdict['image size']['width']
imgheight = imgdict['image size']['height']
for obj in imgdict['objects list']:
if obj['category'] == 'person':
rect = RectDict2List(obj['rects']['visible body'], imgwidth, imgheight, 1, mode='tlwh')
else:
rect = RectDict2List(obj['rect'], imgwidth, imgheight, 1, mode='tlwh')
outputlist.append({
"image_id": imageid,
"category_id": 1,
"bbox": rect,
"score": 1
})
with open(outdetpath, 'w', encoding='utf-8') as f:
dict_str = json.dumps(outputlist, indent=2)
f.write(dict_str)
def DetRes2GT(detrespath, outgtpath, gtannopath):
"""
transfer format: detection results to groundtruth
:param detrespath: the path to input detection result json file
:param outgtpath: the path to output groundtruth json file
:param gtannopath: source annotation json file path for image data
:return:
"""
print('Loading source groundtruth json file: {}'.format(gtannopath))
with open(gtannopath, 'r') as load_f:
gtanno = json.load(load_f)
print('Loading detection result json file: {}'.format(detrespath))
with open(detrespath, 'r') as load_f:
detres = json.load(load_f)
outgt = {}
for (imgname, imgdict) in gtanno.items():
outgt[imgname] = {
"image id": imgdict['image id'],
"image size": imgdict['image size'],
"objects list": []
}
for detdict in detres:
imageid = detdict["image_id"]
bbox = detdict["bbox"]
for imgname in outgt.keys():
if outgt[imgname]['image id'] == imageid:
outgt[imgname]['objects list'].append({
"category": "person",
"rect": List2RectDict(bbox, outgt[imgname]['image size']['width'],
outgt[imgname]['image size']['height'], 1)
})
with open(outgtpath, 'w', encoding='utf-8') as f:
dict_str = json.dumps(outgt, indent=2)
f.write(dict_str)
def generate_coco_anno(personsrcfile, vehiclesrcfile, tgtfile, keywords=None):
"""
transfer ground truth to COCO format
:param personsrcfile: person ground truth file path
:param vehiclesrcfile: vehicle ground truth file path
:param tgtfile: generated file save path
:param keywords: list of str, only keep image with keyword in image name
:return:
"""
attrDict = dict()
attrDict["categories"] = [
{"supercategory": "none", "id": 1, "name": 'visible body'},
{"supercategory": "none", "id": 2, "name": 'full body'},
{"supercategory": "none", "id": 3, "name": 'head'},
{"supercategory": "none", "id": 4, "name": 'vehicle'}
]
with open(personsrcfile, 'r') as load_f:
person_anno_dict = json.load(load_f)
with open(vehiclesrcfile, 'r') as load_f:
vehicle_anno_dict = json.load(load_f)
images = list()
annotations = list()
imageids = list()
objid = 1
for (imagename, imagedict) in person_anno_dict.items():
if keywords:
flag = False
for kw in keywords:
if kw in imagename:
flag = True
if not flag:
continue
image = dict()
image['file_name'] = imagename
imgid = imagedict['image id']
imageids.append(imgid)
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
image['height'] = imgheight
image['width'] = imgwidth
image['id'] = imgid
images.append(image)
for objdict in imagedict['objects list']:
cate = objdict['category']
if cate == 'person':
for label in ['visible body', 'full body', 'head']:
rect = objdict['rects'][label]
annotation = dict()
annotation["image_id"] = imgid
annotation["ignore"] = 0
annotation["iscrowd"] = 0
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["area"] = float(w * h)
annotation["category_id"] = CATEGORY[label]
annotation["id"] = objid
objid += 1
annotation["segmentation"] = [[x, y, x, (y + h), (x + w), (y + h), (x + w), y]]
annotations.append(annotation)
else:
annotation = dict()
if cate == 'crowd':
annotation["iscrowd"] = 1
else:
annotation["iscrowd"] = 0
rect = objdict['rect']
annotation["image_id"] = imgid
annotation["ignore"] = 1
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["area"] = float(w * h)
annotation["category_id"] = CATEGORY['visible body']
annotation["id"] = objid
objid += 1
annotation["segmentation"] = [[x, y, x, (y + h), (x + w), (y + h), (x + w), y]]
annotations.append(annotation)
for objdict in vehicle_anno_dict[imagename]['objects list']:
cate = objdict['category']
if cate == 'vehicles':
annotation = dict()
rect = objdict['rect']
annotation["image_id"] = imgid
annotation["iscrowd"] = 1
annotation["ignore"] = 1
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["area"] = float(w * h)
annotation["category_id"] = CATEGORY['vehicle']
annotation["id"] = objid
objid += 1
annotation["segmentation"] = [[x, y, x, (y + h), (x + w), (y + h), (x + w), y]]
annotations.append(annotation)
else:
annotation = dict()
rect = objdict['rect']
annotation["image_id"] = imgid
annotation["ignore"] = 0
annotation["iscrowd"] = 0
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["area"] = float(w * h)
annotation["category_id"] = CATEGORY['vehicle']
annotation["id"] = objid
objid += 1
annotation["segmentation"] = [[x, y, x, (y + h), (x + w), (y + h), (x + w), y]]
annotations.append(annotation)
attrDict["images"] = images
attrDict["annotations"] = annotations
attrDict["type"] = "instances"
# print attrDict
jsonString = json.dumps(attrDict, indent=2)
with open(tgtfile, "w") as f:
f.write(jsonString)
return imageids
def generate_res_from_gt(personsrcfile, vehiclesrcfile, resFile, keywords=None):
with open(personsrcfile, 'r') as load_f:
person_anno_dict = json.load(load_f)
with open(vehiclesrcfile, 'r') as load_f:
vehicle_anno_dict = json.load(load_f)
annotations = list()
for (imagename, imagedict) in person_anno_dict.items():
if keywords:
flag = False
for kw in keywords:
if kw in imagename:
flag = True
if not flag:
continue
imgid = imagedict['image id']
imgwidth = imagedict['image size']['width']
imgheight = imagedict['image size']['height']
for objdict in imagedict['objects list']:
cate = objdict['category']
if cate == 'person':
for label in ['visible body', 'full body', 'head']:
rect = objdict['rects'][label]
annotation = dict()
annotation["image_id"] = imgid
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["category_id"] = CATEGORY[label]
annotation["score"] = 0.999
annotations.append(annotation)
for objdict in vehicle_anno_dict[imagename]['objects list']:
cate = objdict['category']
if cate != 'vehicles':
rect = objdict['rect']
annotation = dict()
annotation["image_id"] = imgid
x, y, w, h = RectDict2List(rect, imgwidth, imgheight, scale=1, mode='tlwh')
annotation["bbox"] = [x, y, w, h]
annotation["category_id"] = CATEGORY['vehicle']
annotation["score"] = 0.999
annotations.append(annotation)
# print attrDict
jsonString = json.dumps(annotations, indent=2)
with open(resFile, "w") as f:
f.write(jsonString)
def generate_mot_anno(srcdir, tgtdir):
"""
transfer ground truth to MOTChallenge format
:param srcdir: root directory to source gt json file
:param tgtdir: target directory
:return:
"""
print('transferring file format.')
if not os.path.exists(tgtdir):
os.makedirs(tgtdir)
gtdirs = glob.glob(os.path.join(srcdir, '*/'))
for gtdir in gtdirs:
tracksfile = os.path.join(gtdir, 'tracks.json')
seqinfofile = os.path.join(gtdir, 'seqinfo.json')
if not os.path.exists(tracksfile):
continue
with open(tracksfile, 'r') as load_f:
tracks_list = json.load(load_f)
with open(seqinfofile, 'r') as load_f:
seqinfo_dict = json.load(load_f)
seqname = seqinfo_dict["name"]
width = seqinfo_dict["imWidth"]
height = seqinfo_dict["imHeight"]
with open(os.path.join(tgtdir, seqname + '.txt'), 'w') as f:
for track_dict in tracks_list:
track_id = track_dict["track id"]
for frame_dict in track_dict["frames"]:
frame_id = frame_dict["frame id"]
rect = frame_dict["rect"]
occ = frame_dict["occlusion"]
x, y, w, h = RectDict2List(rect, width, height, scale=1, mode='tlwh')
if occ == 'normal':
visible_ratio = 1
elif occ == 'hide':
visible_ratio = 0.66667
elif occ == 'serious_hide':
visible_ratio = 0.33333
else:
visible_ratio = 0
# save MOT file:
f.writelines([str(frame_id), ',', str(track_id), ',', str(x), ',', str(y), ',', str(w), ',',
str(h), ',', '1', ',', '1', ',', str(visible_ratio), '\n'])
def generate_mot_res(srcdir, tgtdir):
"""
generate results in MOTChallenge format from ground truth
:param srcdir: root directory to source gt json file
:param tgtdir: target directory
:return:
"""
print('generating results from gt.')
if not os.path.exists(tgtdir):
os.makedirs(tgtdir)
gtdirs = glob.glob(os.path.join(srcdir, '*/'))
for gtdir in gtdirs:
tracksfile = os.path.join(gtdir, 'tracks.json')
seqinfofile = os.path.join(gtdir, 'seqinfo.json')
if not os.path.exists(tracksfile):
continue
with open(tracksfile, 'r') as load_f:
tracks_list = json.load(load_f)
with open(seqinfofile, 'r') as load_f:
seqinfo_dict = json.load(load_f)
seqname = seqinfo_dict["name"]
width = seqinfo_dict["imWidth"]
height = seqinfo_dict["imHeight"]
seqLength = seqinfo_dict['seqLength']
with open(os.path.join(tgtdir, seqname + '.txt'), 'w') as f:
for i in range(seqLength):
frame_id = i + 1
for track_dict in tracks_list:
track_id = track_dict["track id"]
for frame_dict in track_dict["frames"]:
if frame_id == frame_dict["frame id"]:
rect = frame_dict["rect"]
x, y, w, h = RectDict2List(rect, width, height, scale=1, mode='tlwh')
x += random.gauss(0, 10)
y += random.gauss(0, 10)
w += random.gauss(0, 5)
h += random.gauss(0, 5)
# save MOT result file:
f.writelines([str(frame_id), ',', str(track_id), ',', str(x), ',', str(y), ',', str(w), ',',
str(h), ',', '1', ',', '-1', ',', '-1', ',', '-1', '\n'])