-
Notifications
You must be signed in to change notification settings - Fork 13
/
pose_estimator.py
378 lines (248 loc) · 15.3 KB
/
pose_estimator.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Written by Lex Whalen
import cv2 as cv
import numpy as np
import os
import math
from frame_operations import FrameOperations
class PoseEstimator():
def __init__(self):
self.FRAME_OPS = FrameOperations()
self.BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
"LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
"RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
"LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }
self.POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]
self.CWD = os.getcwd()
self.RESOURCES = os.path.join(self.CWD,'resources')
self.GRAPH_OPT = os.path.join(self.RESOURCES,'graph_opt.pb')
self.NET = cv.dnn.readNetFromTensorflow(self.GRAPH_OPT)
self.THR = 0.1
self.IN_WIDTH = 396
self.IN_HEIGHT = 368
self.POINTS = []
# key angles: RightArm is the angle between Rshoulder, RElbow,RWrist
# note for some calcs we can reuse the same connects!
self.KEY_DISTANCES = {"RArm":{"RShoulder-RElbow":None,"RElbow-RWrist":None,"Neck-RShoulder":None},
"LArm":{"LShoulder-LElbow":None,"LElbow-LWrist":None,"Neck-LShoulder":None},
"RLeg":{"RHip-RKnee":None,"RKnee-RAnkle":None},
"LLeg":{"LHip-RKnee":None,"LKnee-RAnkle":None}}
self.KEY_ANGLES = {"RArm": [],"LArm":[],"RLeg":[],"LLeg":[]}
self.TEXT_COLOR = (0,0,0)
def rad_to_deg(self,rad):
return rad * (180/math.pi)
def get_pose_key_angles(self, frame, wantBlank = False):
"""applies pose estimation on frame, gets the distances between points"""
# for the key points that do not come in pairs
RShoulder_pos = None
RWrist_pos = None
LShoulder_pos = None
LWrist_pos = None
Neck_pos = None
RElbow_pos = None
LElbow_pos = None
RHip_pos = None
RKnee_pos = None
RAnkle_pos = None
LHip_pos = None
LKnee_pos = None
LAnkle_pos = None
frame_h,frame_w = frame.shape[0:2]
self.NET.setInput(cv.dnn.blobFromImage(frame, 1.0, (self.IN_WIDTH, self.IN_HEIGHT), (127.5, 127.5, 127.5), swapRB=True, crop=False))
out = self.NET.forward()
out = out[:, :19, :, :] # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
assert(len(self.BODY_PARTS) == out.shape[1])
#clear to get new points
self.POINTS.clear()
for i in range(len(self.BODY_PARTS)):
# Slice heatmap of corresponging body's part.
heatMap = out[0, i, :, :]
# Originally, we try to find all the local maximums. To simplify a sample
# we just find a global one. However only a single pose at the same time
# could be detected this way.
_, conf, _, point = cv.minMaxLoc(heatMap)
x = (frame_w * point[0]) / out.shape[3]
y = (frame_h * point[1]) / out.shape[2]
# Add a point if it's confidence is higher than threshold.
if(conf > self.THR):
self.POINTS.append((int(x),int(y)))
else:
self.POINTS.append(None)
# create blank frame overlay once OpenPose has read original frame so as to work
if wantBlank:
frame = np.zeros((frame_h,frame_w,3),np.uint8)
self.TEXT_COLOR = (255,255,255)
for pair in self.POSE_PAIRS:
# ex: pair 1: [["Neck","RShoulder"]]
# partFrom = Neck, partTo = RShoulder
partFrom = pair[0]
partTo = pair[1]
assert(partFrom in self.BODY_PARTS)
assert(partTo in self.BODY_PARTS)
# continuing ex: idFrom = BODY_PART["Neck"] returns 1
# similarly, idTo = BODY_PARTS["RShoulder"] returns 2
idFrom = self.BODY_PARTS[partFrom]
idTo = self.BODY_PARTS[partTo]
# if found points (if not found, returns None)
if self.POINTS[idFrom] and self.POINTS[idTo]:
# now we check each of the key points.
# "a", "b" correspond to the lengths of the limbs, "c" is the length between the end dots on the triangle. See video.
# we use law of cosines to find angle c:
# cos(C) = (a^2 + b^2 - c^2) / 2ab
# we first check for the points that do not come in pairs (make up the longest side of the triangle in the vid)
if(partFrom == "RShoulder"):
RShoulder_pos = self.POINTS[idFrom]
if(partTo == "RWrist"):
RWrist_pos = self.POINTS[idTo]
if(partFrom == "LShoulder"):
LShoulder_pos = self.POINTS[idFrom]
if(partTo == "LWrist"):
LWrist_pos = self.POINTS[idTo]
if(partFrom == "Neck"):
Neck_pos = self.POINTS[idFrom]
if(partTo == "RElbow"):
RElbow_pos = self.POINTS[idTo]
if(partTo == "LElbow"):
LElbow_pos = self.POINTS[idTo]
if(partFrom == "RHip"):
RHip_pos = self.POINTS[idFrom]
if(partTo == "RKnee"):
RKnee_pos = self.POINTS[idTo]
if(partTo == "RAnkle"):
RAnkle_pos = self.POINTS[idTo]
if(partFrom == "LHip"):
LHip_pos = self.POINTS[idFrom]
if(partTo == "LKnee"):
LKnee_pos = self.POINTS[idTo]
if(partTo == "LAnkle"):
LAnkle_pos = self.POINTS[idTo]
# START (R) Shoulder -> Elbow -> Wrist
if(partFrom == "RShoulder" and partTo == "RElbow"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["RArm"]["RShoulder-RElbow"] = dist_2
elif(partFrom == "RElbow" and partTo == "RWrist"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["RArm"]["RElbow-RWrist"] = dist_2
# END (R) Shoulder -> Elbow -> Wrist
# START (L) Shoulder -> Elbow -> Wrist
elif(partFrom == "LShoulder" and partTo == "LElbow"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["LArm"]["LShoulder-LElbow"] = dist_2
elif(partFrom == "LElbow" and partTo == "LWrist"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["LArm"]["LElbow-LWrist"] = dist_2
# END (L) Shoulder -> Elbow -> Wrist
# START (R) Neck -> Shoulder -> Elbow, (L) Neck -> Shoulder -> Elbow
# note we have already gotten Shoulder-Elbow values!
elif(partFrom == "Neck" and partTo == "RShoulder"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["RArm"]["Neck-RShoulder"] = dist_2
elif(partFrom == "Neck" and partTo == "LShoulder"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["LArm"]["Neck-LShoulder"] = dist_2
# END (R) Neck -> Shoulder -> Elbow, (L) Neck -> Shoulder -> Elbow
# START (R) Hip -> Knee -> Ankle
elif(partFrom == "RHip" and partTo == "RKnee"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["RLeg"]["RHip-RKnee"] = dist_2
elif(partFrom == "RKnee" and partTo == "RAnkle"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["RLeg"]["RKnee-RAnkle"] = dist_2
# END (R) Hip -> Knee -> Ankle
# START (L) Hip -> Knee -> Ankle
elif(partFrom == "LHip" and partTo == "LKnee"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["LLeg"]["LHip-LKnee"] = dist_2
elif(partFrom == "LKnee" and partTo == "LAnkle"):
dist_2 = (self.POINTS[idFrom][0] - self.POINTS[idTo][0]) **2 + (self.POINTS[idFrom][1] - self.POINTS[idTo][1]) **2
self.KEY_DISTANCES["LLeg"]["LKnee-LAnkle"] = dist_2
# check if you want to return just the blank, or the image with the angles.
cv.line(frame, self.POINTS[idFrom], self.POINTS[idTo], (0, 255, 0), 3) #last value is thickness
cv.ellipse(frame, self.POINTS[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
cv.ellipse(frame, self.POINTS[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
# we get the angles at the end.
if(RShoulder_pos is not None and RWrist_pos is not None):
c_2 = (RShoulder_pos[0] - RWrist_pos[0])**2 + (RShoulder_pos[1] - RWrist_pos[1])**2
a_2 = self.KEY_DISTANCES["RArm"]["RShoulder-RElbow"]
b_2 = self.KEY_DISTANCES["RArm"]["RElbow-RWrist"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = "Error"
self.KEY_ANGLES["RArm"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[3],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
if(LShoulder_pos is not None and LWrist_pos is not None):
c_2 = (LShoulder_pos[0] - LWrist_pos[0])**2 + (LShoulder_pos[1] - LWrist_pos[1])**2
a_2 = self.KEY_DISTANCES["LArm"]["LShoulder-LElbow"]
b_2 = self.KEY_DISTANCES["LArm"]["LElbow-LWrist"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = None
self.KEY_ANGLES["LArm"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[6],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
if(Neck_pos is not None and LElbow_pos is not None):
c_2 = (Neck_pos[0] - LElbow_pos[0])**2 + (Neck_pos[1] - LElbow_pos[1])**2
a_2 = self.KEY_DISTANCES["LArm"]["Neck-LShoulder"]
b_2 = self.KEY_DISTANCES["LArm"]["LShoulder-LElbow"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = None
self.KEY_ANGLES["LArm"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[5],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
if(Neck_pos is not None and RElbow_pos is not None):
c_2 = (Neck_pos[0] - RElbow_pos[0])**2 + (Neck_pos[1] - RElbow_pos[1])**2
a_2 = self.KEY_DISTANCES["RArm"]["Neck-RShoulder"]
b_2 = self.KEY_DISTANCES["RArm"]["RShoulder-RElbow"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = None
self.KEY_ANGLES["RArm"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[2],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
if(RHip_pos is not None and RAnkle_pos is not None):
c_2 = (RHip_pos[0] - RAnkle_pos[0])**2 + (RHip_pos[1] - RAnkle_pos[1])**2
a_2 = self.KEY_DISTANCES["RLeg"]["RHip-RKnee"]
b_2 = self.KEY_DISTANCES["RLeg"]["RKnee-RAnkle"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = None
self.KEY_ANGLES["RLeg"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[9],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
if(LHip_pos is not None and LAnkle_pos is not None):
c_2 = (LHip_pos[0] - LAnkle_pos[0])**2 + (LHip_pos[1] - LAnkle_pos[1])**2
a_2 = self.KEY_DISTANCES["LLeg"]["LHip-LKnee"]
b_2 = self.KEY_DISTANCES["LLeg"]["LKnee-LAnkle"]
# because degrees are easily to visualize for me:
try:
theta = self.rad_to_deg(math.acos((a_2 + b_2 - c_2)/(2*math.sqrt(a_2*b_2))))
except ZeroDivisionError:
theta = None
self.KEY_ANGLES["LLeg"].append(theta)
# display the angle at the center joint. Use self.BODY_PARTS to find joint indices
if(theta is not None):
cv.putText(frame,"{:.1f}".format(theta),self.POINTS[12],cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
t, _ = self.NET.getPerfProfile()
freq = cv.getTickFrequency() / 1000
cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, self.TEXT_COLOR)
return frame