This application use the MediaPipe library and OpenCV to create a comprehensive real-time pose detection and analysis system. It can track body movements, analyze exercise form, count repetitions, and provide immediate feedback on posture and technique.
- Pose Detection System
- Posture Analysis: Evaluates standing posture by checking shoulder alignment, ear-shoulder-hip alignment, and body verticality
- Squat Form Analysis: Measures knee angles and depth to ensure proper squat technique
- Push-up Counter: Counts repetitions and analyzes form, including elbow angles and torso position
- Custom Analysis: Allows for custom angle measurements between any specified landmarks
- Real-time accuracy percentage of correct form
- Rep counter for repetitive exercises
- Visual feedback on form quality with color-coded indicators
- Automatic snapshots when good form is maintained for a set duration
- Video recording capability with timestamp organization
- Organized file storage for post-workout review
- Status bar with exercise information and performance metrics
- Skeleton overlay with joint connections
- Text with semi-transparent backgrounds for enhanced readability
- Color-coded feedback (green for correct form, red for incorrect)
Q
key: Quit the applicationS
key: Toggle skeleton visibilityR
key: Start/stop recording videoM
key: Switch between exercise modesT
key: Start a countdown timer for pose holdingC
key: Clear statistics and rep count
- Python 3.7+
- OpenCV (
cv2
) - MediaPipe (
mediapipe
) - NumPy (
numpy
) - Standard Python libraries:
time
,datetime
,os
-
Clone this repository:
git clone https://github.com/yourusername/pose-detection.git cd pose-detection
-
Install the required dependencies:
pip install opencv-python mediapipe numpy
-
Run the application:
python advanced_pose_detection.py
The system uses MediaPipe's pose detection model which identifies 33 landmarks on the human body. These landmarks are then used to calculate angles and relationships between body parts for exercise analysis.
Evaluates:
- Shoulder alignment (horizontal)
- Ear-shoulder-hip alignment (vertical line)
- Overall body verticality
- Combined posture score based on deviations
Measures:
- Knee angle (between hip, knee, and ankle)
- Proper depth assessment
- Optimal squat form based on biomechanical principles
Analyzes:
- Elbow angle (between shoulder, elbow, and wrist)
- Hip position to detect sagging
- Push-up phase detection (up/down) for rep counting
The angle between three points (A, B, C) is calculated using the arctan2 function:
def calculate_angle(a, b, c):
"""Calculate angle between three points (in degrees)"""
a = np.array([a.x, a.y])
b = np.array([b.x, b.y])
c = np.array([c.x, c.y])
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
- Launch the application
- Stand in front of your webcam
- Use the
M
key to select your exercise mode - Perform your exercise and receive real-time feedback
- Set your desired exercise mode
- Press
R
to start recording - Complete your workout with form feedback
- Press
R
again to stop recording - Find your workout video in the
pose_snapshots
directory
- Select the appropriate exercise mode
- Perform the movement slowly
- Watch the feedback to adjust your form
- Hold the correct form and press
T
to start the timer - Maintain form until the timer completes to save a snapshot
The application can be customized in several ways:
Create a new analysis function following the pattern of existing ones:
def get_new_exercise_feedback(landmarks):
# Get relevant landmarks
landmark_a = landmarks[mp_pose.PoseLandmark.LANDMARK_A]
landmark_b = landmarks[mp_pose.PoseLandmark.LANDMARK_B]
# Calculate metrics
# ...
# Return form assessment
return is_correct, feedback_text, feedback_color
Then add your new exercise to the modes list:
modes = ["posture", "squat", "pushup", "your_new_exercise", "custom"]
Modify the thresholds in the feedback functions:
# Example: Making posture detection more lenient
if posture_score < 0.20: # Changed from 0.15
return True, f"Excellent posture: {posture_score:.3f}", (0, 255, 0)
Adjust the drawing functions to modify the interface:
# Example: Changing the status bar color
cv2.rectangle(frame, (0, 0), (w, 60), (70, 70, 70), -1) # Changed from (50, 50, 50)
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Created by- Muhammad Fahd Bashir