-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
113 lines (96 loc) · 2.88 KB
/
script.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
from ultralytics import YOLO
import numpy as np
import cv2
def calculate_angle(a, b, c):
a = np.array(a) # Shoulder
b = np.array(b) # Elbow
c = np.array(c) # Wrist
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(
a[1] - b[1], a[0] - b[0]
)
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
def draw_keypoints(frame, keypoints):
colors = [
(0, 255, 0),
(0, 0, 255),
(255, 0, 0),
] # Colors for shoulder, elbow, wrist
labels = ["Shoulder", "Elbow", "Wrist"]
for idx, (point, color) in enumerate(zip(keypoints, colors)):
x, y = int(point[0]), int(point[1])
cv2.circle(frame, (x, y), 5, color, -1) # Draw the keypoint
cv2.putText(
frame, labels[idx], (x + 10, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1
)
model = YOLO("yolov8m-pose.pt")
cap = cv2.VideoCapture(0)
rep_count = 0
in_rep = False
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
if results and results[0].keypoints.xy.shape[1] >= 10:
keypoints = results[0].keypoints.xy[0].numpy()
# Note, this only corresponds to persons left shoulder, left elbow, left wrist
shoulder = keypoints[5]
elbow = keypoints[7]
wrist = keypoints[9]
angle = calculate_angle(shoulder, elbow, wrist)
draw_keypoints(frame, [shoulder, elbow, wrist])
feedback = "Good" if 75 <= angle <= 100 else "Needs Improvement"
# Determine if the arm is in a curl position
if angle <= 100:
if not in_rep:
in_rep = True
elif angle >= 160 and in_rep:
# Complete one full extension from the curl, count as one rep
rep_count += 1
in_rep = False # Reset for the next rep
cv2.rectangle(frame, (5, 10), (350, 120), (250, 250, 250), -1)
cv2.putText(
frame,
f"Elbow Angle: {angle:.2f} degrees",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(217, 111, 111),
2,
)
cv2.putText(
frame,
f"Feedback: {feedback}",
(10, 60),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(217, 111, 111),
2,
)
cv2.putText(
frame,
f"Reps: {rep_count}",
(10, 90),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(217, 111, 111),
2,
)
else:
cv2.putText(
frame,
"Insufficient keypoints",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 0, 255),
2,
)
cv2.imshow("Bicep Curl Analysis", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()