A real-time computer vision system that evaluates squat form using MediaPipe Pose, delivers instant corrective feedback, and counts repetitions β via a clean Gradio web interface supporting both video upload and live webcam.
| Video Upload Mode | Webcam Mode |
|---|---|
| Upload a pre-recorded squat video | Live real-time analysis via your webcam |
| Get back an annotated video with feedback overlay | See skeleton, angles & feedback in real time |
- 𦴠Pose Estimation β MediaPipe's pre-trained BlazePose model extracts 33 body landmarks per frame
- π 4 Biomechanical Rules β Back angle, knee-over-toe, squat depth, heel lift (see Evaluation Rules)
- π Rep Counter β Phase-state machine automatically counts completed squat repetitions
- π¨ Rich Annotations β Skeleton overlay, joint labels, angle values, colour-coded feedback panel
- πΉ Video Upload β Process any
.mp4file; download the annotated result - π· Live Webcam β Stream from your webcam with sub-second latency
- π Gradio Web UI β Runs in your browser; optional public sharing via Gradio tunnels
MediaPipe Pose is used to detect 33 body landmarks in each frame. Landmarks are returned as normalised coordinates (x, y) β [0, 1] relative to the frame dimensions.
For squat analysis the following left-side landmarks are used:
| Landmark | MediaPipe Index |
|---|---|
| Left Shoulder | 11 |
| Left Hip | 23 |
| Left Knee | 25 |
| Left Ankle | 27 |
| Left Foot Index (big toe) | 31 |
Why left-side? The system is optimised for a left-side view of the subject, which provides the clearest sagittal-plane profile of the squat.
The interior angle at a joint B formed by landmarks A β B β C is computed as:
cos ΞΈ = (BA Β· BC) / (|BA| Γ |BC|)
ΞΈ = arccos(clamp(cos ΞΈ, -1, 1))
This is implemented in SquatLogic.calculate_angle(a, b, c).
A simple 2-state machine tracks the squat cycle:
STANDING ββ(hip descends to knee level)βββΊ SQUATTING
ββ(hip rises above knee level)ββ SQUATTING β rep_count += 1
All thresholds are defined as constants at the top of squat_logic.py and can be tuned.
| Method | Interior angle at the hip, computed from shoulder β hip β knee |
| Threshold | < 50Β° signals excessive forward lean |
| Feedback | β Straighten your back (torso leaning too far forward) |
BACK_ANGLE_MIN = 50.0 # degrees
| Method | Relative x-position: knee_x - ankle_x > KNEE_TOE_TOLERANCE |
| Threshold | > 0.04 (normalised units, ~4 % of frame width) |
| Feedback | β Keep your knees behind your toes |
KNEE_TOE_TOLERANCE = 0.04 # normalised coords
| Method | Vertical distance: hip_y < knee_y - DEPTH_MARGIN (y increases downward) |
| Threshold | DEPTH_MARGIN = 0.02 |
| Feedback | β Lower your hips further (increase squat depth) |
| Note | Only checked when phase == "SQUATTING" to avoid false positives while standing |
DEPTH_MARGIN = 0.02 # normalised coords
| Method | ankle_y < foot_y - HEEL_LIFT_MARGIN β ankle rising above toe level |
| Threshold | HEEL_LIFT_MARGIN = 0.03 |
| Feedback | β Keep your heels on the ground |
HEEL_LIFT_MARGIN = 0.03 # normalised coords
- Side-view capture β The system is calibrated for a clear left-side profile. A frontal view will cause the x-axis rules (knee-over-toe) to behave incorrectly.
- Single person β Only one subject should be in the frame. MediaPipe's standard Pose model (non-holistic) tracks the most prominent person.
- Adequate lighting β Dark or heavily backlit scenes may reduce landmark confidence.
- Consistent frame rate β The rep counter is based on positional transitions, not time, so an inconsistent frame rate does not cause counting errors.
- Normalised coordinates β All thresholds are expressed in normalised
[0, 1]space, making them resolution-agnostic. - Left-side landmarks only β If the subject faces right (right side visible), landmarks 12/24/26/28/32 (right side) should be substituted in
_extract_joints().
| Limitation | Detail |
|---|---|
| Side-view dependency | Frontal or rear views reduce accuracy significantly |
| No temporal smoothing | Noisy landmark jitter can trigger false feedback on single frames |
| Fixed-threshold rules | Thresholds are not personalised to body proportions or flexibility |
| Single-person only | Multi-person scenes are unsupported |
| Occlusion sensitivity | Covered joints (e.g., loose clothing) lower MediaPipe confidence |
| Webcam latency | Processing latency depends on hardware; a GPU is not required but helps |
| No model training | No custom model was trained; relies entirely on MediaPipe's pre-trained weights |
- Anaconda or Miniconda installed
- A webcam (required for Live Webcam mode)
- Python 3.10 (recommended β this project was tested on
squat_envwith Python 3.10)
conda create -n squat_env python=3.10 -yconda activate squat_envYou should see
(squat_env)appear at the start of your terminal prompt.
pip install -r requirements.txtThis installs all required packages into the squat_env environment.
mediapipe==0.10.9
opencv-python==4.9.0.80
numpy==1.26.4
gradio>=4.44.1
Note:
gradio>=4.44.1is required for compatibility with the pydantic/fastapi versions in modern conda environments.
Make sure squat_env is activated, then run:
(squat_env) python app.pyThe Gradio interface will open automatically at http://localhost:7860.
A public shareable URL is also printed if share=True is set.
- Click the "Video Upload" tab.
- Drop or browse to your
.mp4squat video. - Click "Analyse Video".
- The annotated video (skeleton, feedback panel, rep counter) downloads to the right panel.
Tip: Sample videos are included in the
videos/folder:
videos/correct_posture_squat.mp4videos/incorrect_posture_squats.mp4videos/Exercise Tutorial - Squat.mp4
- Click the "Live Webcam" tab.
- Grant browser camera permissions when prompted.
- Stand in left-side view (left side of body facing the camera) within ~2β3 m.
- Perform squats β feedback status (PASS / WARNING / ALERT), phase, and rep count update in real time.
- Click "Reset Session" to clear the rep counter and restart.
cv_mini_project/
βββ app.py # Gradio application (video upload + webcam tabs)
βββ squat_logic.py # Core posture evaluation logic & rep counter
βββ requirements.txt # Python dependencies
βββ videos/ # Sample squat videos
β βββ correct_posture_squat.mp4 # Reference: correct squat form
β βββ incorrect_posture_squats.mp4# Reference: common form errors
β βββ Exercise Tutorial - Squat.mp4
βββ README.md # This file
This project is submitted as part of a Computer Vision Engineering Mini Project (CVP). Free to use and adapt for educational purposes.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Mini project demonstration video link: "https://drive.google.com/file/d/1HXG7dwR44fWLphQBYrqEHhFuI1fb0x2K/view?usp=sharing"