smurar commited on
Commit
dc5fca9
·
verified ·
1 Parent(s): 79ec8d8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # Initialize MediaPipe Pose and drawing utils.
7
+ mp_pose = mp.solutions.pose
8
+ pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
9
+ mp_drawing = mp.solutions.drawing_utils
10
+
11
+ # Calculate angle between three points.
12
+ def calculate_angle(a, b, c):
13
+ a, b, c = np.array(a), np.array(b), np.array(c)
14
+ radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
15
+ angle = np.abs(radians * 180.0 / np.pi)
16
+ return angle
17
+
18
+ # Process the video and overlay lunge feedback.
19
+ def analyze_lunges(video_path):
20
+ cap = cv2.VideoCapture(video_path)
21
+ frame_width = int(cap.get(3))
22
+ frame_height = int(cap.get(4))
23
+ fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 30
24
+
25
+ output_video = "output_lunges.mp4"
26
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
27
+ out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height))
28
+
29
+ # Check lunge form based on leg angles.
30
+ def check_lunge_feedback(landmarks):
31
+ left_hip = [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x,
32
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y]
33
+ left_knee = [landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x,
34
+ landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y]
35
+ left_ankle = [landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x,
36
+ landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y]
37
+ right_hip = [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x,
38
+ landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y]
39
+ right_knee = [landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x,
40
+ landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y]
41
+ right_ankle = [landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x,
42
+ landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y]
43
+ left_leg_angle = calculate_angle(left_hip, left_knee, left_ankle)
44
+ right_leg_angle = calculate_angle(right_hip, right_knee, right_ankle)
45
+ left_accuracy = max(0, min(100, (1 - abs(left_leg_angle - 90) / 30) * 100))
46
+ right_accuracy = max(0, min(100, (1 - abs(right_leg_angle - 90) / 30) * 100))
47
+ feedback = "Correct Lunge"
48
+ if left_leg_angle < 70 or right_leg_angle < 70:
49
+ feedback = "Incorrect Lunge - Deep enough"
50
+ elif left_leg_angle > 110 or right_leg_angle > 110:
51
+ feedback = "Incorrect Lunge - Lower hips"
52
+ return feedback, left_accuracy, right_accuracy
53
+
54
+ # Draw separate accuracy bars for the left and right legs.
55
+ def draw_accuracy_bar(image, left_accuracy, right_accuracy):
56
+ bar_x, bar_y = 50, 400
57
+ bar_width, bar_height = 200, 20
58
+ left_fill_width = int((left_accuracy / 100) * bar_width)
59
+ cv2.rectangle(image, (bar_x, bar_y), (bar_x + bar_width, bar_y + bar_height), (200, 200, 200), 2)
60
+ cv2.rectangle(image, (bar_x, bar_y), (bar_x + left_fill_width, bar_y + bar_height), (0, 255, 0), -1)
61
+ cv2.putText(image, f"Left Leg Accuracy: {left_accuracy}%", (bar_x, bar_y - 10),
62
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
63
+ right_bar_y = bar_y + 50
64
+ right_fill_width = int((right_accuracy / 100) * bar_width)
65
+ cv2.rectangle(image, (bar_x, right_bar_y), (bar_x + bar_width, right_bar_y + bar_height), (200, 200, 200), 2)
66
+ cv2.rectangle(image, (bar_x, right_bar_y), (bar_x + right_fill_width, right_bar_y + bar_height), (0, 255, 0), -1)
67
+ cv2.putText(image, f"Right Leg Accuracy: {right_accuracy}%", (bar_x, right_bar_y - 10),
68
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
69
+
70
+ while cap.isOpened():
71
+ ret, frame = cap.read()
72
+ if not ret:
73
+ break
74
+ # Process frame with MediaPipe.
75
+ image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
76
+ results = pose.process(image)
77
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
78
+ if results.pose_landmarks:
79
+ mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
80
+ feedback, left_accuracy, right_accuracy = check_lunge_feedback(results.pose_landmarks.landmark)
81
+ draw_accuracy_bar(image, left_accuracy, right_accuracy)
82
+ color = (0, 255, 0) if "Correct" in feedback else (0, 0, 255)
83
+ cv2.putText(image, feedback, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 3)
84
+ out.write(image)
85
+ cap.release()
86
+ out.release()
87
+ return output_video
88
+
89
+ # Gradio Interface for lunge analysis.
90
+ gr.Interface(
91
+ fn=analyze_lunges,
92
+ inputs=gr.Video(),
93
+ outputs=gr.Video(),
94
+ title="Lunge Form Analyzer",
95
+ description="Upload a video of your lunges, and get feedback on your form!",
96
+ ).launch()