-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathCocoFolder.py
173 lines (137 loc) · 5.5 KB
/
CocoFolder.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
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
import torch
import torch.utils.data as data
import numpy as np
import shutil
import time
import random
import os
import math
import json
from PIL import Image
import cv2
import Mytransforms
def read_data_file(file_dir):
lists = []
with open(file_dir, 'r') as fp:
line = fp.readline()
while line:
path = line.strip()
lists.append(path)
line = fp.readline()
return lists
def read_json_file(file_dir):
"""
filename: JSON file
return: two list: key_points list and centers list
"""
fp = open(file_dir)
data = json.load(fp)
kpts = []
centers = []
scales = []
for info in data:
kpt = []
center = []
scale = []
lists = info['info']
for x in lists:
kpt.append(x['keypoints'])
center.append(x['pos'])
scale.append(x['scale'])
kpts.append(kpt)
centers.append(center)
scales.append(scale)
fp.close()
return kpts, centers, scales
def generate_heatmap(heatmap, kpt, stride, sigma):
height, width, num_point = heatmap.shape
start = stride / 2.0 - 0.5
num = len(kpt)
length = len(kpt[0])
for i in range(num):
for j in range(length):
if kpt[i][j][2] > 1:
continue
x = kpt[i][j][0]
y = kpt[i][j][1]
for h in range(height):
for w in range(width):
xx = start + w * stride
yy = start + h * stride
dis = ((xx - x) * (xx - x) + (yy - y) * (yy - y)) / 2.0 / sigma / sigma
if dis > 4.6052:
continue
heatmap[h][w][j + 1] += math.exp(-dis)
if heatmap[h][w][j + 1] > 1:
heatmap[h][w][j + 1] = 1
return heatmap
def generate_vector(vector, cnt, kpts, vec_pair, stride, theta):
height, width, channel = cnt.shape
length = len(kpts)
for j in range(length):
for i in range(channel):
a = vec_pair[0][i]
b = vec_pair[1][i]
if kpts[j][a][2] > 1 or kpts[j][b][2] > 1:
continue
ax = kpts[j][a][0] * 1.0 / stride
ay = kpts[j][a][1] * 1.0 / stride
bx = kpts[j][b][0] * 1.0 / stride
by = kpts[j][b][1] * 1.0 / stride
bax = bx - ax
bay = by - ay
norm_ba = math.sqrt(1.0 * bax * bax + bay * bay) + 1e-9 # to aviod two points have same position.
bax /= norm_ba
bay /= norm_ba
min_w = max(int(round(min(ax, bx) - theta)), 0)
max_w = min(int(round(max(ax, bx) + theta)), width)
min_h = max(int(round(min(ay, by) - theta)), 0)
max_h = min(int(round(max(ay, by) + theta)), height)
for h in range(min_h, max_h):
for w in range(min_w, max_w):
px = w - ax
py = h - ay
dis = abs(bay * px - bax * py)
if dis <= theta:
vector[h][w][2 * i] = (vector[h][w][2 * i] * cnt[h][w][i] + bax) / (cnt[h][w][i] + 1)
vector[h][w][2 * i + 1] = (vector[h][w][2 * i + 1] * cnt[h][w][i] + bay) / (cnt[h][w][i] + 1)
cnt[h][w][i] += 1
return vector
class CocoFolder(data.Dataset):
def __init__(self, file_dir, stride, transformer=None):
self.img_list = read_data_file(file_dir[0])
self.mask_list = read_data_file(file_dir[1])
self.kpt_list, self.center_list, self.scale_list = read_json_file(file_dir[2])
self.stride = stride
self.transformer = transformer
self.vec_pair = [[2,3,5,6,8,9, 11,12,0,1,1, 1,1,2, 5, 0, 0, 14,15],
[3,4,6,7,9,10,12,13,1,8,11,2,5,16,17,14,15,16,17]] # different from openpose
self.theta = 1.0
self.sigma = 7.0
def __getitem__(self, index):
img_path = self.img_list[index]
img = np.array(cv2.imread(img_path), dtype=np.float32)
mask_path = self.mask_list[index]
mask = np.load(mask_path)
mask = np.array(mask, dtype=np.float32)
kpt = self.kpt_list[index]
center = self.center_list[index]
scale = self.scale_list[index]
img, mask, kpt, center = self.transformer(img, mask, kpt, center, scale)
height, width, _ = img.shape
mask = cv2.resize(mask, (width / self.stride, height / self.stride)).reshape((height / self.stride, width / self.stride, 1))
heatmap = np.zeros((height / self.stride, width / self.stride, len(kpt[0]) + 1), dtype=np.float32)
heatmap = generate_heatmap(heatmap, kpt, self.stride, self.sigma)
heatmap[:,:,0] = 1.0 - np.max(heatmap[:,:,1:], axis=2) # for background
heatmap = heatmap * mask
vecmap = np.zeros((height / self.stride, width / self.stride, len(self.vec_pair[0]) * 2), dtype=np.float32)
cnt = np.zeros((height / self.stride, width / self.stride, len(self.vec_pair[0])), dtype=np.int32)
vecmap = generate_vector(vecmap, cnt, kpt, self.vec_pair, self.stride, self.theta)
vecmap = vecmap * mask
img = Mytransforms.normalize(Mytransforms.to_tensor(img), [128.0, 128.0, 128.0], [256.0, 256.0, 256.0]) # mean, std
mask = Mytransforms.to_tensor(mask)
heatmap = Mytransforms.to_tensor(heatmap)
vecmap = Mytransforms.to_tensor(vecmap)
return img, heatmap, vecmap, mask
def __len__(self):
return len(self.img_list)