-
Notifications
You must be signed in to change notification settings - Fork 1
/
human_appearance.py
325 lines (244 loc) · 12.2 KB
/
human_appearance.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf
import gc
import cv2
import copy
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torchvision.transforms as transforms
import torch.nn as nn
import torch.utils.data
from scipy.ndimage.filters import gaussian_filter
ROOT_DIR = os.path.abspath("./PoseEstimation/keras_Realtime_Multi-Person_Pose_Estimation-master/")
# Import OpenPose
sys.path.append(ROOT_DIR) # To find local version of the library
import util
ALPHA_POSE = os.path.abspath(("./PoseEstimation/AlphaPose/AlphaPose-pytorch/"))
# Import AlphaPose
sys.path.append(ALPHA_POSE) # To find local version of the library
from opt import opt
import demo
from dataloader import ImageLoader, DetectionLoader, DetectionProcessor, DataWriter, Mscoco
from yolo.util import write_results, dynamic_write_results
from SPPE.src.main_fast_inference import *
from tqdm import tqdm
import time
from fn import getTime
from pPose_nms import pose_nms, write_json
#Mask-RCNN
def get_person_appearance(model, obs, paths, path_to_images):
#14x14 = 196 sized flattened feature vector
feature_size = 14*14
activation_ = np.zeros((obs[0].shape[1], feature_size))
activations_ = []
count = 0
total = len(obs)*obs[0].shape[0]
for i in range(len(obs)):
for person in range(obs[i].shape[0]):
count += 1
for frame in range(obs[i].shape[1]):
image = skimage.io.imread(path_to_images + os.path.splitext(os.path.basename(paths[i]))[0] + "/" + str(int(obs[i][person][frame][1])) + ".png")
height_, width_, _ = image.shape
x1 = int(obs[i][person][frame][2] * width_)
y1 = int(obs[i][person][frame][3] * height_)
x2 = int(obs[i][person][frame][2] * width_) + int(obs[i][person][frame][4] * width_)
y2 = int(obs[i][person][frame][3] * height_) + int(obs[i][person][frame][5] * height_)
cropped_person = image[y1:y2, x1:x2]
#activations_74 is the final layer of the network
activations = model.run_graph([cropped_person], [("activation_74", model.keras_model.get_layer("activation_74").output)])
#extract feature vector of size 14x14x256 and average along the channel dimension
activation = np.transpose(activations["activation_74"][0,0,:,:,:], [2, 0, 1])
activation = np.mean(activation, axis=0)
activation_[frame] = activation.flatten()
activations_.append(activation_)
print(str(count)+"/"+str(total))
activations_ = np.reshape(activations_, [len(activations_), obs[0].shape[1], feature_size])
return activations_
#OpenPose
def get_person_pose(model, params, model_params, obs, paths, path_to_images):
#17 2D joint locations
feature_size = 17*2
coords = np.zeros((obs[0].shape[1], feature_size))
all_coords = []
for i in range(len(obs)):
#i = 42
for person in range(obs[i].shape[0]):
#person = 74
for frame in range(obs[i].shape[1]):
#frame = 0
image = cv2.imread(path_to_images + os.path.splitext(os.path.basename(paths[i]))[0] + "/" + str(
int(obs[i][person][frame][1])) + ".png")
height_, width_, _ = image.shape
print(path_to_images + os.path.splitext(os.path.basename(paths[i]))[0] + "/" + str(
int(obs[i][person][frame][1])) + ".png")
print('i: ',i," person: ",person, " frame: ", frame)
x1 = int(obs[i][person][frame][2] * width_)
y1 = int(obs[i][person][frame][3] * height_)
x2 = int(obs[i][person][frame][2] * width_) + int(obs[i][person][frame][4] * width_)
y2 = int(obs[i][person][frame][3] * height_) + int(obs[i][person][frame][5] * height_)
cropped_person = image[y1:y2, x1:x2]
oriImg = cropped_person
#if a person is too small dont calculate pose and continue
if oriImg.shape[0] < 15 or oriImg.shape[1] < 15:
joint_coords = np.zeros((17, 2))
coords[frame] = joint_coords.flatten()
#else calculate pose
else:
multiplier = [x * model_params['boxsize'] / oriImg.shape[0] for x in params['scale_search']]
heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
print(oriImg.shape[0], " ", oriImg.shape[1])
for m in range(len(multiplier)):
scale = multiplier[m]
imageToTest = cv2.resize(oriImg, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
imageToTest_padded, pad = util.padRightDownCorner(imageToTest, model_params['stride'],
model_params['padValue'])
input_img = np.transpose(np.float32(imageToTest_padded[:, :, :, np.newaxis]),
(3, 0, 1, 2)) # required shape (1, width, height, channels)
output_blobs = model.predict(input_img)
# extract outputs, resize, and remove padding
heatmap = np.squeeze(output_blobs[1]) # output 1 is heatmaps
heatmap = cv2.resize(heatmap, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
interpolation=cv2.INTER_CUBIC)
heatmap = heatmap[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
heatmap = cv2.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)
heatmap_avg = heatmap_avg + heatmap / len(multiplier)
#coordinates of 17 joints of the human body
# 0: "head", 1: "neck" ,2: "r.shoulder", 3: "r.elbow", 4: "r.wrist", 5: "l.shoulder", 6: "l.elbow",
# 7: "l.wrist", 8: "r.hip", 9: "r.knee", 10: "r.ankle", 11: "l.hip", 12: "l.knee", 13: "l.ankle",
# 14: "l.eye", 15: "r.eye", 16: "l.ear", 17: "r.ear"
joint_coords = np.zeros((17,2))
for part in range(17):
map_ori = heatmap_avg[:, :, part]
map = gaussian_filter(map_ori, sigma=3)
map_left = np.zeros(map.shape)
map_left[1:, :] = map[:-1, :]
map_right = np.zeros(map.shape)
map_right[:-1, :] = map[1:, :]
map_up = np.zeros(map.shape)
map_up[:, 1:] = map[:, :-1]
map_down = np.zeros(map.shape)
map_down[:, :-1] = map[:, 1:]
peaks_binary = np.logical_and.reduce(
(map >= map_left, map >= map_right, map >= map_up, map >= map_down, map > params['thre1']))
peaks = list(zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0])) # note reverse
if len(peaks) != 0:
joint_coords[part] = peaks[0]
#normalize coordinates
joint_coords[part][0] /= cropped_person.shape[1]
joint_coords[part][1] /= cropped_person.shape[0]
coords[frame] = joint_coords.flatten()
all_coords.append(coords)
all_coords_ = np.reshape(all_coords, [len(all_coords), obs[0].shape[1], feature_size])
return all_coords_
#AlphaPose
def get_person_pose_(obs, paths, path_to_images):
count = 0
output = './PoseEstimation/AlphaPose/AlphaPose-pytorch/examples/demo/'
#17 2D human joints with confidence score
#feature_size = 17*3
#10 angles
feature_size = 10
#extract people from data
for i in range(len(obs)):
for person in range(obs[i].shape[0]):
for frame in range(obs[i].shape[1]):
count += 1
#image = cv2.imread(path_to_images + os.path.splitext(os.path.basename(paths[i]))[0] + "/" + str(
#int(obs[i][person][frame][1])) + ".png")
print(path_to_images + os.path.splitext(os.path.basename(paths[i]))[0] + "/" + str(
int(obs[i][person][frame][1])) + ".png")
#
#height_, width_, _ = image.shape
#
#x1 = int(obs[i][person][frame][2] * width_)
#y1 = int(obs[i][person][frame][3] * height_)
#x2 = int(obs[i][person][frame][2] * width_) + int(obs[i][person][frame][4] * width_)
#y2 = int(obs[i][person][frame][3] * height_) + int(obs[i][person][frame][5] * height_)
#
#cropped_person = image[y1:y2, x1:x2]
#cropped_person = cv2.resize(cropped_person, (64, 128))
outfile = output + '%s.jpg' % (str(count))
#cv2.imwrite(outfile, cropped_person)
#extract poses for each person
keypoints = demo.test()
final_pose = np.zeros((count, feature_size))
#print(range(len(keypoints)))
for i in range(len(keypoints)):
img_name = keypoints[i].get('imgname')
index = int(os.path.splitext(img_name)[0])
if len(keypoints[i].get('result')) > 0:
angles = []
pose = keypoints[i].get('result')[0].get('keypoints')
#image = cv2.imread(output+img_name)
#height_, width_, _ = image.shape
#pose = pose.numpy()
#normalize pose
#pose[:,0] = pose[:, 0] / width_
#pose[:,1] = pose[:, 1] / height_
#conf = keypoints[i].get('result')[0].get('kp_score')
#conf = conf.numpy()
#conf = conf.flatten()
#pose = pose.flatten()
#pose = np.concatenate((pose, conf))
#final_pose[index-1] = pose
# {0, "Nose"},
# {1, "LEye"},
# {2, "REye"},
# {3, "LEar"},
# {4, "REar"},
# {5, "LShoulder"},
# {6, "RShoulder"},
# {7, "LElbow"},
# {8, "RElbow"},
# {9, "LWrist"},
# {10, "RWrist"},
# {11, "LHip"},
# {12, "RHip"},
# {13, "LKnee"},
# {14, "Rknee"},
# {15, "LAnkle"},
# {16, "RAnkle"}
#calculate angles between body parts
angle_between_nodes = [(5, 7), (6, 8), (7, 9), (8, 10), (11, 13), (13, 15), (12, 14), (14, 16)]
for pair in angle_between_nodes:
node1_x = pose[pair[0], 0]
node1_y = pose[pair[0], 1]
node2_x = pose[pair[1], 0]
node2_y = pose[pair[1], 1]
vector = (node2_x - node1_x, node2_y - node1_y)
dot_product = np.dot(vector, (1, 0))
norm = np.linalg.norm(vector)
angle = np.arccos(dot_product / norm)
if np.isnan(angle):
angle = 0
angles.append(angle)
angle_between_limbs = [((11, 15), (12, 16)), ((5, 9), (6, 10))]
for limb_pair in angle_between_limbs:
node1_x = pose[limb_pair[0][0], 0]
node1_y = pose[limb_pair[0][0], 1]
node2_x = pose[limb_pair[0][1], 0]
node2_y = pose[limb_pair[0][1], 1]
vector1 = (node2_x - node1_x, node2_y - node1_y)
node3_x = pose[limb_pair[1][0], 0]
node3_y = pose[limb_pair[1][0], 1]
node4_x = pose[limb_pair[1][1], 0]
node4_y = pose[limb_pair[1][1], 1]
vector2 = (node4_x - node3_x, node4_y - node3_y)
dot_product = np.dot(vector1, vector2)
norm1 = np.linalg.norm(vector1)
norm2 = np.linalg.norm(vector2)
angle = np.arccos(dot_product / norm1 / norm2)
if np.isnan(angle):
angle = 0
angles.append(angle)
final_pose[index - 1] = angles
final_pose = np.reshape(final_pose, [int(count / obs[0].shape[1]), obs[0].shape[1], feature_size])
return final_pose