-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera4.py
349 lines (286 loc) · 11.7 KB
/
camera4.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
import cv2
import numpy as np
import time
import tkinter as tk
import PoseModule as pm
from collections import deque
# Initialize video capture and pose detector
cap = cv2.VideoCapture(0)
detector = pm.poseDetector(detectionCon=0.7, trackCon=0.7) # Increased confidence thresholds
class ExerciseTracker:
def __init__(self):
self.angle_buffer = deque(maxlen=5) # Smoothing buffer for angles
self.prev_angles = {} # Store previous angles for smoothing
self.rep_threshold = 0.3 # Threshold for rep counting
def smooth_angle(self, angle):
"""Apply smoothing to angle measurements"""
self.angle_buffer.append(angle)
return np.mean(self.angle_buffer)
def calculate_bilateral_angles(self, img, lmlist, points):
"""Calculate angles for both left and right side with smoothing"""
angles = {}
for side in ['left', 'right']:
p1, p2, p3 = points[side]
try:
angle = detector.finfAngle(img, p1, p2, p3)
# Apply smoothing
if f'{side}_angle' in self.prev_angles:
angle = 0.7 * angle + 0.3 * self.prev_angles[f'{side}_angle']
self.prev_angles[f'{side}_angle'] = angle
angles[side] = angle
except:
angles[side] = self.prev_angles.get(f'{side}_angle', 0)
return angles
def exercise_logic(exercise_func, total_rep=10):
count = 0
dir = 0
frame_count = 0
start_time = time.time()
tracker = ExerciseTracker()
while count < total_rep:
success, img = cap.read()
if not success:
continue
img = cv2.resize(img, (1288, 720))
img = detector.findPose(img, False)
lmlist = detector.findPosition(img, False)
if len(lmlist) != 0:
count, dir = exercise_func(img, lmlist, count, dir, tracker)
# Display rep count and timer
cv2.putText(img, f'Reps: {int(count)}', (50, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
cv2.putText(img, f'Time: {int(time.time() - start_time)}s', (50, 150), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
cv2.imshow("Exercise Detection", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def push_up_logic(img, lmlist, count, dir, tracker):
# Points for both arms (shoulder, elbow, wrist)
arm_points = {
'left': (11, 13, 15),
'right': (12, 14, 16)
}
# Calculate angles for both arms
arm_angles = tracker.calculate_bilateral_angles(img, lmlist, arm_points)
# Body alignment points (shoulder, hip, knee)
body_points = {
'left': (11, 23, 25),
'right': (12, 24, 26)
}
body_angles = tracker.calculate_bilateral_angles(img, lmlist, body_points)
# Calculate percentage for both arms
left_per = np.interp(arm_angles['left'], (85, 165), (0, 100))
right_per = np.interp(arm_angles['right'], (85, 165), (0, 100))
# Check body alignment
body_aligned = all(angle > 160 for angle in body_angles.values())
# Average arm percentage for progress bar
avg_per = (left_per + right_per) / 2
# Count rep only if both arms are in correct position and body is aligned
if avg_per > 95 and body_aligned and dir == 0:
count += 0.5
dir = 1
if avg_per < 5 and body_aligned and dir == 1:
count += 0.5
dir = 0
# Visual feedback
progress_bar = np.interp(avg_per, (0, 100), (0, img.shape[1]))
cv2.rectangle(img, (50, 50), (int(progress_bar), 100), (0, 255, 0), -1)
# Form feedback
if not body_aligned:
cv2.putText(img, "Keep body straight!", (50, 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if abs(left_per - right_per) > 15:
cv2.putText(img, "Keep arms even!", (50, 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return count, dir
def squats_logic(img, lmlist, count, dir, tracker):
# Points for both legs (hip, knee, ankle)
leg_points = {
'left': (23, 25, 27),
'right': (24, 26, 28)
}
# Calculate angles for both legs
leg_angles = tracker.calculate_bilateral_angles(img, lmlist, leg_points)
# Hip points (shoulder, hip, knee)
hip_points = {
'left': (11, 23, 25),
'right': (12, 24, 26)
}
hip_angles = tracker.calculate_bilateral_angles(img, lmlist, hip_points)
# Calculate percentages
left_per = np.interp(leg_angles['left'], (90, 170), (0, 100))
right_per = np.interp(leg_angles['right'], (90, 170), (0, 100))
avg_per = (left_per + right_per) / 2
# Check proper form
proper_depth = all(90 <= angle <= 110 for angle in leg_angles.values())
proper_hip_hinge = all(angle >= 90 for angle in hip_angles.values())
# Count rep
if avg_per > 95 and dir == 0:
count += 0.5
dir = 1
if proper_depth and proper_hip_hinge and dir == 1:
count += 0.5
dir = 0
# Visual feedback
progress_bar = np.interp(avg_per, (0, 100), (0, img.shape[1]))
cv2.rectangle(img, (50, 50), (int(progress_bar), 100), (0, 255, 0), -1)
# Form feedback
if abs(left_per - right_per) > 15:
cv2.putText(img, "Keep weight even!", (50, 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if not proper_hip_hinge:
cv2.putText(img, "Hinge at hips!", (50, 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return count, dir
def jumping_jacks_logic(img, lmlist, count, dir, tracker):
# Points for arms and legs
arm_points = {
'left': (11, 13, 15),
'right': (12, 14, 16)
}
leg_points = {
'left': (23, 25, 27),
'right': (24, 26, 28)
}
# Calculate angles
arm_angles = tracker.calculate_bilateral_angles(img, lmlist, arm_points)
leg_angles = tracker.calculate_bilateral_angles(img, lmlist, leg_points)
# Calculate percentages
arm_left_per = np.interp(arm_angles['left'], (30, 170), (0, 100))
arm_right_per = np.interp(arm_angles['right'], (30, 170), (0, 100))
leg_left_per = np.interp(leg_angles['left'], (10, 45), (0, 100))
leg_right_per = np.interp(leg_angles['right'], (10, 45), (0, 100))
avg_arm_per = (arm_left_per + arm_right_per) / 2
avg_leg_per = (leg_left_per + leg_right_per) / 2
# Count rep
if avg_arm_per > 95 and avg_leg_per > 95 and dir == 0:
count += 0.5
dir = 1
if avg_arm_per < 5 and avg_leg_per < 5 and dir == 1:
count += 0.5
dir = 0
# Visual feedback
progress_bar = np.interp(avg_arm_per, (0, 100), (0, img.shape[1]))
cv2.rectangle(img, (50, 50), (int(progress_bar), 100), (0, 255, 0), -1)
# Form feedback
if abs(arm_left_per - arm_right_per) > 15:
cv2.putText(img, "Keep arms even!", (50, 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if abs(leg_left_per - leg_right_per) > 15:
cv2.putText(img, "Keep legs even!", (50, 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return count, dir
def sit_up_logic(img, lmlist, count, dir, tracker):
# Points for torso and legs
torso_points = {
'left': (11, 23, 25),
'right': (12, 24, 26)
}
leg_points = {
'left': (23, 25, 27),
'right': (24, 26, 28)
}
# Calculate angles
torso_angles = tracker.calculate_bilateral_angles(img, lmlist, torso_points)
leg_angles = tracker.calculate_bilateral_angles(img, lmlist, leg_points)
# Calculate percentage
left_per = np.interp(torso_angles['left'], (70, 180), (0, 100))
right_per = np.interp(torso_angles['right'], (70, 180), (0, 100))
avg_per = (left_per + right_per) / 2
# Check proper form
proper_leg_position = all(40 <= angle <= 50 for angle in leg_angles.values())
# Count rep
if avg_per < 5 and proper_leg_position and dir == 0:
count += 0.5
dir = 1
if avg_per > 95 and proper_leg_position and dir == 1:
count += 0.5
dir = 0
# Visual feedback
progress_bar = np.interp(avg_per, (0, 100), (0, img.shape[1]))
cv2.rectangle(img, (50, 50), (int(progress_bar), 100), (0, 255, 0), -1)
# Form feedback
if not proper_leg_position:
cv2.putText(img, "Keep legs at 45 degrees!", (50, 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if abs(left_per - right_per) > 15:
cv2.putText(img, "Keep torso centered!", (50, 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return count, dir
def lunges_logic(img, lmlist, count, dir, tracker):
# Points for front and back legs
leg_points = {
'left': (23, 25, 27),
'right': (24, 26, 28)
}
hip_points = {
'left': (11, 23, 25),
'right': (12, 24, 26)
}
# Calculate angles
leg_angles = tracker.calculate_bilateral_angles(img, lmlist, leg_points)
hip_angles = tracker.calculate_bilateral_angles(img, lmlist, hip_points)
# Calculate percentage
left_per = np.interp(leg_angles['left'], (85, 170), (0, 100))
right_per = np.interp(leg_angles['right'], (85, 170), (0, 100))
# Check proper form
proper_front_knee = any(85 <= angle <= 95 for angle in leg_angles.values())
proper_back_knee = any(85 <= angle <= 100 for angle in leg_angles.values())
proper_torso = all(angle >= 160 for angle in hip_angles.values())
# Count rep
if proper_front_knee and proper_back_knee and proper_torso and dir == 0:
count += 0.5
dir = 1
if all(per > 95 for per in [left_per, right_per]) and dir == 1:
count += 0.5
dir = 0
# Visual feedback
progress_bar = np.interp((left_per + right_per) / 2, (0, 100), (0, img.shape[1]))
cv2.rectangle(img, (50, 50), (int(progress_bar), 100), (0, 255, 0), -1)
# Form feedback
if not proper_torso:
cv2.putText(img, "Keep torso upright!", (50, 200), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
if not (proper_front_knee and proper_back_knee):
cv2.putText(img, "Check knee angles!", (50, 230), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
return count, dir
# Create the GUI window
root = tk.Tk()
root.title("AI Fitness Trainer")
root.geometry("300x400")
# Style the window
root.configure(bg='#f0f0f0')
title_label = tk.Label(root, text="AI Fitness Trainer", font=("Arial", 18, "bold"), bg='#f0f0f0')
title_label.pack(pady=20)
# Exercise selection
exercise_var = tk.StringVar()
exercise_var.set("Push-ups")
exercises = {
"Push-ups": push_up_logic,
"Squats": squats_logic,
"Jumping Jacks": jumping_jacks_logic,
"Sit-ups": sit_up_logic,
"Lunges": lunges_logic
}
# Create styled option menu
exercise_menu = tk.OptionMenu(root, exercise_var, *exercises.keys())
exercise_menu.configure(width=20)
exercise_menu.pack(pady=10)
# Function to start exercise
def start_exercise():
selected_exercise = exercise_var.get()
root.iconify() # Minimize window during exercise
exercise_logic(exercises[selected_exercise])
root.deiconify() # Restore window after exercise
# Create styled button
start_button = tk.Button(root, text="Start Exercise",
command=start_exercise,
font=("Arial", 12),
bg='#4CAF50',
fg='white',
width=15,
height=2)
start_button.pack(pady=20)
# Add quit button
quit_button = tk.Button(root, text="Quit",
command=root.quit,
font=("Arial", 12),
bg='#f44336',
fg='white',
width=15,
height=1)
quit_button.pack(pady=10)
# Run the application
root.mainloop()
# Clean up
cap.release()
cv2.destroyAllWindows()