Build an AI Push-Up Counter with MediaPipe Body Landmarks

Build an AI Push-Up Counter with MediaPipe Body Landmarks

Table of Contents

  1. Introduction
  2. Building an AI Push-up Counter from Scratch
    • Installing the Required Libraries
    • Loading the Video File
    • Resizing the Video
    • Post Detection and Point Extraction
    • Drawing the Points on the Screen
    • Finding the Angles
    • Implementing the Counting Algorithm
    • Displaying the Count and Graphical Interface
  3. Conclusion

Building an AI Push-up Counter from Scratch

In this Tutorial, we will walk you through the process of building your very own AI push-up counter from scratch. By following along with this tutorial, you will learn how to install the necessary libraries, load and resize a video file, perform post detection and point extraction, draw points on the screen, find angles, implement a counting algorithm, and display the count and a graphical interface.

Installing the Required Libraries

The first step is to install the required libraries for our AI push-up counter. We will need to install the "opcv" library, which handles computer vision tasks, and the "non-fire" library, which provides functions for performing calculations. Additionally, we will install the "CV Zone" library, which provides a wrapper for the "CV2" library, and the "math" module to perform mathematical calculations.

pip install opcv
pip install non-fire
pip install CV Zone

Loading the Video File

Once the necessary libraries are installed, we can proceed to load the video file that we will be using for our push-up counter. You can choose any video file you like, and we recommend saving it in the same directory as your Python code. To load the video file, we will use the OpenCV library's "VideoCapture" object. We will then read the video file in a loop and display the frames on the screen.

Resizing the Video

To ensure that the video is displayed at a consistent size, we will resize it to a fixed Shape. We will use the CV2 library's "resize" function to resize the video to our desired width and Height. By resizing the video, we can ensure that the push-up analysis is performed consistently regardless of the size of the original video.

Post Detection and Point Extraction

Next, we will perform post detection and point extraction on the video frames using the MediaPipe library. MediaPipe provides a pre-trained model that can detect the positions of the body landmarks in each frame of the video. We will extract the required points for our push-up counter, such as the points on the hands and elbows.

Drawing the Points on the Screen

Once we have extracted the required points, we can draw them on the screen to Visualize the body landmarks. We will use the CV Zone library to draw circles at the detected points. By drawing the points on the screen, we can verify that the body landmarks are accurately tracked and extracted.

Finding the Angles

After drawing the points on the screen, we can proceed to find the angles at the specified points. We will calculate the angles based on the positions of the points on the human body. By finding the angles, we can determine whether the person is performing a push-up or not.

Implementing the Counting Algorithm

To keep track of the number of push-ups, we will implement a counting algorithm. The algorithm will determine when a push-up is completed based on the angles of the body landmarks. We will count one push-up when the person goes from a fully extended position to a fully contracted position. Additionally, we will take into account the direction of the movement to ensure accurate counting.

Displaying the Count and Graphical Interface

Finally, we will display the count of push-ups on the screen using CV2's "putText" function. We will create a graphical interface that shows the count and a visual representation of the push-up bar. The interface will update in real-time as the person performs push-ups, providing immediate feedback on their progress.

Conclusion

In this tutorial, we have explored the process of building an AI push-up counter from scratch. We have learned how to install the necessary libraries, load and resize a video file, perform post detection and point extraction, draw points on the screen, find angles, implement a counting algorithm, and display the count and a graphical interface. By following along with this tutorial, you have gained the knowledge and skills to create your very own AI push-up counter. Explore the possibilities of applying this technology to other Fitness tracking applications and continue to enhance your AI development skills. Happy coding!


Highlights

  • Learn how to build an AI push-up counter from scratch
  • Install the necessary libraries for computer vision tasks
  • Load and resize a video file for analysis
  • Perform post detection and point extraction on video frames
  • Draw body landmarks on the screen to verify accuracy
  • Calculate angles to determine push-up completion
  • Implement a counting algorithm based on angles and direction
  • Display the push-up count and graphical interface

FAQ

Q: What are the required libraries for building an AI push-up counter? A: The required libraries are opcv, non-fire, CV Zone, and math.

Q: Can I use any video file for analysis? A: Yes, you can use any video file of your choice. We recommend saving it in the same directory as your Python code.

Q: How do I resize the video to a fixed shape? A: You can use the "resize" function from the CV2 library to resize the video to your desired width and height.

Q: How does the counting algorithm work? A: The counting algorithm tracks the angles of the body landmarks and determines when a push-up is completed based on the change in angles. It also takes into account the direction of movement to ensure accurate counting.

Q: How is the count displayed on the screen? A: The count is displayed using the CV2 library's "putText" function, which allows you to overlay text on the video frames.


Resources

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content