Spaces:
Sleeping
Sleeping
Download app.py from sschmied/APopTrainer: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/spaces/sschmied/APopTrainer/resolve/main/app.py
- Command line
-
hf download hf://spaces/sschmied/APopTrainer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sschmied/APopTrainer/resolve/main/app.py
3.91 kB
| # app.py | |
| import gradio as gr | |
| import cv2 | |
| import mediapipe as mp | |
| import numpy as np | |
| import plotly.graph_objects as go | |
| import math | |
| import logging | |
| # Initialize MediaPipe Pose model | |
| mp_pose = mp.solutions.pose | |
| pose = mp_pose.Pose( | |
| static_image_mode=True, | |
| model_complexity=2, | |
| min_detection_confidence=0.5 | |
| ) | |
| # Browser TTS Integration | |
| tts_js = """ | |
| <script> | |
| function speak(text) { | |
| if (window.speechSynthesis) { | |
| const utterance = new SpeechSynthesisUtterance(text); | |
| utterance.volume = 1; | |
| utterance.rate = 1; | |
| window.speechSynthesis.speak(utterance); | |
| } | |
| } | |
| // Activate TTS on any button click | |
| document.addEventListener("DOMContentLoaded", () => { | |
| document.querySelectorAll('button').forEach(button => { | |
| button.addEventListener('click', () => { | |
| if (window.speechSynthesis) { | |
| // Pre-activate audio context | |
| window.speechSynthesis.cancel(); | |
| } | |
| }); | |
| }); | |
| }); | |
| </script> | |
| """ | |
| def calculate_angle(a, b, c): | |
| ba = np.array(a) - np.array(b) | |
| bc = np.array(c) - np.array(b) | |
| cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc)) | |
| return np.degrees(np.arccos(np.clip(cosine_angle, -1.0, 1.0))) | |
| def process_image(img): | |
| try: | |
| results = pose.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | |
| output_image = np.zeros_like(img) | |
| if results.pose_landmarks: | |
| # Draw pose landmarks | |
| mp.solutions.drawing_utils.draw_landmarks( | |
| output_image, | |
| results.pose_landmarks, | |
| mp_pose.POSE_CONNECTIONS, | |
| landmark_drawing_spec=mp.solutions.drawing_styles.get_default_pose_landmarks_style() | |
| ) | |
| # Get key points | |
| landmarks = results.pose_landmarks.landmark | |
| nose = landmarks[mp_pose.PoseLandmark.NOSE.value] | |
| left_hip = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value] | |
| right_hip = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value] | |
| # Calculate angles | |
| vertical_angle = math.degrees(math.atan2( | |
| left_hip.x - right_hip.x, | |
| left_hip.y - right_hip.y | |
| )) | |
| # Create feedback message | |
| feedback = [] | |
| if vertical_angle > 15: | |
| feedback.append("Keep your hips lower") | |
| if nose.y > 0.8: | |
| feedback.append("Look forward") | |
| feedback_html = "<br>".join(feedback) if feedback else "Good form!" | |
| else: | |
| feedback_html = "No pose detected" | |
| output_image = img | |
| return cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB), feedback_html | |
| except Exception as e: | |
| logging.error(f"Processing error: {e}") | |
| return img, "Error processing image" | |
| def build_interface(): | |
| with gr.Blocks(title="Surf Trainer", css=".gradio-container {background: #f0f0f0}") as app: | |
| gr.HTML(tts_js) | |
| gr.Markdown("# 🏄 Surf Pop-Up Trainer") | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image(label="Input Image", sources=["upload"]) | |
| submit_btn = gr.Button("Analyze", variant="primary") | |
| with gr.Column(): | |
| image_output = gr.Image(label="Pose Analysis", interactive=False) | |
| feedback_output = gr.HTML(label="Feedback") | |
| submit_btn.click( | |
| fn=process_image, | |
| inputs=image_input, | |
| outputs=[image_output, feedback_output] | |
| ) | |
| gr.Markdown("---\n<div style='text-align: center'>Powered by MediaPipe & Gradio</div>") | |
| return app | |
| if __name__ == "__main__": | |
| build_interface().launch() |