-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_singleposemodel.py
206 lines (164 loc) · 6.2 KB
/
eval_singleposemodel.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
import tensorflow as tf
import math
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import os
from utils import load_graph_model, get_input_tensors, get_output_tensors
# make tensorflow stop spamming messages
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "3"
# PATHS
imagePath = 'path/to/.jpg/file'
modelPath = 'path/to/folder/containing/model.json'
# CONSTANTS
OutputStride = 16
KEYPOINT_NAMES = [
"nose", "leftEye", "rightEye", "leftEar", "rightEar", "leftShoulder",
"rightShoulder", "leftElbow", "rightElbow", "leftWrist", "rightWrist",
"leftHip", "rightHip", "leftKnee", "rightKnee", "leftAnkle", "rightAnkle"
]
KEYPOINT_IDS = {name: id for id, name in enumerate(KEYPOINT_NAMES)}
CONNECTED_KEYPOINTS_NAMES = [
("leftHip", "leftShoulder"), ("leftElbow", "leftShoulder"),
("leftElbow", "leftWrist"), ("leftHip", "leftKnee"),
("leftKnee", "leftAnkle"), ("rightHip", "rightShoulder"),
("rightElbow", "rightShoulder"), ("rightElbow", "rightWrist"),
("rightHip", "rightKnee"), ("rightKnee", "rightAnkle"),
("leftShoulder", "rightShoulder"), ("leftHip", "rightHip")
]
CONNECTED_KEYPOINT_INDICES = [(KEYPOINT_IDS[a], KEYPOINT_IDS[b])
for a, b in CONNECTED_KEYPOINTS_NAMES]
print("Loading model...", end="")
graph = load_graph_model(modelPath)
print("done.\nLoading sample image...", end="")
def getBoundingBox(keypointPositions, offset=(10, 10, 10, 10)):
minX = math.inf
minY = math.inf
maxX = - math.inf
maxY = -math.inf
for x, y in keypointPositions:
if (x < minX):
minX = x
if(y < minY):
minY = y
if(x > maxX):
maxX = x
if (y > maxY):
maxY = y
return (minX - offset[0], minY-offset[1]), (maxX+offset[2], maxY + offset[3])
# load sample image into numpy array
img = tf.keras.preprocessing.image.load_img(imagePath)
imgWidth, imgHeight = img.size
targetWidth = (int(imgWidth) // OutputStride) * OutputStride + 1
targetHeight = (int(imgHeight) // OutputStride) * OutputStride + 1
print(imgHeight, imgWidth, targetHeight, targetWidth)
img = img.resize((targetWidth, targetHeight))
x = tf.keras.preprocessing.image.img_to_array(img, dtype=np.float32)
InputImageShape = x.shape
print("Input Image Shape in hwc", InputImageShape)
widthResolution = int((InputImageShape[1] - 1) / OutputStride) + 1
heightResolution = int((InputImageShape[0] - 1) / OutputStride) + 1
print('Resolution', widthResolution, heightResolution)
# add imagenet mean - extracted from body-pix source
m = np.array([-123.15, -115.90, -103.06])
x = np.add(x, m)
sample_image = x[tf.newaxis, ...]
print("done.\nRunning inference...", end="")
# evaluate the loaded model directly
with tf.compat.v1.Session(graph=graph) as sess:
input_tensor_names = get_input_tensors(graph)
print(input_tensor_names)
output_tensor_names = get_output_tensors(graph)
print(output_tensor_names)
input_tensor = graph.get_tensor_by_name(input_tensor_names[0])
results = sess.run(output_tensor_names, feed_dict={
input_tensor: sample_image})
print("done. {} outputs received".format(len(results))) # should be 4 outputs
print('fwd', results[0].shape)
print('bwd', results[1].shape)
offsets = np.squeeze(results[2], 0)
print('offsets', offsets.shape)
heatmaps = np.squeeze(results[3], 0)
print('heatmaps', heatmaps.shape)
offsetVector = []
heatmapPositions = []
keypointPositions = []
keyScores = []
for i in range(heatmaps.shape[2]):
heatmap = heatmaps[:, :, i] # Heat map of each keypoint
# SHOW HEATMAPS
'''
plt.clf()
plt.title('Heatmap' + str(i) + KEYPOINT_NAMES[i])
plt.ylabel('y')
plt.xlabel('x')
plt.imshow(heatmap * OutputStride)
plt.show()
'''
heatmap_sigmoid = tf.sigmoid(heatmap)
# Find position of max value in heatmap_sigmoid
y_heat, x_heat = np.unravel_index(
np.argmax(heatmap_sigmoid, axis=None), heatmap_sigmoid.shape)
heatmapPositions.append([x_heat, y_heat])
# max value is confidence score of part
keyScores.append(heatmap_sigmoid[y_heat, x_heat].numpy())
# Offset Corresponding to heatmap x and y
x_offset = offsets[y_heat, x_heat, i]
y_offset = offsets[y_heat, x_heat, heatmaps.shape[2]+i]
offsetVector.append([x_offset, y_offset])
key_x = x_heat * OutputStride + x_offset
key_y = y_heat * OutputStride + y_offset
keypointPositions.append([key_x, key_y])
print('heatmapPositions', np.asarray(heatmapPositions).shape)
print('offsetVector', np.asarray(offsetVector).shape)
print('keypointPositions', np.asarray(keypointPositions).shape)
print('keyScores', np.asarray(keyScores).shape)
# PRINT KEYPOINT CONFIDENCE SCORES
print("Keypoint Confidence Score")
for i, score in enumerate(keyScores):
print(KEYPOINT_NAMES[i], score)
# PRINT POSE CONFIDENCE SCORE
print("Pose Confidence Score", np.mean(np.asarray(keyScores)))
# Get Bounding BOX
(xmin, ymin), (xmax, ymax) = getBoundingBox(keypointPositions)
print('Bonding Box xmin,ymin, xmax, ymax format: ', xmin, ymin, xmax, ymax)
# Show Bounding BOX
implot = plt.imshow(img)
# Get the current reference
ax = plt.gca()
# Create a Rectangle patch
rect = patches.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin,
linewidth=1, edgecolor='r', facecolor='none', fill=False)
# Add the patch
ax.add_patch(rect)
plt.show()
# Show all keypoints
plt.figure(0)
im = plt.imread(imagePath)
implot = plt.imshow(im)
x_points = []
y_points = []
for i, [x, y] in enumerate(keypointPositions):
x_points.append(x)
y_points.append(y)
plt.scatter(x=x_points, y=y_points, c='r', s=40)
plt.show()
# DEBUG KEYPOINTS
# Visualize each keypoints with their keypoint names
'''
for i, [x, y] in enumerate(keypointPositions):
plt.figure(i)
plt.title('keypoint' + str(i) + KEYPOINT_NAMES[i])
# img = plt.imread(imagePath)
implot = plt.imshow(img)
plt.scatter(x=[x], y=[y], c='r', s=40)
plt.show()
'''
# SHOW CONNECTED KEYPOINTS
plt.figure(20)
for pt1, pt2 in CONNECTED_KEYPOINT_INDICES:
plt.title('connection points')
implot = plt.imshow(img)
plt.plot((keypointPositions[pt1][0], keypointPositions[pt2][0]), (
keypointPositions[pt1][1], keypointPositions[pt2][1]), 'ro-', linewidth=2, markersize=5)
plt.show()