sschmied commited on
Commit
cdc5feb
·
verified ·
1 Parent(s): cddf332

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -420
app.py DELETED
@@ -1,420 +0,0 @@
1
- import cv2
2
- import os
3
- import numpy as np
4
- from ultralytics import YOLO
5
- import time
6
- import matplotlib.pyplot as plt
7
- import pyttsx3
8
- import datetime
9
- import gradio as gr
10
- import torch
11
- import tempfile
12
- #from fastrtc import Stream
13
-
14
- # Global Initializations
15
- device = "cuda" if torch.cuda.is_available() else "cpu"
16
- model = YOLO("yolo11n-pose.pt").to(device)
17
-
18
- # Text-to-speech engine
19
- engine = pyttsx3.init()
20
- voices = engine.getProperty('voices')
21
- engine.setProperty('voice', voices[1].id)
22
- engine.setProperty('rate', 150)
23
- engine.setProperty('volume', 0.9)
24
- engine.say("Welcome to the Pop-Up Trainer.")
25
- engine.runAndWait()
26
-
27
- # Tracking state
28
- state = {
29
- 'warning_timestamp_hip': 0,
30
- 'warning_timestamp_head': 0,
31
- 'hip_warning_given': False,
32
- 'head_warning_given': False,
33
- 'repetitions': 0,
34
- 'position': "down",
35
- 'back_angles': [],
36
- 'head_angles': [],
37
- 'left_knee_angles': [],
38
- 'right_knee_angles': [],
39
- 'frame_numbers': []
40
- }
41
-
42
- colors = {
43
- 'default': (255, 0, 0), 'circle': (0, 0, 0),
44
- 'back': (0, 255, 0), 'head': (0, 165, 255)
45
- }
46
-
47
- # Paths to example images & videos
48
- image_folder = "images"
49
- video_folder = "videos"
50
-
51
- # Get list of example images & videos
52
- example_images = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.endswith((".jpg", ".png", ".jpeg"))]
53
- example_videos = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith((".mp4", ".avi", ".mov"))]
54
-
55
-
56
- def get_date_time():
57
- return datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
58
-
59
- def calculate_angle(p1, p2, p3):
60
- if not all(p != (0, 0) for p in [p1, p2, p3]):
61
- return 0
62
- v1 = np.array(p1) - np.array(p2)
63
- v2 = np.array(p3) - np.array(p2)
64
- cosine_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
65
- return np.degrees(np.arccos(np.clip(cosine_angle, -1.0, 1.0)))
66
-
67
- #def process_frame(frame, frame_width, frame_height, fps, conf_threshold):
68
- def process_frame(frame, fps, conf_threshold): #put fps back when plotting
69
- global device
70
-
71
- results = model(frame,
72
- device=device,
73
- imgsz=(480, 640),
74
- half=True,
75
- conf=conf_threshold,
76
- stream=True,
77
- stream_buffer=False,
78
- max_det=1,
79
- vid_stride=3,
80
- show=False, #turn off for faster inference
81
- show_conf=False,
82
- save=False,
83
- show_boxes=False,
84
- save_crop=False
85
-
86
- )
87
-
88
- processed_frame = results[0].plot()
89
-
90
- # Draw skeleton on image
91
- #skeleton_image = draw_skeleton(frame, results[0].keypoints.data)
92
-
93
- # Plot angles
94
- #plot_image = plot_angles(fps)
95
-
96
- # Combine visualizations
97
- #combined = np.hstack((skeleton_image, plot_image))
98
-
99
- # Stack frames vertically
100
- #min_width = min(pose_frame.shape[1], combined.shape[1])
101
- #pose_frame = cv2.resize(pose_frame, (min_width, pose_frame.shape[0]))
102
- #combined = cv2.resize(combined, (min_width, combined.shape[0]))
103
- #final_frame = np.vstack((pose_frame, combined))
104
- return processed_frame
105
-
106
- def process_image(image_path, conf):
107
-
108
- image = cv2.imread(image_path)
109
- results = model(frame,
110
- device=device,
111
- imgsz=(480, 640),
112
- half=True,
113
- conf=conf_threshold,
114
- stream=True,
115
- stream_buffer=False,
116
- max_det=1,
117
- vid_stride=3,
118
- show=False, #turn off for faster inference
119
- show_conf=False,
120
- save=False,
121
- show_boxes=False,
122
- save_crop=False
123
-
124
- )
125
-
126
- #results = process_frame(image, device=device, conf=conf)
127
-
128
- processed_image = results[0].plot()
129
-
130
- return cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
131
-
132
- # def process_video(video_path, conf_threshold):
133
- # global state
134
- # reset_state()
135
-
136
- # cap = cv2.VideoCapture(video_path)
137
- # if not cap.isOpened():
138
- # return "Error: Could not open video file."
139
-
140
- # frame_width, frame_height = int(cap.get(3)), int(cap.get(4))
141
- # fps = int(cap.get(cv2.CAP_PROP_FPS))
142
-
143
- # temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
144
- # output_video_path = temp_output.name
145
-
146
- # fourcc = cv2.VideoWriter_fourcc(*"mp4v")
147
- # out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
148
-
149
- # while cap.isOpened():
150
- # ret, frame = cap.read()
151
- # if not ret:
152
- # break
153
-
154
- # results = process_frame(frame, fps=fps, conf=conf_threshold)
155
-
156
- # processed_frame = results[0].plot()
157
- # out.write(processed_frame)
158
-
159
- # yield cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB) # Stream frame-by-frame
160
-
161
- # cap.release()
162
- # out.release()
163
- # return output_video_path # Return downloadable processed video
164
-
165
-
166
-
167
-
168
- def process_webcam(conf_threshold):
169
- cap = cv2.VideoCapture(0)
170
- if not cap.isOpened():
171
- raise ValueError("Error opening webcam")
172
-
173
- while cap.isOpened():
174
- ret, frame = cap.read()
175
- if not ret:
176
- break
177
-
178
- processed_frame = process_frame(frame, 30, conf=conf_threshold)
179
- processed_frame = results[0].plot()
180
-
181
- yield cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
182
-
183
- cap.release()
184
-
185
-
186
- def draw_skeleton(image, keypoints):
187
- skeleton_image = np.full_like(image, 255)
188
- connections = [(3, 1), (1, 0), (0, 2), (2, 4), (1, 2), (4, 6), (3, 5),
189
- (5, 6), (5, 7), (7, 9), (6, 8), (8, 10), (11, 12), (11, 13),
190
- (13, 15), (12, 14), (14, 16), (5, 11), (6, 12)]
191
-
192
- keypoints = keypoints.cpu().numpy()
193
-
194
- #if keypoints.ndim == 2:
195
- #keypoints = np.expand_dims(keypoints, axis=0)
196
-
197
- keypoints = keypoints[0]
198
-
199
- hip_idx = 12 if keypoints[12][2] > 0.5 else 11
200
- shoulder_idx = 6 if keypoints[12][2] > 0.5 else 5
201
- ear_idx = 4 if keypoints[12][2] > 0.5 else 3
202
- eye_idx = 2 if keypoints[12][2] > 0.5 else 1
203
- knee_idx = 14 if keypoints[12][2] > 0.5 else 13
204
- ankle_idx = 16 if keypoints[12][2] > 0.5 else 15
205
-
206
- for i, (x, y, conf) in enumerate(keypoints):
207
- if conf > 0.5:
208
- cv2.circle(skeleton_image, (int(x), int(y)), 12, colors['circle'], -1)
209
- cv2.putText(skeleton_image, f'{i}', (int(x), int(y)-10),
210
- cv2.FONT_HERSHEY_SIMPLEX, 0.4, colors['circle'], 1)
211
-
212
- for part_a, part_b in connections:
213
- x1, y1, conf1 = keypoints[part_a]
214
- x2, y2, conf2 = keypoints[part_b]
215
- if conf1 > 0.5 and conf2 > 0.5:
216
- color = colors['default']
217
- if (part_a, part_b) == (shoulder_idx, hip_idx):
218
- color = colors['back']
219
- elif (part_a, part_b) == (eye_idx, ear_idx):
220
- color = colors['head']
221
- cv2.line(skeleton_image, (int(x1), int(y1)), (int(x2), int(y2)), color, 6)
222
-
223
- angles = process_pose(keypoints, hip_idx, shoulder_idx, ear_idx, eye_idx, knee_idx, ankle_idx, skeleton_image)
224
- update_visualization(skeleton_image, angles, image.shape[0])
225
- return skeleton_image
226
-
227
- def process_pose(keypoints, hip_idx, shoulder_idx, ear_idx, eye_idx, knee_idx, ankle_idx, skeleton_image):
228
- global state
229
- angles = {'back': 0, 'head': 0, 'right_knee': 0, 'left_knee': 0}
230
-
231
- # Head angle calculation
232
- eye, ear = keypoints[eye_idx], keypoints[ear_idx]
233
- if eye[2] > 0.5 and ear[2] > 0.5:
234
- angles['head'] = -np.degrees(np.arctan2(eye[1] - ear[1], abs(eye[0] - ear[0])))
235
- if angles['head'] < 0 and not state['head_warning_given']:
236
- state['warning_timestamp_head'] = time.time()
237
- engine.say("Keep your head up")
238
- engine.runAndWait()
239
- state['head_warning_given'] = True
240
-
241
- # Back angle calculation
242
- shoulder, hip = keypoints[shoulder_idx], keypoints[hip_idx]
243
- if shoulder[2] > 0.5 and hip[2] > 0.5:
244
- angles['back'] = -np.degrees(np.arctan2(shoulder[1] - hip[1], abs(shoulder[0] - hip[0])))
245
- if angles['back'] < 0 and not state['hip_warning_given']:
246
- state['warning_timestamp_hip'] = time.time()
247
- engine.say("Lower your hips")
248
- engine.runAndWait()
249
- state['hip_warning_given'] = True
250
-
251
- # Knee angles calculation
252
- for side, indices in [('right', (12, 14, 16)), ('left', (11, 13, 15))]:
253
- hip, knee, ankle = [keypoints[i] for i in indices]
254
- if all(k[2] > 0.5 for k in [hip, knee, ankle]):
255
- angles[f'{side}_knee'] = calculate_angle(
256
- (hip[0], hip[1]), (knee[0], knee[1]), (ankle[0], ankle[1]))
257
-
258
- # Repetition counting logic
259
- if angles['right_knee'] > 150 or angles['left_knee'] > 150:
260
- if state['position'] == "up":
261
- state['repetitions'] += 1
262
- state['position'] = "down"
263
- engine.say(f"Repetitions: {state['repetitions']}")
264
- engine.runAndWait()
265
- elif angles['right_knee'] < 110 and angles['left_knee'] < 110:
266
- state['position'] = "up"
267
-
268
- # Update state with new angles
269
- for key, value in angles.items():
270
- state[f'{key}_angles'].append(value)
271
- state['frame_numbers'].append(len(state['frame_numbers']))
272
-
273
- return angles
274
-
275
- def update_visualization(image, angles, height):
276
- global state
277
- current_time = time.time()
278
-
279
- # Display warnings
280
- if current_time - state['warning_timestamp_hip'] < 5:
281
- cv2.putText(image, 'Lower your hips', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
282
- if current_time - state['warning_timestamp_head'] < 5:
283
- cv2.putText(image, 'Lift your head up', (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
284
-
285
- # Display angles
286
- y_pos = 180
287
- for name, value in angles.items():
288
- cv2.putText(image, f'{name.capitalize()} Angle: {value:.2f}', (10, y_pos),
289
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
290
- y_pos += 60
291
-
292
- # Display repetitions
293
- cv2.putText(image, f'Repetitions: {state["repetitions"]}', (10, y_pos),
294
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
295
-
296
- # Add ground line
297
- ground_y = height - 50
298
- cv2.line(image, (0, ground_y), (image.shape[1], ground_y), (0, 0, 0), 2)
299
- overlay = image.copy()
300
- overlay[ground_y:, :] = (192, 192, 192)
301
- cv2.addWeighted(overlay, 0.5, image, 0.5, 0, image)
302
- return cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=(0, 0, 0))
303
-
304
- def plot_angles(fps):
305
- global state
306
- times = [frame / fps for frame in state['frame_numbers']]
307
- plt.figure(figsize=(5, 5))
308
-
309
- # Plot all angles
310
- for angle_type in ['back', 'head', 'left_knee', 'right_knee']:
311
- angles = state[f'{angle_type}_angles'][:len(times)]
312
- plt.plot(times, angles, label=f'{angle_type.capitalize()} Angle')
313
-
314
- plt.xlabel('Time (seconds)')
315
- plt.ylabel('Angle (degrees)')
316
- plt.legend()
317
- plt.title('Angles')
318
- plt.grid(True)
319
- plt.tight_layout()
320
-
321
- plot_path = os.path.join(output_folder, 'plot.png')
322
- plt.savefig(plot_path)
323
- plt.close()
324
- return cv2.imread(plot_path)
325
-
326
- def process_video(video_path):
327
- cap = cv2.VideoCapture(video_path)
328
- if not cap.isOpened():
329
- return "Error: Could not open video file."
330
-
331
- frame_width, frame_height = int(cap.get(3)), int(cap.get(4))
332
- fps = int(cap.get(cv2.CAP_PROP_FPS))
333
-
334
- temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
335
- output_video_path = temp_output.name
336
-
337
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
338
- out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
339
-
340
- while cap.isOpened():
341
- ret, frame = cap.read()
342
- if not ret:
343
- break
344
-
345
- results = model(frame,
346
- device=device,
347
- imgsz=(480, 640),
348
- half=True,
349
- conf=conf_threshold,
350
- stream=True,
351
- stream_buffer=False,
352
- max_det=1,
353
- vid_stride=3,
354
- show=False, #turn off for faster inference
355
- show_conf=False,
356
- save=False,
357
- show_boxes=False,
358
- save_crop=False
359
-
360
- )
361
- processed_frame = results[0].plot()
362
- out.write(processed_frame)
363
-
364
- yield cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB) # Stream frame-by-frame
365
-
366
- cap.release()
367
- out.release()
368
- return output_video_path # Return downloadable processed video
369
-
370
-
371
- def create_gui():
372
- with gr.Blocks(title="Pose Trainer") as demo:
373
- gr.Markdown("# 🏄 Pose Estimation App with Repetition Counter")
374
- gr.Markdown(
375
- "**A repetition counter. A repetition starts in the down position when both knees are greater than 150 degrees.**"
376
- " **Halfway is UP when both knees are less than 100 degrees.**"
377
- " **The repetition is complete when the knees are over 150 degrees again (down).**"
378
- )
379
-
380
- with gr.Row():
381
- with gr.Column():
382
- with gr.Tab("Image"):
383
- #image_input = gr.Image(label="Upload Image", sources="upload", type="image", height=480, width=640, autoplay=True)
384
- img_input = gr.File(label="Upload an Image", type="filepath")
385
- img_output = gr.Image(label="Processed Image")
386
- img_examples = gr.Gallery(value=example_images, inputs=img_input, height=150, preview=True)
387
- img_button = gr.Button("Process Image")
388
- img_button.click(fn=process_image, inputs=[img_input, gr.Slider(label="Confidence Threshold", minimum=0.0, maximum=1.0, step=0.05, value=0.30)], outputs=img_output)
389
-
390
-
391
-
392
- with gr.Tab("Upload Video"):
393
- vid_input = gr.File(label="Upload a Video", type="filepath")
394
- vid_output = gr.Video(label="Processed Video Stream", streaming=True)
395
- vid_examples = gr.Examples(examples=example_videos, inputs=vid_input)
396
- vid_button = gr.Button("Process Video")
397
- vid_button.click(process_video, inputs=[img_input, gr.Slider(label="Confidence Threshold", minimum=0.0, maximum=1.0, step=0.05, value=0.30)], outputs=vid_output)
398
-
399
-
400
-
401
- with gr.Tab("Use Webcam"):
402
- #webcam_input = gr.Video(label="Webcam", sources="webcam", interactive=True, streaming=True, format="mp4", height=480, width=640, autoplay=True, show_download_button=True)
403
-
404
- webcam_output = gr.Video(label="Live Processed Webcam Stream", streaming=True)
405
- webcam_button = gr.Button("Start Webcam")
406
- webcam_button.click(process_webcam, outputs=webcam_output)
407
-
408
-
409
-
410
- gr.Markdown("Developed for **Hugging Face Spaces** | YOLOv11n-Pose + OpenCV | **GPU Acceleration Supported**")
411
-
412
-
413
-
414
-
415
-
416
- return demo
417
-
418
- if __name__ == "__main__":
419
- gui = create_gui()
420
- gui.launch()