-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExerciseCondition.py
191 lines (159 loc) · 8.42 KB
/
ExerciseCondition.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
import cv2
import numpy as np
import PoseModule as pm
class ExerciseType(pm.poseDetector):
def __init__(self, width, height, fps):
super().__init__()
self.width = width
self.height = height
self.fps = fps
self.count = 0
self.color = (255, 0, 255)
self.dir = 0
self.Up = False
self.Down = False
self.check = False
self.Landmarks = {'nose': 0, 'left_eye_inner': 1, 'left_eye': 2, 'left_eye_outer': 3,
'right_eye_inner': 4, 'right_eye': 5, 'right_eye_outer': 6, 'left_ear': 7,
'right_ear': 8, 'mouth_left': 9, 'mouth_right': 10, 'left_shoulder': 11,
'right_shoulder': 12, 'left_elbow': 13, 'right_elbow': 14, 'left_wrist': 15,
'right_wrist': 16, 'left_pinky': 17, 'right_pinky': 18, 'left_index': 19,
'right_index': 20, 'left_thumb': 21, 'right_thumb': 22, 'left_hip': 23,
'right_hip': 24, 'left_knee': 25, 'right_knee': 26, 'left_ankle': 27,
'right_ankle': 28, 'left_heel': 29, 'right_heel': 30, 'left_foot_index': 31,
'right_foot_index': 32}
def SelectExerciseMode(self, img, Mode, draw=True):
if Mode == 'barbellCurl':
p1, p2, p3 = self.Landmarks['left_shoulder'], self.Landmarks['left_elbow'],\
self.Landmarks['left_wrist']
Angle = self.findAngle(img, p1, p2, p3, draw)
ref_low = 225
ref_high = 305
per = np.interp(Angle, (ref_low, ref_high), (0, 100))
bar = np.interp(Angle, (ref_low + 10, ref_high), (self.height-100, 100))
if per >= 60:
self.Up = True
if per == 100:
self.color = (0, 255, 0)
if self.dir == 0:
self.count += .5
self.dir = 1
self.check = True
if (int(per) > 80) & (int(per) < 100) & (self.check == False):
cv2.putText(img, 'UP! UP! UP!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
if per == 0:
self.color = (0, 255, 0)
if self.dir == 1:
self.count += .5
self.dir = 0
self.Up = False
self.check = False
elif (self.Up == True) & (self.dir == 0):
self.check = False
# cv2.putText(img, 'MORE Arm UP!!!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
# (0, 0, 255), 3)
elif Mode == 'pushup':
p1, p2, p3 = self.Landmarks['left_shoulder'], self.Landmarks['left_elbow'],\
self.Landmarks['left_wrist']
p4, p5, p6 = self.Landmarks['right_shoulder'], self.Landmarks['right_elbow'],\
self.Landmarks['right_wrist']
p7, p8, p9 = self.Landmarks['left_shoulder'], self.Landmarks['left_hip'], \
self.Landmarks['left_knee']
p10, p11, p12 = self.Landmarks['left_hip'], self.Landmarks['left_knee'], \
self.Landmarks['left_ankle']
p13, p14, p15 = self.Landmarks['right_hip'], self.Landmarks['right_knee'], \
self.Landmarks['right_ankle']
Angle_arm_L = self.findAngle(img, p1, p2, p3, draw)
Angle_arm_R = self.findAngle(img, p4, p5, p6, draw)
ref_low = 225
ref_high = 250
per = np.interp(Angle_arm_L, (ref_low, ref_high), (0, 100))
per_R = np.interp(Angle_arm_R, (ref_low, ref_high), (0, 100))
bar = np.interp(Angle_arm_L, (ref_low + 10, ref_high), (self.height-100, 100))
Angle_back = self.findAngle(img, p7, p8, p9, draw=False)
Angle_leg_left = self.findAngle(img, p10, p11, p12, draw=True)
Angle_leg_right = self.findAngle(img, p13, p14, p15, draw=True)
if (int(Angle_leg_left) < 180) & (int(Angle_leg_left) > 160) &\
(int(Angle_leg_right) < 180) & (int(Angle_leg_right) > 160):
if (per >= 70) & (per_R >= 70):
self.Up = True
if per == 100:
self.color = (0, 255, 0)
# & (int(Angle_back) < 210) & (int(Angle_back) > 170):
if (self.dir == 0):
self.count += .5
self.dir = 1
self.check = True
if (int(per) > 80) & (int(per) < 100) & (self.check == False):
cv2.putText(img, 'MORE! MORE! MORE!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
if (per == 0) & (per_R == 0):
self.color = (0, 255, 0)
if (self.dir == 1):
self.count += .5
self.dir = 0
self.Up = False
self.check = False
elif (self.Up == True) & (self.dir == 0):
self.check = False
cv2.putText(img, 'BEND YOUR ARMS MORE !!!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
else:
per = 0
bar = self.height-100
self.Draw(img, per, bar)
elif Mode == 'squat': # side
p1, p2, p3 = self.Landmarks['left_hip'], self.Landmarks['left_knee'], \
self.Landmarks['left_ankle']
p4, p5, p6 = self.Landmarks['right_hip'], self.Landmarks['right_knee'], \
self.Landmarks['right_ankle']
p7, p8, p9 = self.Landmarks['left_ear'], self.Landmarks['left_shoulder'], \
self.Landmarks['left_hip']
Angle_leg_left = self.findAngle(img, p1, p2, p3, draw)
Angle_leg_right = self.findAngle(img, p4, p5, p6, draw)
Angle_left_side = self.findAngle(img, p7, p8, p9, draw)
ref_low = 90
ref_high = 150
per = np.interp(Angle_leg_left, (ref_low, ref_high), (0, 100))
bar = np.interp(Angle_leg_left, (ref_low + 10, ref_high), (100, self.height-100))
if (int(Angle_left_side) < 220) & (int(Angle_left_side) > 170):
if per <= 40:
self.Down = True
if per == 0:
self.color = (0, 255, 0)
if self.dir == 0:
self.count += .5
self.dir = 1
# self.check = True
if (int(per) < 30) & (int(per) > 0) & (self.check == False): # 30 0
cv2.putText(img, 'DOWN! DOWN! DOWN!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
if per == 100:
self.color = (0, 255, 0)
if self.dir == 1:
self.count += .5
self.dir = 0
self.Down = False
self.check = False
elif (self.Down == True) & (self.dir == 0):
self.check = False
cv2.putText(img, 'MORE LEGS DOWN!!!', (250, 50), cv2.FONT_HERSHEY_PLAIN, 3,
(0, 0, 255), 3)
#
per = 100 - per
else:
per = 0
bar = self.height-100
self.Draw(img, per, bar)
def Draw(self, img, per, bar):
cv2.rectangle(img, (0, 0), (150, 200), (0, 255, 0), cv2.FILLED)
cv2.putText(img, str(int(self.count)), (20, 150), cv2.FONT_HERSHEY_PLAIN, 10,
(255, 0, 0), 10)
cv2.rectangle(img, (self.width -100, 100), (self.width - 50, self.height-100),
self.color, 3)
cv2.rectangle(img, (self.width -100, int(bar)), (self.width - 50, self.height-100),
self.color, cv2.FILLED)
cv2.putText(img, f'{int(per)} %', (1100, 80), cv2.FONT_HERSHEY_PLAIN, 3,
self.color, 3)
return self.count