File size: 5,611 Bytes
d3dbf03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import json
import os

import numpy as np


def parse_args():
    parser = argparse.ArgumentParser(
        description='Convert and merge hand pose dataset to COCO style')
    parser.add_argument(
        '--data_root',
        type=str,
        default='./data/',
        help='the root to all involved datasets')
    parser.add_argument(
        '--out_anno_prefix',
        type=str,
        default='hand_det',
        help='the prefix of output annotation files')

    args = parser.parse_args()
    return args


def get_data_root(path):
    path = path.split('/')
    index = path.index('annotations') - 1
    root = path[index]
    if root == 'halpe':
        root = 'halpe/hico_20160224_det/images/train2015/'
    return root


def parse_coco_style(file_path, anno_idx=0):
    with open(file_path) as f:
        contents = json.load(f)

    data_root = get_data_root(file_path) + '/'
    images = contents['images']
    annos = contents['annotations']
    images_out, annos_out = [], []
    for img, anno in zip(images, annos):
        assert img['id'] == anno['image_id']
        img_out = dict(
            file_name=data_root + img['file_name'],
            height=img['height'],
            width=img['width'],
            id=anno_idx)
        anno_out = dict(
            area=anno['area'],
            iscrowd=anno['iscrowd'],
            image_id=anno_idx,
            bbox=anno['bbox'],
            category_id=0,
            id=anno_idx)
        anno_idx += 1
        images_out.append(img_out)
        annos_out.append(anno_out)
    return images_out, annos_out, anno_idx


def parse_halpe(file_path, anno_idx):

    def get_bbox(keypoints):
        """Get bbox from keypoints."""
        if len(keypoints) == 0:
            return [0, 0, 0, 0]
        x1, y1, _ = np.amin(keypoints, axis=0)
        x2, y2, _ = np.amax(keypoints, axis=0)
        w, h = x2 - x1, y2 - y1
        return [x1, y1, w, h]

    with open(file_path) as f:
        contents = json.load(f)

    data_root = get_data_root(file_path) + '/'
    images = contents['images']
    annos = contents['annotations']
    images_out, annos_out = [], []
    for img, anno in zip(images, annos):
        assert img['id'] == anno['image_id']
        keypoints = np.array(anno['keypoints']).reshape(-1, 3)
        lefthand_kpts = keypoints[-42:-21, :]
        righthand_kpts = keypoints[-21:, :]

        left_mask = lefthand_kpts[:, 2] > 0
        right_mask = righthand_kpts[:, 2] > 0
        lefthand_box = get_bbox(lefthand_kpts[left_mask])
        righthand_box = get_bbox(righthand_kpts[right_mask])

        if max(lefthand_box) > 0:
            img_out = dict(
                file_name=data_root + img['file_name'],
                height=img['height'],
                width=img['width'],
                id=anno_idx)
            anno_out = dict(
                area=lefthand_box[2] * lefthand_box[3],
                iscrowd=anno['iscrowd'],
                image_id=anno_idx,
                bbox=lefthand_box,
                category_id=0,
                id=anno_idx)
            anno_idx += 1
            images_out.append(img_out)
            annos_out.append(anno_out)

        if max(righthand_box) > 0:
            img_out = dict(
                file_name=data_root + img['file_name'],
                height=img['height'],
                width=img['width'],
                id=anno_idx)
            anno_out = dict(
                area=righthand_box[2] * righthand_box[3],
                iscrowd=anno['iscrowd'],
                image_id=anno_idx,
                bbox=righthand_box,
                category_id=0,
                id=anno_idx)
            anno_idx += 1
            images_out.append(img_out)
            annos_out.append(anno_out)
    return images_out, annos_out, anno_idx


train_files = [
    'freihand/annotations/freihand_train.json',
    'halpe/annotations/halpe_train_v1.json',
    'onehand10k/annotations/onehand10k_train.json',
    '/rhd/annotations/rhd_train.json'
]

val_files = ['onehand10k/annotations/onehand10k_test.json']


def convert2dict(data_root, anno_files):
    anno_files = [data_root + _ for _ in anno_files]

    images, annos, anno_idx = [], [], 0
    for anno_file in anno_files:
        if 'freihand' in anno_file or 'onehand10k' in anno_file \
                                   or 'rhd' in anno_file:
            images_out, annos_out, anno_idx = parse_coco_style(
                anno_file, anno_idx)
            images += images_out
            annos += annos_out
        elif 'halpe' in anno_file:
            images_out, annos_out, anno_idx = parse_halpe(anno_file, anno_idx)
            images += images_out
            annos += annos_out
        else:
            print(f'{anno_file} not supported')

    result = dict(
        images=images,
        annotations=annos,
        categories=[{
            'id': 0,
            'name': 'hand'
        }])
    return result


if __name__ == '__main__':
    args = parse_args()
    data_root = args.data_root + '/'
    prefix = args.out_anno_prefix
    os.makedirs('hand_det', exist_ok=True)

    result = convert2dict(data_root, train_files)
    with open(f'hand_det/{prefix}_train.json', 'w') as f:
        json.dump(result, f)

    result = convert2dict(data_root, val_files)
    with open(f'hand_det/{prefix}_val.json', 'w') as f:
        json.dump(result, f)