smurar commited on
Commit
870618f
·
verified ·
1 Parent(s): 0adc3d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # Initialize MediaPipe and drawing tools
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
+ def calculate_angle(a, b, c):
12
+ a, b, c = np.array(a), np.array(b), np.array(c)
13
+ radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
14
+ return np.abs(radians * 180.0 / np.pi)
15
+
16
+ def check_dips_feedback(landmarks):
17
+ shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
18
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
19
+ elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x,
20
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
21
+ wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
22
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
23
+
24
+ angle = calculate_angle(shoulder, elbow, wrist)
25
+ accuracy = max(0, min(100, (1 - abs(angle - 115) / 50) * 100))
26
+ feedback = "Correct Dip" if 70 <= angle <= 160 else "Incorrect Dip"
27
+ if angle > 160:
28
+ feedback += " - Lower Your Body More"
29
+ elif angle < 70:
30
+ feedback += " - Don't Go Too Low"
31
+ return feedback, int(accuracy)
32
+
33
+ def draw_accuracy_bar(image, accuracy):
34
+ bar_x, bar_y = 50, image.shape[0] - 50
35
+ bar_width, bar_height = 200, 20
36
+ fill_width = int((accuracy / 100) * bar_width)
37
+ color = (0, 255, 0) if accuracy >= 80 else (0, 0, 255) if accuracy < 50 else (0, 255, 255)
38
+ cv2.rectangle(image, (bar_x, bar_y), (bar_x+bar_width, bar_y+bar_height), (200,200,200), 2)
39
+ cv2.rectangle(image, (bar_x, bar_y), (bar_x+fill_width, bar_y+bar_height), color, -1)
40
+ cv2.putText(image, f"Accuracy: {accuracy}%", (bar_x, bar_y-10),
41
+ cv2.FONT_HERSHEY_DUPLEX, 0.6, (255,255,255), 2)
42
+
43
+ def analyze_dips(video_path):
44
+ cap = cv2.VideoCapture(video_path)
45
+ frame_width, frame_height = int(cap.get(3)), int(cap.get(4))
46
+ fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 30
47
+
48
+ output_video = "output_dips.mp4"
49
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
50
+ out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height))
51
+
52
+ while cap.isOpened():
53
+ ret, frame = cap.read()
54
+ if not ret:
55
+ break
56
+ image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
57
+ results = pose.process(image)
58
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
59
+ if results.pose_landmarks:
60
+ mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
61
+ landmarks = results.pose_landmarks.landmark
62
+ feedback, accuracy = check_dips_feedback(landmarks)
63
+ draw_accuracy_bar(image, accuracy)
64
+ color = (0, 255, 0) if "Correct" in feedback else (0, 0, 255)
65
+ cv2.putText(image, feedback, (50, 50),
66
+ cv2.FONT_HERSHEY_COMPLEX, 1, color, 3)
67
+ out.write(image)
68
+ cap.release()
69
+ out.release()
70
+ return output_video
71
+
72
+ gr.Interface(
73
+ fn=analyze_dips,
74
+ inputs=gr.Video(),
75
+ outputs=gr.Video(),
76
+ title="Dips Form Analyzer",
77
+ description="Upload a video of your dips and receive form feedback!"
78
+ ).launch()