Push-Ups with Python! (mediapipe + OpenCV)

Aryan Vij
2 min readFeb 14, 2022

--

In the Singapore Army, there is something called IPPT (Individual Physical Proficiency Test). The problem with this test is not how physically demanding it is, but instead the electronic machine that is used to count the number of Push-ups and Sit-ups done.

Like most people out there, my Push-up form was always sub-par (according to the machine’s opinion). Furthermore, many NSMen (those that have already completed their 2 years of mandatory National Service) struggle to do well for the IPPT test due to the lack of practice with the machine.

Therefore, I decided to build a program using mediapipe and OpenCV that tracks our Push-ups, ensuring that we use the proper form for each and every rep.

Landmarks detected by the mediapipe Pose Module

The code for this program can be found here.

The code above is from PoseModule.py and serves a few functions

  • Activate mediapipe’s Pose Detection module.
  • Detect the human body.
  • Finds the position of the different landmarks on the human body based on the model. (Landmarks are shown in image above).
  • Finds the angle between joints (depending on which joints you select). For my Push Up program, I chose to find the angle at the elbow, shoulder, and hip as those are vital for proper push-up form.

Next would be the code for the actual Push-up counting. In this, we will use the PoseModule and determine whether a push-up should be considered proper or improper form.

One important thing to note is from Line 17–21. Determine the resolution of your image captured from your camera, and from there adjust the pixel values when drawing the rectangles for the Push-up count, etc. (Lines 68–82).

And there we have it! A Push-up counting software that ensures proper form. Don’t go down all the way? No count! Put your knees on the floor? No count!

Happy Push-uping!

--

--