-
Notifications
You must be signed in to change notification settings - Fork 10
/
DeepFtoCoco.py
113 lines (98 loc) · 3.84 KB
/
DeepFtoCoco.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
import glob
import json
import numpy as np
from PIL import Image
from tqdm import tqdm
dataset = {
"info": {},
"licenses": [],
"images": [],
"annotations": [],
"categories": []
}
lst_name = ['short_sleeved_shirt', 'long_sleeved_shirt', 'short_sleeved_outwear', 'long_sleeved_outwear',
'vest', 'sling', 'shorts', 'trousers', 'skirt', 'short_sleeved_dress',
'long_sleeved_dress', 'vest_dress', 'sling_dress']
for idx, e in enumerate(lst_name):
dataset['categories'].append({
'id': idx + 1,
'name': e,
'supercategory': "clothes",
'keypoints': ['%i' % (i) for i in range(1, 295)],
'skeleton': []
})
path = '<path>'
imgs = glob.glob(path + 'image/*.jpg')
bar = tqdm(total=len(imgs))
sub_index = 0 # the index of ground truth instance
for num in range(1, len(imgs) + 1):
bar.update(1)
json_name = path + 'annos/' + str(num).zfill(6) + '.json'
image_name = path + 'image/' + str(num).zfill(6) + '.jpg'
if (num >= 0):
imag = Image.open(image_name)
width, height = imag.size
with open(json_name, 'r') as f:
temp = json.loads(f.read())
pair_id = temp['pair_id']
source = temp['source']
styles = {}
for i in temp:
if i == 'source' or i == 'pair_id':
continue
else:
points = np.zeros((294, 3))
sub_index = sub_index + 1
box = temp[i]['bounding_box']
w = box[2] - box[0]
h = box[3] - box[1]
x_1 = box[0]
y_1 = box[1]
bbox = [x_1, y_1, w, h]
cat = temp[i]['category_id']
style = temp[i]['style']
styles.update({style: pair_id})
seg = temp[i]['segmentation']
landmarks = temp[i]['landmarks']
points_x = landmarks[0::3]
points_y = landmarks[1::3]
points_v = landmarks[2::3]
points_x = np.array(points_x)
points_y = np.array(points_y)
points_v = np.array(points_v)
case = [0, 25, 58, 89, 128, 143, 158, 168, 182, 190, 219, 256, 275, 294]
idx_i, idx_j = case[cat - 1], case[cat]
for n in range(idx_i, idx_j):
points[n] = points_x[n - idx_i]
points[n, 1] = points_y[n - idx_i]
points[n, 2] = points_v[n - idx_i]
num_points = len(np.where(points_v > 0)[0])
dataset['annotations'].append({
'area': w * h,
'source': source,
'bbox': bbox,
'category_id': cat,
'id': sub_index,
'pair_id': pair_id,
'image_id': num,
'iscrowd': 0,
'style': style,
'num_keypoints': num_points,
'keypoints': points.tolist(),
'segmentation': seg,
})
dataset['images'].append({
'coco_url': '',
'date_captured': '',
'source': source,
'file_name': str(num).zfill(6) + '.jpg',
'flickr_url': '',
'id': num,
'license': 0,
'width': width,
'height': height,
'match_desc': styles,
})
json_name = path + 'annots.json'
with open(json_name, 'w') as f:
json.dump(dataset, f)