-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_json_tokyo.py
93 lines (77 loc) · 2.52 KB
/
make_json_tokyo.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
import glob
import json
dataset_path = '/home/rauf/datasets/ir/tokyo/'
annotations_folder = dataset_path + 'labels/'
cameras = ['fir', 'mir', 'nir']
img_sizes = {'fir': {'w': 640, 'h': 480},
'mir': {'w': 320, 'h': 256},
'nir': {'w': 320, 'h': 256}}
images = []
annotations = []
licenses = []
json_coco_format = {}
counter_img = 0
counter_ann = 0
for cam in cameras:
for filepath in glob.iglob('{}/*.txt'.format(annotations_folder + cam + '/')):
with open(filepath) as fp:
lines = fp.readlines()
file_nm = filepath.split('/')[-1].split('.')[0]
image = {
"file_name": cam + '/' + file_nm + '.png',
"height": img_sizes[cam]['h'],
"width": img_sizes[cam]['w'],
"id": counter_img
}
images.append(image)
for line in lines:
values = line.split()
class_id = values[0]
x_coord = float(values[1]) * img_sizes[cam]['w']
y_coord = float(values[2]) * img_sizes[cam]['h']
w_coord = float(values[3]) * img_sizes[cam]['w']
h_coord = float(values[4]) * img_sizes[cam]['h']
x = x_coord - w_coord/2
y = y_coord - h_coord/2
w = w_coord
h = h_coord
cl_id = 0
if int(class_id) == 0:
cl_id = 1
elif int(class_id) == 1:
cl_id = 2
else:
continue
annotation = {
'image_id': counter_img,
'category_id': cl_id,
'id': counter_ann,
'bbox': [x, y, w, h],
'area': w * h
}
annotations.append(annotation)
counter_ann += 1
counter_img += 1
info = {
"description": "Tokyo Dataset",
"version": "1.0",
"year": 2019
}
licenses = [
{
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
"id": 1,
"name": "Attribution-NonCommercial-ShareAlike License"
}
]
categories = [
{"id": 1, "name": 'person'},
{"id": 2, "name": 'car'}
]
json_coco_format['info'] = info
json_coco_format['images'] = images
json_coco_format['annotations'] = annotations
json_coco_format['licenses'] = licenses
json_coco_format['categories'] = categories
with open('train_tokyo.json', 'w+') as outfile:
json.dump(json_coco_format, outfile)