-
Notifications
You must be signed in to change notification settings - Fork 25
/
coco_style_convert.py
133 lines (105 loc) · 4.28 KB
/
coco_style_convert.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
# -*- coding: utf-8 -*-
# ---------------------
import json
import sys
import click
import imageio
import numpy as np
from path import Path
from joint import Joint
from pose import Pose
MAX_COLORS = 42
# check python version
assert sys.version_info >= (3, 6), '[!] This script requires Python >= 3.6'
def get_pose(frame_data, person_id):
# type: (np.ndarray, int) -> Pose
"""
:param frame_data: data of the current frame
:param person_id: person identifier
:return: list of joints in the current frame with the required person ID
"""
pose = [Joint(j) for j in frame_data[frame_data[:, 1] == person_id]]
pose.sort(key=(lambda j: j.type))
return Pose(pose)
H1 = 'path of the output directory'
@click.command()
@click.option('--out_dir_path', type=click.Path(), prompt='Enter \'out_dir_path\'', help=H1)
def main(out_dir_path):
# type: (str) -> None
"""
Script for annotation conversion (from JTA format to COCO format)
"""
out_dir_path = Path(out_dir_path).abspath()
if not out_dir_path.exists():
out_dir_path.makedirs()
for dir in Path('annotations').dirs():
out_subdir_path = out_dir_path / dir.basename()
if not out_subdir_path.exists():
out_subdir_path.makedirs()
print(f'▸ converting \'{dir.basename()}\' set')
for anno in dir.files():
with open(anno, 'r') as json_file:
data = json.load(json_file)
data = np.array(data)
print(f'▸ converting annotations of \'{Path(anno).abspath()}\'')
# getting sequence number from `anno`
sequence = None
try:
sequence = int(Path(anno).basename().split('_')[1].split('.')[0])
except:
print('[!] error during conversion.')
print('\ttry using JSON files with the original nomenclature.')
coco_dict = {
'info': {
'description': f'JTA 2018 Dataset - Sequence #{sequence}',
'url': 'http://aimagelab.ing.unimore.it/jta',
'version': '1.0',
'year': 2018,
'contributor': 'AImage Lab',
'date_created': '2018/01/28',
},
'licences': [{
'url': 'http://creativecommons.org/licenses/by-nc/2.0',
'id': 2,
'name': 'Attribution-NonCommercial License'
}],
'images': [],
'annotations': [],
'categories': [{
'supercategory': 'person',
'id': 1,
'name': 'person',
'keypoints': Joint.NAMES,
'skeleton': Pose.SKELETON
}]
}
for frame_number in range(0, 900):
image_id = sequence * 1000 + (frame_number + 1)
coco_dict['images'].append({
'license': 4,
'file_name': f'{frame_number + 1}.jpg',
'height': 1080,
'width': 1920,
'date_captured': '2018-01-28 00:00:00',
'id': image_id
})
# NOTE: frame #0 does NOT exists: first frame is #1
frame_data = data[data[:, 0] == frame_number + 1] # type: np.ndarray
for p_id in set(frame_data[:, 1]):
pose = get_pose(frame_data=frame_data, person_id=p_id)
# ignore the "invisible" poses
# (invisible pose = pose of which I do not see any joint)
if pose.invisible:
continue
annotation = pose.coco_annotation
annotation['image_id'] = image_id
annotation['id'] = image_id * 100000 + int(p_id)
annotation['category_id'] = 1
coco_dict['annotations'].append(annotation)
print(f'\r▸ progress: {100 * (frame_number / 899):6.2f}%', end='')
print()
out_file_path = out_subdir_path / f'seq_{sequence}.coco.json'
with open(out_file_path, 'w') as f:
json.dump(coco_dict, f)
if __name__ == '__main__':
main()