Thilak118 commited on
Commit
690e476
·
verified ·
1 Parent(s): b70d762

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -0
app.py CHANGED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+ # Initialize MediaPipe Pose
7
+ mp_drawing = mp.solutions.drawing_utils
8
+ mp_pose = mp.solutions.pose
9
+
10
+ # Global variables for rep counting
11
+ counter = 0
12
+ stage = None
13
+
14
+ # Function to calculate angle between three points
15
+ def calculate_angle(a, b, c):
16
+ a = np.array(a) # First point
17
+ b = np.array(b) # Mid point (pivot)
18
+ c = np.array(c) # End point
19
+ radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
20
+ angle = np.abs(radians * 180.0 / np.pi)
21
+ if angle > 180.0:
22
+ angle = 360 - angle
23
+ return angle
24
+
25
+ # Function to process frames (used for both webcam and uploaded video)
26
+ def process_frames(cap):
27
+ global counter, stage
28
+ with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
29
+ while cap.isOpened():
30
+ ret, frame = cap.read()
31
+ if not ret:
32
+ break
33
+ image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
+ image.flags.writeable = False
35
+ results = pose.process(image)
36
+ image.flags.writeable = True
37
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
38
+ try:
39
+ landmarks = results.pose_landmarks.landmark
40
+ shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
41
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
42
+ elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x,
43
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
44
+ wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
45
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
46
+ angle = calculate_angle(shoulder, elbow, wrist)
47
+ cv2.putText(image, str(round(angle, 2)),
48
+ tuple(np.multiply(elbow, [frame.shape[1], frame.shape[0]]).astype(int)),
49
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
50
+ if angle > 160:
51
+ stage = "down"
52
+ if angle < 30 and stage == "down":
53
+ stage = "up"
54
+ counter += 1
55
+ except:
56
+ pass
57
+ cv2.rectangle(image, (0, 0), (225, 73), (245, 117, 16), -1)
58
+ cv2.putText(image, 'REPS', (15, 12),
59
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
60
+ cv2.putText(image, str(counter), (10, 60),
61
+ cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2, cv2.LINE_AA)
62
+ cv2.putText(image, 'STAGE', (65, 12),
63
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
64
+ cv2.putText(image, stage if stage else "", (60, 60),
65
+ cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2, cv2.LINE_AA)
66
+ mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
67
+ mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=2),
68
+ mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2))
69
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
70
+ yield image_rgb, f"Reps: {counter}", f"Stage: {stage if stage else 'None'}"
71
+ cap.release()
72
+
73
+ # Function to process webcam feed
74
+ def process_webcam():
75
+ global counter, stage
76
+ counter = 0
77
+ stage = None
78
+ cap = cv2.VideoCapture(0) # Use default webcam
79
+ for frame, reps, stage in process_frames(cap):
80
+ yield frame, reps, stage
81
+
82
+ # Function to process uploaded video
83
+ def process_uploaded_video(video_path):
84
+ global counter, stage
85
+ counter = 0
86
+ stage = None
87
+ cap = cv2.VideoCapture(video_path)
88
+ for frame, reps, stage in process_frames(cap):
89
+ yield frame, reps, stage
90
+
91
+ # Gradio interface using gr.Blocks
92
+ with gr.Blocks() as demo:
93
+ gr.Markdown("# Exercise Pose Detection")
94
+ gr.Markdown("Choose an input source to track bicep curls and pose.")
95
+
96
+ # Main Layout: Split into Left and Right Columns
97
+ with gr.Row():
98
+ # Left Side: Input Source and Controls (Made Smaller)
99
+ with gr.Column(scale=1):
100
+ input_source = gr.Dropdown(choices=["Use Webcam", "Upload Video"], label="Select Input Source", value="Use Webcam", elem_classes="small-dropdown")
101
+ start_webcam_btn = gr.Button("Start Webcam", visible=True, elem_classes="small-button")
102
+ video_input = gr.Video(label="Upload Video", visible=False, elem_classes="small-video-input")
103
+
104
+ # Right Side: Pose Detection Feed and Outputs
105
+ with gr.Column(scale=3):
106
+ with gr.Row():
107
+ video_output = gr.Image(label="Pose Detection Feed", streaming=True, elem_classes="large-video")
108
+ with gr.Column():
109
+ rep_count = gr.Textbox(label="Rep Count", elem_classes="small-textbox")
110
+ stage_output = gr.Textbox(label="Stage", elem_classes="small-textbox")
111
+
112
+ # Custom CSS for styling
113
+ demo.css = """
114
+ .small-dropdown {
115
+ max-width: 200px !important;
116
+ }
117
+ .small-button {
118
+ max-width: 200px !important;
119
+ padding: 5px !important;
120
+ font-size: 14px !important;
121
+ }
122
+ .small-video-input {
123
+ max-width: 200px !important;
124
+ }
125
+ .large-video {
126
+ width: 100% !important;
127
+ max-width: 800px !important;
128
+ margin: 0 auto !important;
129
+ }
130
+ .small-textbox {
131
+ max-width: 150px !important;
132
+ height: 50px !important;
133
+ margin: 10px 0 !important;
134
+ }
135
+ """
136
+
137
+ # Function to toggle visibility of input components based on dropdown
138
+ def toggle_inputs(source):
139
+ if source == "Use Webcam":
140
+ return gr.update(visible=True), gr.update(visible=False), gr.update(value=None), gr.update(value=None), gr.update(value=None)
141
+ else:
142
+ return gr.update(visible=False), gr.update(visible=True), gr.update(value=None), gr.update(value=None), gr.update(value=None)
143
+
144
+ # Update visibility and clear outputs when dropdown changes
145
+ input_source.change(
146
+ toggle_inputs,
147
+ inputs=[input_source],
148
+ outputs=[start_webcam_btn, video_input, video_output, rep_count, stage_output]
149
+ )
150
+
151
+ # Start webcam feed when the button is clicked
152
+ start_webcam_btn.click(
153
+ process_webcam,
154
+ inputs=None,
155
+ outputs=[video_output, rep_count, stage_output],
156
+ queue=True
157
+ )
158
+
159
+ # Process uploaded video when a file is uploaded
160
+ video_input.change(
161
+ process_uploaded_video,
162
+ inputs=[video_input],
163
+ outputs=[video_output, rep_count, stage_output],
164
+ queue=True
165
+ )
166
+
167
+ demo.launch()