File size: 4,299 Bytes
1e216d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import mediapipe as mp
import numpy as np
import gradio as gr

# Initialize MediaPipe Pose and Drawing utilities
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

# Utility function to calculate angles
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])
    angle = np.abs(radians * 180.0 / np.pi)
    return angle

# Function to provide burpees feedback based on landmarks
def check_burpees_feedback(landmarks):
    shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
                landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
    elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x,
             landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
    wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
             landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
    hip = [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x,
           landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y]
    knee = [landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x,
            landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y]
    ankle = [landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x,
             landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y]
    
    squat_angle = calculate_angle(hip, knee, ankle)
    pushup_angle = calculate_angle(shoulder, elbow, wrist)
    jump_angle = calculate_angle(knee, hip, shoulder)
    
    feedback = "Correct Burpee"
    accuracy = 100
    if squat_angle < 90:
        feedback = "Squat too shallow - Go deeper!"
        accuracy -= 30
    elif squat_angle > 140:
        feedback = "Squat too deep - Reduce depth!"
        accuracy -= 30
    if pushup_angle < 70:
        feedback = "Push-up too low - Elbows too bent!"
        accuracy -= 30
    elif pushup_angle > 120:
        feedback = "Push-up too high - Keep arms at 90 degrees!"
        accuracy -= 30
    if jump_angle < 130:
        feedback = "Jump not high enough - Extend fully!"
        accuracy -= 30
    accuracy = max(0, min(accuracy, 100))
    return feedback, accuracy

# Function to draw an accuracy bar on the frame
def draw_accuracy_bar(image, accuracy):
    bar_x, bar_y = 50, 400
    bar_width, bar_height = 200, 20
    fill_width = int((accuracy / 100) * bar_width)
    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), (0, 255, 0), -1)
    cv2.putText(image, f"Accuracy: {accuracy}%", (bar_x, bar_y - 10),
                cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 2)

# Main function to analyze burpees form in a video
def analyze_burpees(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
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    output_video = "output_burpees.mp4"
    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_burpees_feedback(landmarks)
            draw_accuracy_bar(image, accuracy)
            color = (0, 255, 0) if "Correct" in feedback else (0, 0, 255)
            cv2.putText(image, feedback, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, color, 3)
        
        out.write(image)
    
    cap.release()
    out.release()
    return output_video

# Gradio Interface for Burpees Analysis
gr.Interface(
    fn=analyze_burpees,
    inputs=gr.Video(),
    outputs=gr.Video(),
    title="Burpees Form Analyzer",
    description="Upload a video of your burpees, and get feedback on your form!"
).launch()