Spaces:
Sleeping
Sleeping
HEMANTH commited on
Commit ·
62639da
1
Parent(s): 45a65b7
added media pipe folder and uploads
Browse files- workouts/bicepCurls.py +103 -0
workouts/bicepCurls.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import BasicPoseModule as pm_modified
|
| 4 |
+
import time
|
| 5 |
+
import mediapipe as mp
|
| 6 |
+
import os
|
| 7 |
+
from app import session
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Initialize Mediapipe Pose
|
| 12 |
+
mp_pose = mp.solutions.pose
|
| 13 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 14 |
+
|
| 15 |
+
def calculate_angle(a, b, c):
|
| 16 |
+
a = np.array(a) # First point
|
| 17 |
+
b = np.array(b) # Middle point
|
| 18 |
+
c = np.array(c) # Last point
|
| 19 |
+
|
| 20 |
+
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
|
| 21 |
+
angle = np.abs(radians * 180.0 / np.pi)
|
| 22 |
+
|
| 23 |
+
if angle > 180.0:
|
| 24 |
+
angle = 360 - angle
|
| 25 |
+
|
| 26 |
+
return angle
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def bicepcurls(video_path):
|
| 30 |
+
cap = cv2.VideoCapture(video_path)
|
| 31 |
+
count = 0
|
| 32 |
+
movement_dir = 0
|
| 33 |
+
|
| 34 |
+
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
| 35 |
+
while cap.isOpened():
|
| 36 |
+
ret, frame = cap.read()
|
| 37 |
+
if not ret:
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
frame = cv2.resize(frame, (1280, 720))
|
| 41 |
+
|
| 42 |
+
# Convert the frame to RGB
|
| 43 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 44 |
+
image.flags.writeable = False
|
| 45 |
+
|
| 46 |
+
# Process the image for pose detection
|
| 47 |
+
results = pose.process(image)
|
| 48 |
+
|
| 49 |
+
# Convert back to BGR for rendering
|
| 50 |
+
image.flags.writeable = True
|
| 51 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 52 |
+
|
| 53 |
+
if results.pose_landmarks:
|
| 54 |
+
landmarks = results.pose_landmarks.landmark
|
| 55 |
+
|
| 56 |
+
# Extract relevant joints
|
| 57 |
+
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
|
| 58 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
|
| 59 |
+
elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x,
|
| 60 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
|
| 61 |
+
wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
|
| 62 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
|
| 63 |
+
|
| 64 |
+
# Calculate elbow angle
|
| 65 |
+
elbow_angle = calculate_angle(shoulder, elbow, wrist)
|
| 66 |
+
|
| 67 |
+
# Determine movement direction and count reps
|
| 68 |
+
progress_percentage = np.interp(elbow_angle, (50, 160), (0, 100))
|
| 69 |
+
progress_bar = np.interp(elbow_angle, (50, 160), (650, 100))
|
| 70 |
+
|
| 71 |
+
color = (255, 0, 255)
|
| 72 |
+
if progress_percentage == 100:
|
| 73 |
+
color = (0, 255, 0)
|
| 74 |
+
if movement_dir == 0:
|
| 75 |
+
count += 0.5
|
| 76 |
+
movement_dir = 1
|
| 77 |
+
if progress_percentage == 0:
|
| 78 |
+
color = (0, 0, 255)
|
| 79 |
+
if movement_dir == 1:
|
| 80 |
+
count += 0.5
|
| 81 |
+
movement_dir = 0
|
| 82 |
+
|
| 83 |
+
# Draw Progress Bar
|
| 84 |
+
cv2.rectangle(frame, (1100, 100), (1175, 650), color, 3)
|
| 85 |
+
cv2.rectangle(frame, (1100, int(progress_bar)), (1175, 650), color, cv2.FILLED)
|
| 86 |
+
cv2.putText(frame, f'{int(progress_percentage)}%', (1100, 75), cv2.FONT_HERSHEY_PLAIN, 4, color, 4)
|
| 87 |
+
|
| 88 |
+
# Draw Counter
|
| 89 |
+
cv2.rectangle(frame, (0, 450), (250, 720), (0, 255, 0), cv2.FILLED)
|
| 90 |
+
cv2.putText(frame, str(int(count)), (45, 670), cv2.FONT_HERSHEY_PLAIN, 15, (255, 0, 0), 30)
|
| 91 |
+
|
| 92 |
+
# Draw landmarks
|
| 93 |
+
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 94 |
+
|
| 95 |
+
# Encode frame for streaming
|
| 96 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
| 97 |
+
frame = buffer.tobytes()
|
| 98 |
+
yield (b'--frame\r\n'
|
| 99 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
| 100 |
+
|
| 101 |
+
cap.release()
|
| 102 |
+
today_date = time.strftime("%Y-%m-%d")
|
| 103 |
+
session[today_date]['bicepscurls'] = count
|