-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiomechanicsOfGait.py
352 lines (314 loc) · 17.8 KB
/
BiomechanicsOfGait.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
import cv2
import mediapipe as mp
import numpy as np
import time
import math
mp_pose = mp.solutions.pose
mp_draw = mp.solutions.drawing_utils
pose = mp_pose.Pose()
cap = cv2.VideoCapture("./Dataset/gait.mp4")
# mediapipe connect point
rl1, rl2, rl3 = 24, 26, 28
ll1, ll2, ll3 = 23, 25, 27
left_ear, left_shoulder, left_hip = 8, 12, 24
ra1, ra2, ra3 = 11, 13, 15
la1, la2, la3 = 12, 14, 16
rs1, rs2, rs3 = 23, 11, 15
ls1, ls2, ls3 = 24, 12, 16
right_toe_y = []
right_toe_x = []
left_toe_y = []
left_toe_x = []
left_heel_x = []
left_heel_y = []
right_heel_x = []
right_heel_y = []
right_arm_x = []
right_arm_y = []
left_arm_x = []
left_arm_y = []
check_lead_foot = 0 #boolean
no_total_frame = 0
no_neck_frame = 0
no_rightleg_frame = 0
no_leftleg_frame = 0
no_limping_right = 0
no_limping_left = 0
no_swing_right = 0
no_swing_left = 0
lead_foot_right = 0
lead_foot_left = 0
while True:
try:
# read videos
ret, img = cap.read()
cv2.imshow('Frame', img)
cv2.waitKey(1)
results = pose.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
mp_draw.draw_landmarks(img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
h, w, c = img.shape
opImg = np.zeros([h, w, c])
opImg.fill(128)
mp_draw.draw_landmarks(opImg, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_draw.DrawingSpec((255, 0, 0), 2, 2),
mp_draw.DrawingSpec((255, 0, 255), 2, 2))
########################################################################### Start analyze ################################################################################
# Read data by mediapipe
new_lmList = []
if results.pose_landmarks:
for id, lm in enumerate(results.pose_landmarks.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
new_lmList.append([id, cx, cy])
cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
if len(new_lmList) != 0:
no_total_frame += 1
cv2.putText(img, str(no_total_frame), (50, 50),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 256, 0), 2)
# Right leg
rx1, ry1 = new_lmList[rl1][1:]
rx2, ry2 = new_lmList[rl2][1:]
rx3, ry3 = new_lmList[rl3][1:]
# Calculate the angle right leg
right_angle = math.degrees(math.atan2(ry3 - ry2, rx3 - rx2) - math.atan2(ry1 - ry2, rx1 - rx2))
if right_angle < 0:
right_angle += 360
# Left leg
lx1, ly1 = new_lmList[ll1][1:]
lx2, ly2 = new_lmList[ll2][1:]
lx3, ly3 = new_lmList[ll3][1:]
# Calculate the angle left leg
left_angle = math.degrees(math.atan2(ly3 - ly2, lx3 - lx2) - math.atan2(ly1 - ly2, lx1 - lx2))
if left_angle < 0:
left_angle += 360
# Right arm
rax1, ray1 = new_lmList[ra1][1:]
rax2, ray2 = new_lmList[ra2][1:]
rax3, ray3 = new_lmList[ra3][1:]
# Calculate the angle right arm
right_arm_angle = math.degrees(math.atan2(ray3 - ray2, rax3 - rax2) - math.atan2(ray1 - ray2, rax1 - rax2))
if right_arm_angle < 0:
right_arm_angle += 360
# Left arm
lax1, lay1 = new_lmList[la1][1:]
lax2, lay2 = new_lmList[la2][1:]
lax3, lay3 = new_lmList[la3][1:]
# Calculate the angle left arm
left_arm_angle = math.degrees(math.atan2(lay3 - lay2, lax3 - lax2) - math.atan2(lay1 - lay2, lax1 - lax2))
if left_arm_angle < 0:
left_arm_angle += 360
# Right shoulder
rsx1, rsy1 = new_lmList[rs1][1:]
rsx2, rsy2 = new_lmList[rs2][1:]
rsx3, rsy3 = new_lmList[rs3][1:]
# Calculate the angle right arm
right_shoulder_angle = math.degrees(math.atan2(rsy3 - rsy2, rsx3 - rsx2) - math.atan2(rsy1 - rsy2, rsx1 - rsx2))
# if right_shoulder_angle < 0:
# right_shoulder_angle += 360
# Left shoulder
lsx1, lsy1 = new_lmList[ls1][1:]
lsx2, lsy2 = new_lmList[ls2][1:]
lsx3, lsy3 = new_lmList[ls3][1:]
# Calculate the angle left arm
left_shoulder_angle = math.degrees(math.atan2(lsy3 - lsy2, lsx3 - lsx2) - math.atan2(lsy1 - lsy2, lsx1 - lsx2))
# if left_shoulder_angle < 0:
# left_shoulder_angle += 360
# Showing line and angle two legs
cv2.line(img, (rx1, ry1), (rx2, ry2), (255, 255, 255), 3)
cv2.line(img, (rx3, ry3), (rx2, ry2), (255, 255, 255), 3)
cv2.circle(img, (rx1, ry1), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rx1, ry1), 15, (0, 0, 255), 2)
cv2.circle(img, (rx2, ry2), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rx2, ry2), 15, (0, 0, 255), 2)
cv2.circle(img, (rx3, ry3), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rx3, ry3), 15, (0, 0, 255), 2)
cv2.putText(img, str(int(right_angle)), (rx2 - 50, ry2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
cv2.line(img, (lx1, ly1), (lx2, ly2), (255, 255, 255), 3)
cv2.line(img, (lx3, ly3), (lx2, ly2), (255, 255, 255), 3)
cv2.circle(img, (lx1, ly1), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lx1, ly1), 15, (0, 255, 0), 2)
cv2.circle(img, (lx2, ly2), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lx2, ly2), 15, (0, 255, 0), 2)
cv2.circle(img, (lx3, ly3), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lx3, ly3), 15, (0, 255, 0), 2)
cv2.putText(img, str(int(left_angle)), (lx2 - 50, ly2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
# Showing line and angle two arms
cv2.line(img, (rax1, ray1), (rax2, ray2), (255, 255, 255), 3)
cv2.line(img, (rax3, ray3), (rax2, ray2), (255, 255, 255), 3)
cv2.circle(img, (rax1, ray1), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rax1, ray1), 15, (0, 0, 255), 2)
cv2.circle(img, (rax2, ray2), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rax2, ray2), 15, (0, 0, 255), 2)
cv2.circle(img, (rax3, ray3), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rax3, ray3), 15, (0, 0, 255), 2)
# cv2.putText(img, str(int(right_arm_angle)), (rax2 - 50, ray2 + 50),
# cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
cv2.line(img, (lax1, lay1), (lax2, lay2), (255, 255, 255), 3)
cv2.line(img, (lax3, lay3), (lax2, lay2), (255, 255, 255), 3)
cv2.circle(img, (lax1, lay1), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lax1, lay1), 15, (0, 255, 0), 2)
cv2.circle(img, (lax2, lay2), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lax2, lay2), 15, (0, 255, 0), 2)
cv2.circle(img, (lax3, lay3), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lax3, lay3), 15, (0, 255, 0), 2)
# cv2.putText(img, str(int(left_arm_angle)), (lax2 - 50, lay2 + 50),
# cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
# Showing angle sholder
cv2.line(img, (rsx1, rsy1), (rax2, ray2), (255, 255, 255), 3)
cv2.line(img, (rsx3, rsy3), (rax2, ray2), (255, 255, 255), 3)
cv2.circle(img, (rsx1, rsy1), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rsx1, rsy1), 15, (0, 0, 255), 2)
cv2.circle(img, (rsx2, rsy2), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rsx2, rsy2), 15, (0, 0, 255), 2)
cv2.circle(img, (rsx3, rsy3), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (rsx3, rsy3), 15, (0, 0, 255), 2)
cv2.putText(img, str(int(right_shoulder_angle)), (rax2 - 50, ray2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
cv2.line(img, (lsx1, lsy1), (lsx2, lsy2), (255, 255, 255), 3)
cv2.line(img, (lsx3, lsy3), (lsx2, lsy2), (255, 255, 255), 3)
cv2.circle(img, (lsx1, lsy1), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lsx1, lsy1), 15, (0, 255, 0), 2)
cv2.circle(img, (lsx2, lsy2), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lsx2, lsy2), 15, (0, 255, 0), 2)
cv2.circle(img, (lsx3, lsy3), 10, (0, 255, 0), cv2.FILLED)
cv2.circle(img, (lsx3, lsy3), 15, (0, 255, 0), 2)
cv2.putText(img, str(int(left_shoulder_angle)), (lsx2 - 50, lsy2 + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
#Showing angle of shoulder
# cv2.putText(img, "angle right shoulder : " + str(int(right_shoulder_angle)), (50, 125),
# cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
# cv2.putText(img, "angle right shoulder : " + str(int(right_shoulder_angle)), (50, 125),
# cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 2)
# cv2.putText(img, "angle left shoulder : " + str(int(left_shoulder_angle)), (50, 150),
# cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
# cv2.putText(img, "angle left shoulder : " + str(int(left_shoulder_angle)), (50, 150),
# cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 2)
############################################################################### Circumduction ##################################################################################
#data for check Circumduction (some leg always swing phase)
if 200 > right_angle > 160 :
no_rightleg_frame += 1
if 200 > left_angle > 160 :
no_leftleg_frame += 1
# Circumduction 99% of one leg always swing phase
if no_rightleg_frame / no_total_frame > 0.99 or no_leftleg_frame / no_total_frame > 0.99:
cv2.putText(img, "Gait abnormal - risk : Circumduction", (50, 100),
cv2.FONT_HERSHEY_PLAIN, 1, (0,0,0), 5)
cv2.putText(img, "Gait abnormal - risk : Circumduction", (50, 100),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
############################################################################# Slouch posture ###################################################################################
# display angle of neck / bent
left_ear_x, left_ear_y = new_lmList[left_ear][1:]
left_shoulder_x, left_shoulder_y = new_lmList[left_shoulder][1:]
left_hip_x, left_hip_y = new_lmList[left_hip][1:]
left_ear_angle = math.degrees(math.atan2(left_hip_y - left_shoulder_y, left_hip_x - left_shoulder_x) - math.atan2(left_ear_y - left_shoulder_y, left_ear_x - left_shoulder_x))
if left_ear_angle < 0:
left_ear_angle += 360
cv2.line(img, (left_ear_x, left_ear_y), (left_shoulder_x, left_shoulder_y), (255, 255, 255), 3)
cv2.line(img, (left_hip_x, left_hip_y), (left_shoulder_x, left_shoulder_y), (255, 255, 255), 3)
cv2.circle(img, (left_ear_x, left_ear_y), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (left_ear_x, left_ear_y), 15, (0, 0, 255), 2)
cv2.circle(img, (left_shoulder_x, left_shoulder_y), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (left_shoulder_x, left_shoulder_y), 15, (0, 0, 255), 2)
cv2.circle(img, (left_hip_x, left_hip_y), 10, (0, 0, 255), cv2.FILLED)
cv2.circle(img, (left_hip_x, left_hip_y), 15, (0, 0, 255), 2)
cv2.putText(img, str(int(left_ear_angle)-180), (left_shoulder_x - 50, left_shoulder_y + 50),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if left_ear_angle - 180 > 30 or left_ear_angle - 180 < -30 :
no_neck_frame += 1
if no_neck_frame / no_total_frame > 0.6 :
cv2.putText(img, "Gait abnormal - risk : Slouch posture", (50, 125),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
cv2.putText(img, "Gait abnormal - risk : Slouch posture", (50, 125),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
############################################################################# Gait limping ################################################################################
# Gait limping read toe and heel point
right_toe_index_x, right_toe_index_y = new_lmList[32][1:]
right_heel_index_x, right_heel_index_y = new_lmList[30][1:]
left_toe_index_x, left_toe_index_y = new_lmList[31][1:]
left_heel_index_x, left_heel_index_y = new_lmList[29][1:]
right_toe_y.append(right_toe_index_y)
right_toe_x.append(right_toe_index_x)
right_heel_x.append(right_heel_index_x)
right_heel_y.append(right_heel_index_y)
left_toe_y.append(left_toe_index_y)
left_toe_x.append(left_toe_index_x)
left_heel_x.append(left_heel_index_x)
left_heel_y.append(left_heel_index_y)
# print("X : ", right_toe_index_x, " Y : ", right_toe_index_y)
if left_toe_index_x > left_heel_index_x: #Check gait side (right side)
if left_toe_index_x > right_heel_index_x:
check_lead_foot = 1 #Left foot is lead
no_limping_left += 1
else:
check_lead_foot = 0 #Right foot is lead
no_limping_right += 1
elif left_toe_index_x < left_heel_index_x:
if left_toe_index_x < right_heel_index_x:
check_lead_foot = 1 #Left foot is lead
no_limping_left += 1
else:
check_lead_foot = 0 #Right foot is lead
no_limping_right += 1
if no_limping_right / no_total_frame > 0.75 or no_limping_left / no_total_frame > 0.75:
cv2.putText(img, "Gait abnormal - risk : Limping", (50, 150),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
cv2.putText(img, "Gait abnormal - risk : Limping", (50, 150),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
#Showing Lead of feet
if check_lead_foot:
cv2.putText(img, "Lead foot : left", (50, 350),
cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 2)
else:
cv2.putText(img, "Lead foot : right", (50, 350),
cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 2)
############################################################################# No arm swing ################################################################################
if left_shoulder_angle <= 15 and left_shoulder_angle >= -15:
no_swing_left += 1
if right_shoulder_angle <= 15 and right_shoulder_angle >= -15:
no_swing_right += 1
if no_swing_right / no_total_frame > 0.90:
cv2.putText(img, "Gait abnormal - risk : Left arm no swing", (50, 175),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
cv2.putText(img, "Gait abnormal - risk : Left arm no swing", (50, 175),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
if no_swing_left / no_total_frame > 0.90:
cv2.putText(img, "Gait abnormal - risk : Right arm no swing", (50, 200),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 5)
cv2.putText(img, "Gait abnormal - risk : Right arm no swing", (50, 200),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
print("="*20)
# Check Neck, Circumduction(R L legs), Limping(R L feet), No arm swing(R L shoulder)
print("Slouch posture : %.3f "%float(no_neck_frame / no_total_frame), "\nStraight \t Right leg : %.3f "%float(no_rightleg_frame / no_total_frame), "\tLeft leg : %.3f "%float(no_leftleg_frame / no_total_frame), "\nLimping \t right leg : %.3f "%float(no_limping_right / no_total_frame), "\t left leg : %.3f "%float(no_limping_left / no_total_frame), "\nNo swing \t Right arm : %.3f "%float(no_swing_right / no_total_frame), "\t Left arm : %.3f"%float(no_swing_left / no_total_frame))
print("="*20)
cv2.imshow("Extracted Pose", opImg)
cv2.imshow("Pose Estimation", img)
# Break the loop if the user presses 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
no_gait = 0
print("="*30," Conclude ", "="*30, "\n Gait abnormal - risk : ", sep='', end='')
if no_limping_right / no_total_frame > 0.75 or no_limping_left / no_total_frame > 0.75:
no_gait += 1
print("\n\t- Limping", end='')
if no_neck_frame / no_total_frame > 0.60 :
no_gait += 1
print("\n\t- Slouch posture", end='')
if no_rightleg_frame / no_total_frame > 0.99 or no_leftleg_frame / no_total_frame > 0.99:
no_gait += 1
print("\n\t- Circumduction", end='')
if no_swing_right / no_total_frame > 0.90:
no_gait += 1
print("\n\t- Right arm no swing", end='')
if no_swing_left / no_total_frame > 0.90:
no_gait += 1
print("\n\t- Left arm no swing", end='')
if no_gait == 0:
print("No risk", end='')
print()
print("="*70)
break
cap.release()
cv2.destroyAllWindows()