-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercises.py
298 lines (231 loc) · 11.2 KB
/
exercises.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
import cv2
import numpy as np
import mediapipe as mp
# draw landmarks & connections to screen
mp_drawing = mp.solutions.drawing_utils
# import Pose model
mp_pose = mp.solutions.pose
# Function to calculate the angle between three points
def calc_angle(x, y, z):
x = np.array(x)
y = np.array(y)
z = np.array(z)
radians = np.arctan2(z[1]-y[1], z[0]-y[0]) - np.arctan2(x[1]-y[1], x[0]-y[0])
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
# Recognize curl function
def recognise_curl(detection):
global counter
global state
global feedback
global range_flag
try:
landmarks = detection.pose_landmarks.landmark
# left arm
left_wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
left_elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
# right arm
right_wrist = [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
right_shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
right_elbow = [landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y]
left_elbow_angle = calc_angle(left_shoulder, left_elbow, left_wrist)
right_elbow_angle = calc_angle(right_shoulder, right_elbow, right_wrist)
# down state
if left_elbow_angle > 160 and right_elbow_angle > 160:
if not range_flag:
feedback = 'Did not curl completely.'
else:
feedback = 'Good rep!'
state = 'Down'
# not fully curled
elif (left_elbow_angle > 50 and right_elbow_angle > 50) and state == 'Down':
range_flag = False
feedback = ''
# up state
elif (left_elbow_angle < 30 and right_elbow_angle < 30) and state == 'Down':
state = 'Up'
feedback = ''
range_flag = True
counter += 1
except:
pass
# Recognize squat function
def recognise_squat(detection):
global counter
global state
global feedback
try:
landmarks = detection.pose_landmarks.landmark
# GET COORDINATES
left_hip = [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y]
left_knee = [landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y]
left_heel = [landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].y]
right_hip = [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y]
right_knee = [landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y]
right_heel = [landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].y]
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
right_shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
left = calc_angle(left_hip, left_knee, left_heel)
right = calc_angle(right_hip, right_knee, right_heel)
#POSE CHECKING 1: Knees bending inwards
shoulder_dist = left_shoulder[0] - right_shoulder[0]
knee_dist = left_knee[0] - right_knee[0]
if shoulder_dist - knee_dist > 0.04:
feedback = 'Open up your knees further apart to shoulder width!'
else:
feedback = ''
# standing up
if left > 170 and right > 170:
state = "Up"
if left < 165 and right < 165:
feedback = 'Almost there... lower until height of hips!'
if left < 140 and right < 140 and state == "Up":
state = "Down"
counter += 1
if state == "Down":
feedback = 'Good rep!'
except:
pass
# Recognize situp function
def recognise_situp(detection):
global counter
global state
global feedback
global range_flag
global halfway
try:
landmarks = detection.pose_landmarks.landmark
left_hip = [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y]
left_knee = [landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y]
left_heel = [landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].y]
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
# CALCULATE ANGLES
angle_knee = calc_angle(left_hip, left_knee, left_heel)
angle_body = calc_angle(left_shoulder, left_hip, left_knee)
if (angle_body < 80 and angle_body > 50) and state == "Down": #Half-way there (Used for checking bad situps)
halfway = True
if angle_body < 40 and state == "Down": #Complete situp
state = "Up"
range_flag = True
if angle_body > 90 and angle_knee < 60: #Resting position;to check if situp was done properly
state = "Down"
if halfway: #Check if a rep was attempted
if range_flag: #Check if a proper rep was performed
counter += 1
feedback = "Good repetition!"
else:
feedback = "Did not perform sit up completely."
range_flag = False #Reset vars
halfway = False
if angle_knee > 70: #Triggers anytime the legs are not tucked in
feedback = "Keep legs tucked in closer"
except:
pass
#Recognize pushup function
def recognize_pushup(detection):
global counter
global state
global feedback
global range_flag
try:
landmarks = detection.pose_landmarks.landmark
#Get coordinates
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
left_elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
left_wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
right_shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
right_elbow = [landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y]
right_wrist = [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
left_elbow_angle = calc_angle(left_shoulder, left_elbow, left_wrist)
right_elbow_angle = calc_angle(right_shoulder, right_elbow, right_wrist)
#Down state
if left_elbow_angle < 90 and right_elbow_angle < 90:
range_flag = True
state = "Down"
feedback = "Push upwards!"
elif left_elbow_angle >= 100 and right_elbow_angle >= 100 and state == "Down":
range_flag = False
feedback = "Go Lower!."
#Up state
elif left_elbow_angle > 160 and right_elbow_angle > 160 and state == "Down":
if range_flag:
state = "Up"
counter += 1
feedback = "Good push-up!"
else:
feedback = "Did not push up completely."
else:
feedback = "keep going!"
except:
pass
def reset_exercise_state():
global counter
global state
global feedback
global range_flag
global halfway
counter = 0
state = 'Down'
feedback = ''
range_flag = True
# initialise variables
counter = 0
state = 'Down'
range_flag = True
halfway = False
feedback = ''
# Start video capture
def generate_frames(user_choice):
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# Mediapipe Pose model instance
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
success, frame = cap.read()
if not success:
break
# Mirror frame
frame = cv2.flip(frame, 1)
# Recolor image from BGR to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Pose detection
detection = pose.process(image)
# Recolor image from RGB back to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image.flags.writeable = True
# Render detections
mp_drawing.draw_landmarks(image, detection.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2))
# Recognise particular exercise based on user input
if user_choice == 1:
recognise_squat(detection)
elif user_choice == 2:
recognise_curl(detection)
elif user_choice == 3:
recognise_situp(detection)
elif user_choice == 4:
recognize_pushup(detection)
# Status box setup
cv2.rectangle(image, (0,0), (int(cap.get(3)), 60), (0,0,0), -1)
cv2.putText(image, "REPS:", (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1, cv2.LINE_AA) # font, size, color, line width, line type
cv2.putText(image, "STATE:", (150, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(image, "FEEDBACK:", (300, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(image, str(counter), (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(image, state, (150, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(image, feedback, (300, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255,255,255), 1, cv2.LINE_AA)
ret, buffer = cv2.imencode('.jpg', image)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
cap.release()
cv2.destroyAllWindows()