-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
94 lines (73 loc) · 2.74 KB
/
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
import os
import json
def get_categories(ann_path):
if ann_path == None:
return None
categories_temp = []
categories = []
categories_to_id = {}
for i in range(len(os.listdir(ann_path))):
with open(ann_path.rstrip("/") + "/" + str(i+1) + ".json") as file:
data = json.load(file)
for obj in data['regions']:
category = obj['tags'][0]
if category not in categories_temp:
categories_temp.append(category)
for category in categories_temp:
categories.append(
{
"supercategory": category,
"id": categories_temp.index(category) + 1,
"name": category
}
)
categories_to_id[category] = categories_temp.index(category) + 1
return categories, categories_to_id
def convert(ann_path, categories_to_id, output_dir, train_or_test):
if ann_path == None:
return None
images = []
annotations = []
obj_id = 1
for i in range(len(os.listdir(ann_path))):
with open(ann_path + str(i+1) + ".json") as file:
data = json.load(file)
image_path = output_dir.rstrip("/") + "/" + train_or_test + "/" + data['asset']['name']
image_id = i+1
image_width = data['asset']['size']['width']
image_height = data['asset']['size']['height']
images.append(
{
"height": image_height,
"width": image_width,
"id": image_id,
"file_name": image_path
}
)
for obj in data['regions']:
category = obj['tags'][0]
bbox = obj['boundingBox']
annotations.append(
{
"iscrowd": 0,
"image_id": image_id,
"bbox": [
float(bbox['left']),
float(bbox['top']),
float(bbox['width']),
float(bbox['height'])
],
"category_id": categories_to_id[category],
"id": obj_id,
"area": image_height*image_width
}
)
obj_id += 1
return images, annotations
def create_coco(file_name, images, annotations, categories):
result_data = {}
result_data['images'] = images
result_data['annotations'] = annotations
result_data['categories'] = categories
with open(file_name, 'w') as outfile:
json.dump(result_data, outfile, indent=4)