-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkinematics_extraction.py
263 lines (213 loc) · 8.81 KB
/
kinematics_extraction.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
#==================================================================================
# KINEMATICS_EXTRACTION
#----------------------------------------------------------------------------------
# Input: Pose sequence, Output: Raw kinematics
# Given a JSON describing poses throughout two video views,
# Extracts kinematics and computes kinematics through joint angles
#==================================================================================
# Imports
#==================================================================================
import numpy as np
import json
import time
#==================================================================================
# Constants
#==================================================================================
ptID = {
'nose': 0,
'eye_L': 1,'eye_R': 2,
'ear_L': 3,'ear_R': 4,
'shoulder_L': 5, 'shoulder_R': 6,
'elbow_L': 7, 'elbow_R': 8,
'wrist_L': 9, 'wrist_R': 10,
'hip_L': 11, 'hip_R': 12,
'knee_L': 13, 'knee_R': 14,
'ankle_L': 15, 'ankle_R': 16
}
#==================================================================================
# Methods
#==================================================================================
# Calculates joint angle of knee in Side view
def calc_knee_angle_S(hip, knee, ankle, isRightToLeft):
if (hip == [-1, -1] or knee == [-1, -1] or ankle == [-1,-1]):
return None # returns this value as error code for no keypoint detection
# Identifying joint positions
a = np.array(hip)
b = np.array(knee)
c = np.array(ankle)
# Compute vectors from main joint
ba = a - b
m_ba = - ba
bc = c - b
cosine_angle = np.dot(m_ba, bc) / (np.linalg.norm(m_ba) * np.linalg.norm(bc))
angle = np.arccos(cosine_angle)
angle = np.degrees(angle)
if (isRightToLeft and bc[0] < m_ba[0]): angle = - angle # Check if angle should be negative when walking <---
elif (not isRightToLeft and bc[0] > m_ba[0]): angle = - angle # Check if angle should be negative when walking --->
return angle.tolist()
# Calculates joint angle of hip in Side view
def calc_hip_angle_S(hip, knee, isRightToLeft):
if(hip == [-1,-1] or knee == [-1,-1]):
return None # returns this value as error code for no keypoint detection
# Identifying joint positions
a = np.array(hip) # Main joint
b = np.array(knee)
# Compute vectors from joints
ab = b - a
m_N = np.array([0,-1])
cosine_angle = np.dot(ab, m_N) / (np.linalg.norm(ab) * np.linalg.norm(m_N))
angle = np.arccos(cosine_angle)
angle = np.degrees(angle)
if (isRightToLeft and ab[0] > m_N[0]): angle = - angle
elif (not isRightToLeft and ab[0] < m_N[0]): angle = - angle
return angle.tolist()
# Traversing through pose to compute kinematics in sideView
def raw_angles_S(data, isRightToLeft=False, limit=10000):
knee_ang_L = []
knee_ang_R = []
hip_ang_L = []
hip_ang_R = []
count = 1
for pose in data:
#Left
knee_L = pose[ptID['knee_L']]
ankle_L = pose[ptID['ankle_L']]
hip_L = pose[ptID['hip_L']]
angle = calc_knee_angle_S(hip_L, knee_L, ankle_L, isRightToLeft)
knee_ang_L.append(angle)
angle = calc_hip_angle_S(hip_L, knee_L, isRightToLeft)
hip_ang_L.append(angle)
#Right
knee_R = pose[ptID['knee_R']]
ankle_R = pose[ptID['ankle_R']]
hip_R = pose[ptID['hip_R']]
angle = calc_knee_angle_S(hip_R, knee_R, ankle_R, isRightToLeft)
knee_ang_R.append(angle)
angle = calc_hip_angle_S(hip_R, knee_R, isRightToLeft)
hip_ang_R.append(angle)
if(count == limit): break
count += 1
knee_ang = [knee_ang_L, knee_ang_R]
hip_ang = [hip_ang_L, hip_ang_R]
return knee_ang, hip_ang
# Calculates joint angle of knee in Front view
def calc_knee_angle_F(hip, knee, ankle, isRightToLeft):
if (hip == [-1, -1] or knee == [-1, -1] or ankle == [-1,-1]):
return None # returns this value as error code for no keypoint detection
# Identifying joint positions
a = np.array(hip)
b = np.array(knee)
c = np.array(ankle)
# Compute vectors from main joint
ba = a - b
m_ba = - ba
bc = c - b
cosine_angle = np.dot(m_ba, bc) / (np.linalg.norm(m_ba) * np.linalg.norm(bc))
angle = np.arccos(cosine_angle)
angle = np.degrees(angle)
if (isRightToLeft and bc[0] > m_ba[0]): angle = - angle
elif (not isRightToLeft and bc[0] < m_ba[0]): angle = - angle
angle = angle + 5 # Heuristic catering for perpendicular of pelvis
return angle.tolist()
# Calculates joint angle of hip in Front view
def calc_hip_angle_F(hip, knee, isRightToLeft):
if(hip == [-1,-1] or knee == [-1,-1]):
return None # returns this value as error code for no keypoint detection
# Identifying joint positions
a = np.array(hip) # Main joint
b = np.array(knee)
# Compute vectors from joints
ab = b - a
m_N = np.array([0,-1])
cosine_angle = np.dot(ab, m_N) / (np.linalg.norm(ab) * np.linalg.norm(m_N))
angle = np.arccos(cosine_angle)
angle = np.degrees(angle)
if (isRightToLeft and ab[0] > m_N[0]): angle = - angle
elif (not isRightToLeft and ab[0] < m_N[0]): angle = - angle
angle = angle * 4/3 - 5 # A heuristic for catering for forward/backward pelvic tilt and perpendicular of pelvis
return angle.tolist()
# Traversing through pose to compute kinematics in Front view
def raw_angles_F(data, isRightToLeft=False, limit=10000):
knee_ang_L = []
knee_ang_R = []
hip_ang_L = []
hip_ang_R = []
count = 1
for pose in data:
#Left
knee_L = pose[ptID['knee_L']]
ankle_L = pose[ptID['ankle_L']]
hip_L = pose[ptID['hip_L']]
angle = calc_knee_angle_F(hip_L, knee_L, ankle_L, isRightToLeft)
knee_ang_L.append(angle)
angle = calc_hip_angle_F(hip_L, knee_L, isRightToLeft)
hip_ang_L.append(angle)
#Right
knee_R = pose[ptID['knee_R']]
ankle_R = pose[ptID['ankle_R']]
hip_R = pose[ptID['hip_R']]
angle = calc_knee_angle_F(hip_R, knee_R, ankle_R, not isRightToLeft)
knee_ang_R.append(angle)
angle = calc_hip_angle_F(hip_R, knee_R, not isRightToLeft)
hip_ang_R.append(angle)
if(count == limit): break
count += 1
knee_ang = [knee_ang_L, knee_ang_R]
hip_ang = [hip_ang_L, hip_ang_R]
return knee_ang, hip_ang
# Checks which direction gait is from side view (affects how angles in saggital plane are calculated)
def checkGaitDirectionS(dataS, dimS):
# Finds first instance of ankle in video
for pose in dataS:
ankle_L = pose[ptID['ankle_L']]
ankle_R = pose[ptID['ankle_R']]
if(ankle_L != [-1,-1]):
ankle_init = ankle_L
break
if (ankle_R != [-1, -1]):
ankle_init = ankle_R
break
init_x = ankle_init[0]
max_x = dimS[0]
if (init_x > max_x / 2):
return True
else:
return False
# Computes and saves kinematics (joint angles) from poses
def kinematics_extract(readFile, writeFile):
with open(readFile, 'r') as f:
jsonPose = json.load(f)
jsonList = []
for cap in jsonPose:
dataS = cap['dataS']
dimS = cap['dimS']
dataF = cap['dataF']
lenS = cap['lenS']
lenF = cap['lenF']
limit = max(lenF, lenS) # Can set to min if the same is desired
isRightToLeft = checkGaitDirectionS(dataS, dimS) # True: Right to Left, False: Left to Right
knee_FlexExt, hip_FlexExt = raw_angles_S(dataS, isRightToLeft, limit) # Coronal plane
knee_AbdAdd, hip_AbdAdd = raw_angles_F(dataF, isRightToLeft, limit) # Sagittal plane
jsonDict = {
'knee_FlexExt' : knee_FlexExt,
'hip_FlexExt' : hip_FlexExt,
'knee_AbdAdd' : knee_AbdAdd,
'hip_AbdAdd' : hip_AbdAdd
}
jsonList.append(jsonDict)
with open(writeFile, 'w') as outfile:
json.dump(jsonList, outfile, separators=(',', ':'))
#==================================================================================
# Main
#==================================================================================
def main():
for i in range(1, 22):
if(len(str(i)) < 2): i = '0' + str(i)
path = '..\\Part' + str(i) + '\\'
readFile = path + 'Part' + str(i) + '_pose.json'
writeFile = path + 'Part' + str(i) + '_angles.json'
start_time = time.time()
kinematics_extract(readFile, writeFile)
print('Kinematics extracted and saved in', '\"'+writeFile+'\"', '[Time:', '{0:.2f}'.format(time.time() - start_time), 's]')
if __name__ == '__main__':
main()