-
Notifications
You must be signed in to change notification settings - Fork 34
/
coco_utils.py
145 lines (132 loc) · 5.7 KB
/
coco_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
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
'''
File: coco_utils.py
Project: MobilePose
File Created: Saturday, 3rd March 2018 7:04:57 pm
Author: Yuliang Xiu (yuliangxiu@sjtu.edu.cn)
-----
Last Modified: Thursday, 8th March 2018 3:02:15 pm
Modified By: Yuliang Xiu (yuliangxiu@sjtu.edu.cn>)
-----
Copyright 2018 - 2018 Shanghai Jiao Tong University, Machine Vision and Intelligence Group
'''
# define coco class
import json
import numpy as np
from collections import namedtuple, Mapping
# Create namedtuple without defaults
def namedtuple_with_defaults(typename, field_names, default_values=()):
T = namedtuple(typename, field_names)
T.__new__.__defaults__ = (None,) * len(T._fields)
if isinstance(default_values, Mapping):
prototype = T(**default_values)
else:
prototype = T(*default_values)
T.__new__.__defaults__ = tuple(prototype)
return T
# Used for solving TypeError: Object of type 'float32' is not JSON serializable
class MyEncoder(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(MyEncoder, self).default(obj)
# Classes for coco groud truth, CocoImage and CocoAnnotation
CocoImage = namedtuple_with_defaults('image', ['file_name', 'height', 'width', 'id'])
CocoAnnotation = namedtuple_with_defaults('annotation', ['num_keypoints', 'area',
'iscrowd', 'keypoints',
'image_id', 'bbox', 'category_id',
'id'])
class CocoData:
def __init__(self, coco_images_arr, coco_annotations_arr):
self.Coco = {}
coco_images_arr = [item._asdict() for item in coco_images_arr]
coco_annotations_arr = [item._asdict() for item in coco_annotations_arr]
self.Coco['images'] = coco_images_arr
self.Coco['annotations'] = coco_annotations_arr
self.Coco['categories'] = [{"id": 1, "name": "test"}]
def dumps(self):
return json.dumps(self.Coco, cls=MyEncoder)
# Change keypoints [x, y, prob] prob = int(prob)
def float2int(str_data):
json_data = json.loads(str_data)
annotations = []
if 'annotations' in json_data:
annotations = json_data['annotations']
else:
annotations = json_data
json_size = len(annotations)
for i in range(json_size):
annotation = annotations[i]
keypoints = annotation['keypoints']
keypoints_num = int(len(keypoints) / 3)
for j in range(keypoints_num):
keypoints[j * 3 + 2] = int(round(keypoints[j * 3 + 2]))
return json.dumps(json_data)
# Append coco ground truth to coco_images_arr and coco_annotations_arr
def transform_to_coco_gt(datas, coco_images_arr, coco_annotations_arr):
"""
data: num_samples * 32, type Tensor
16 keypoints
output:
inside coco_images_arr, coco_annotations_arr
"""
for idx, sample in enumerate(datas):
coco_image = CocoImage()
coco_annotation = CocoAnnotation()
sample = np.array(sample.numpy()).reshape(-1, 2)
num_keypoints = len(sample)
keypoints = np.append(sample, np.array(np.ones(num_keypoints).reshape(-1, 1) * 2),
axis=1)
xmin = np.min(sample[:,0])
ymin = np.min(sample[:,1])
xmax = np.max(sample[:,0])
ymax = np.max(sample[:,1])
width = ymax - ymin
height = xmax - xmin
coco_image = coco_image._replace(id = idx, width=width, height=height, file_name="")
coco_annotation = coco_annotation._replace(num_keypoints=num_keypoints)
coco_annotation = coco_annotation._replace(area=width*height)
coco_annotation = coco_annotation._replace(keypoints=keypoints.reshape(-1))
coco_annotation = coco_annotation._replace(image_id=idx)
coco_annotation = coco_annotation._replace(bbox=[xmin, ymin, width, height])
coco_annotation = coco_annotation._replace(category_id=1) # default "1" for keypoint
coco_annotation = coco_annotation._replace(id=idx)
coco_annotation = coco_annotation._replace(iscrowd=0)
coco_images_arr.append(coco_image)
coco_annotations_arr.append(coco_annotation)
return ()
# Coco predict result class
CocoPredictAnnotation = namedtuple_with_defaults('predict_anno', ['image_id', 'category_id', 'keypoints', 'score'])
# Append coco predict result to coco_images_arr and coco_pred_annotations_arr
def transform_to_coco_pred(datas, coco_pred_annotations_arr, beg_idx):
"""
data: num_samples * 32, type Variable
16 keypoints
output:
inside coco_pred_annotations_arr
"""
for idx, sample in enumerate(datas):
coco_pred_annotation = CocoPredictAnnotation()
sample = np.array(sample.data.cpu().numpy()).reshape(-1, 2)
num_keypoints = len(sample)
keypoints = np.append(sample, np.array(np.ones(num_keypoints).reshape(-1, 1) * 2),
axis=1)
xmin = np.min(sample[:,0])
ymin = np.min(sample[:,1])
xmax = np.max(sample[:,0])
ymax = np.max(sample[:,1])
width = ymax - ymin
height = xmax - xmin
# set value
cur_idx = beg_idx + idx
coco_pred_annotation = coco_pred_annotation._replace(image_id=cur_idx)
coco_pred_annotation = coco_pred_annotation._replace(category_id=1)
coco_pred_annotation = coco_pred_annotation._replace(keypoints=keypoints.reshape(-1))
coco_pred_annotation = coco_pred_annotation._replace(score=2)
# add to arr
coco_pred_annotations_arr.append(coco_pred_annotation)
return ()