Spaces:
Sleeping
Sleeping
| import cv2 | |
| import mediapipe as mp | |
| import numpy as np | |
| import gradio as gr | |
| mp_pose = mp.solutions.pose | |
| pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) | |
| mp_drawing = mp.solutions.drawing_utils | |
| def calculate_angle(a, b, c): | |
| a, b, c = np.array(a), np.array(b), np.array(c) | |
| radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0]) | |
| return np.abs(radians * 180.0 / np.pi) | |
| def check_pullup_feedback(landmarks): | |
| shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, | |
| landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y] | |
| elbow = [landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, | |
| landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y] | |
| wrist = [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, | |
| landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y] | |
| angle = calculate_angle(shoulder, elbow, wrist) | |
| target_angle = 70 | |
| tolerance = 20 | |
| accuracy = max(0, min(100, (1 - abs(angle - target_angle) / tolerance) * 100)) | |
| if angle > (target_angle + tolerance / 2): | |
| feedback = ("Incomplete Pull-up - Your elbows remain too extended. Engage your back muscles to pull higher.") | |
| elif angle < (target_angle - tolerance / 2): | |
| feedback = ("Over-bending - Your elbows are too flexed. Try to control your descent.") | |
| else: | |
| feedback = "Correct Pull-up - Great form!" | |
| return feedback, int(accuracy) | |
| def draw_accuracy_bar(image, accuracy): | |
| bar_x, bar_y = 50, image.shape[0] - 50 | |
| bar_width, bar_height = 200, 20 | |
| fill_width = int((accuracy / 100) * bar_width) | |
| color = (0, 255, 0) if accuracy >= 80 else (0, 0, 255) if accuracy < 50 else (0, 255, 255) | |
| cv2.rectangle(image, (bar_x, bar_y), (bar_x + bar_width, bar_y + bar_height), (200,200,200), 2) | |
| cv2.rectangle(image, (bar_x, bar_y), (bar_x + fill_width, bar_y + bar_height), color, -1) | |
| cv2.putText(image, f"Accuracy: {accuracy}%", (bar_x, bar_y-10), | |
| cv2.FONT_HERSHEY_DUPLEX, 0.6, (255,255,255), 2) | |
| def analyze_pullups(video_path): | |
| cap = cv2.VideoCapture(video_path) | |
| frame_width, frame_height = int(cap.get(3)), int(cap.get(4)) | |
| fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 30 | |
| output_video = "output_pullups.mp4" | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height)) | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| results = pose.process(image) | |
| image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
| if results.pose_landmarks: | |
| mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) | |
| landmarks = results.pose_landmarks.landmark | |
| feedback, accuracy = check_pullup_feedback(landmarks) | |
| draw_accuracy_bar(image, accuracy) | |
| text_color = (0, 255, 0) if "Correct" in feedback else (0, 0, 255) | |
| cv2.putText(image, feedback, (50, 50), | |
| cv2.FONT_HERSHEY_COMPLEX, 1, text_color, 3) | |
| out.write(image) | |
| cap.release() | |
| out.release() | |
| return output_video | |
| gr.Interface( | |
| fn=analyze_pullups, | |
| inputs=gr.Video(), | |
| outputs=gr.Video(), | |
| title="Pull-ups Form Analyzer", | |
| description="Upload a video of your pull-ups and receive form feedback!" | |
| ).launch() | |