CoolFace
Apppublic

VardhanRaj369/AI_Workout_Tracker

sourceHugging Facemitupdated 10mo agoView on Hugging Face
0likes
exercise_module.py137 linesDownload Raw Back to root
1import math
2
3def calculate_angle(a, b, c):
4    """Returns angle between 3 points."""
5    ax, ay = a
6    bx, by = b
7    cx, cy = c
8
9    ab = (ax - bx, ay - by)
10    cb = (cx - bx, cy - by)
11
12    dot = ab[0]*cb[0] + ab[1]*cb[1]
13    mag_ab = math.sqrt(ab[0]**2 + ab[1]**2)
14    mag_cb = math.sqrt(cb[0]**2 + cb[1]**2)
15
16    if mag_ab * mag_cb == 0:
17        return 0
18
19    cos = dot / (mag_ab * mag_cb)
20    cos = min(1.0, max(-1.0, cos))
21
22    return math.degrees(math.acos(cos))
23
24
25class ExerciseCounter:
26    def __init__(self, exercise):
27        self.exercise = exercise
28        self.count = 0
29        self.stage = None
30        self.feedback = ""
31
32    def update(self, lm):
33
34        if not lm:
35            return self.count, "Move into frame", False
36
37        rep_flag = False
38
39        # Extract major joints (RIGHT SIDE)
40        shoulder = (lm[12].x, lm[12].y)
41        elbow    = (lm[14].x, lm[14].y)
42        wrist    = (lm[16].x, lm[16].y)
43
44        hip      = (lm[24].x, lm[24].y)
45        knee     = (lm[26].x, lm[26].y)
46        ankle    = (lm[28].x, lm[28].y)
47
48        # Main angles
49        elbow_angle = calculate_angle(shoulder, elbow, wrist)
50        knee_angle  = calculate_angle(hip, knee, ankle)
51        shoulder_angle = calculate_angle(elbow, shoulder, hip)
52
53        # ------------------------------------------------
54        # 1️⃣ SQUATS
55        # ------------------------------------------------
56        if self.exercise == "Squats":
57            # DOWN position
58            if knee_angle < 90:
59                self.stage = "down"
60                self.feedback = "Go Up"
61
62            # UP position → COUNT REP
63            if knee_angle > 160 and self.stage == "down":
64                self.stage = "up"
65                self.count += 1
66                rep_flag = True
67                self.feedback = "Perfect Squat!"
68
69        # ------------------------------------------------
70        # 2️⃣ PUSH-UPS
71        # ------------------------------------------------
72        elif self.exercise == "Push-ups":
73            # DOWN
74            if elbow_angle < 90:
75                self.stage = "down"
76                self.feedback = "Push Up!"
77
78            # UP → count
79            if elbow_angle > 160 and self.stage == "down":
80                self.stage = "up"
81                self.count += 1
82                rep_flag = True
83                self.feedback = "Good Push-up!"
84
85        # ------------------------------------------------
86        # 3️⃣ BICEP CURLS
87        # ------------------------------------------------
88        elif self.exercise == "Bicep Curls":
89
90            # CURL UP
91            if elbow_angle < 40:
92                self.stage = "up"
93                self.feedback = "Lower Down"
94
95            # CURL DOWN → count
96            if elbow_angle > 160 and self.stage == "up":
97                self.stage = "down"
98                self.count += 1
99                rep_flag = True
100                self.feedback = "Great Curl!"
101
102        # ------------------------------------------------
103        # 4️⃣ SHOULDER PRESS
104        # ------------------------------------------------
105        elif self.exercise == "Shoulder Press":
106
107            # DOWN = hands at shoulder level
108            if shoulder_angle < 40:
109                self.stage = "down"
110                self.feedback = "Press Up"
111
112            # UP → count
113            if shoulder_angle > 80 and self.stage == "down":
114                self.stage = "up"
115                self.count += 1
116                rep_flag = True
117                self.feedback = "Nice Press!"
118
119        # ------------------------------------------------
120        # 5️⃣ TRICEP DIPS
121        # ------------------------------------------------
122        elif self.exercise == "Tricep Dips":
123
124            # DIP DOWN
125            if elbow_angle < 80:
126                self.stage = "down"
127                self.feedback = "Push Up"
128
129            # DIP UP → count
130            if elbow_angle > 160 and self.stage == "down":
131                self.stage = "up"
132                self.count += 1
133                rep_flag = True
134                self.feedback = "Great Dip!"
135
136        return self.count, self.feedback, rep_flag
137