-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.py
67 lines (60 loc) · 2.25 KB
/
encoding.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
import json, numpy as np, pandas as pd
from tqdm.auto import tqdm
class NPEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NPEncoder, self).default(obj)
def jsonfy(data):
data.insert(loc=5, column='target', value=None)
data.loc[:, 'target'] = data['file name'].apply(lambda x: x.split('/')[0])
image_ids = data['file name'].unique()
image_dict = dict(zip(image_ids, range(len(image_ids))))
json_dict = {'images': list(), 'type': 'instances', 'annotations': list(), 'categories': list()}
# build the image list
for image_id in image_ids:
image = {'file_name': image_id + '.png', 'height': 400, 'width': 712, 'id': image_dict[image_id]}
json_dict['images'].append(image)
categories = [
{'super category': 'face', 'id': 1, 'name': 'anger'},
{'super category': 'face', 'id': 2, 'name': 'disgust'},
{'super category': 'face', 'id': 3, 'name': 'fear'},
{'super category': 'face', 'id': 4, 'name': 'happy'},
{'super category': 'face', 'id': 5, 'name': 'neutral'},
{'super category': 'face', 'id': 6, 'name': 'sad'},
{'super category': 'face', 'id': 7, 'name': 'surprise'}
]
json_dict['categories'].extend(categories)
category_map = {'anger': 1, 'disgust': 2, 'fear': 3, 'happy': 4, 'neutral': 5, 'sad': 6, 'surprise': 7}
# build the annotation list
for idx, row in tqdm(data.iterrows()):
image_id = image_dict[row['file name']]
category = row['target']
annotation = {
'area': row['width'] * row['height'],
'iscrowd': 0,
'image_id': image_id,
'bbox': [row['x'], row['y'], row['width'], row['height']],
'category_id': category_map[category],
'id': idx,
'segmentation': None
}
json_dict['annotations'].append(annotation)
return json_dict
train = pd.read_csv('bounding box train')
test = pd.read_csv('bounding box test')
result = jsonfy(train)
file = open('annotations train.json', 'w', encoding='utf-8')
json_str = json.dumps(result, cls=NPEncoder)
file.write(json_str)
file.close()
result = jsonfy(test)
file = open('annotations test.json', 'w', encoding='utf-8')
json_str = json.dumps(result, cls=NPEncoder)
file.write(json_str)
file.close()