Counting Your Bicep Curl Reps with Python and AI

Have you ever wanted to keep track of your bicep curl reps in a more accurate and automated way? Well, today is your lucky day! In this article, we will explore how to build a Python script that uses machine learning to count your bicep curl reps using a dumbbell.

Let’s dive right in!

Counting Your Bicep Curl Reps with Python and AI
Counting Your Bicep Curl Reps with Python and AI

Setting Up the Environment

To start, we need to install a few modules. Open up your terminal or command prompt and type the following commands:

pip install pillow
pip install opencv-python
pip install scikit-learn

These modules will enable us to work with images, process camera input, and utilize traditional machine learning algorithms for our counting task.

Next, we will split our program into four files: an app file for the graphical user interface (GUI), a main file for running the program, a camera file for camera processing, and a model file for the machine learning model.

Building the Camera Class

In the camera file, we will create a class called “Camera” that handles camera processing. We will use OpenCV to work with the camera and capture frames. Here’s a simplified version of the code:

import cv2

class Camera:
    def __init__(self):
        self.camera = cv2.VideoCapture(0)  # Use the primary camera (change if necessary)

        if not self.camera.isOpened():
            raise ValueError("Camera not found!")

    def get_frame(self):
        ret, frame = self.camera.read()

        if ret:
            return ret, frame
        else:
            return None, None

    def __del__(self):
        self.camera.release()

This code initializes the camera and provides a method to retrieve frames from the camera. Make sure you have the correct camera index set in the VideoCapture() function (e.g., 0 for the primary camera).

Further reading:  Understanding Random Forest: A Powerful Machine Learning Model

Creating the GUI

In the app file, we will build the graphical user interface using the Tkinter module. Here’s a sample code skeleton:

import tkinter as tk
from PIL import Image, ImageTk
import cv2
from camera import Camera

class App:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Techal Bicep Rep Counter")

        # Create GUI elements here

    def init_gui(self):
        # Initialize the GUI elements here

    def update(self):
        # Update the GUI and perform counting logic here

    def start(self):
        # Start the main loop
        self.window.mainloop()

if __name__ == "__main__":
    app = App()
    app.start()

In this code, we initialize the GUI window and define the necessary functions for initialization, updating the GUI, and starting the main loop.

Counting Reps with Machine Learning

To count the reps, we need to create a machine learning model. In the model file, we will use the scikit-learn module to train a linear support vector classifier (SVC) model. Here’s a simplified version of the code:

import numpy as np
from sklearn.svm import LinearSVC

class Model:
    def __init__(self):
        self.model = LinearSVC()

    def train_model(self, image_list, class_list):
        self.model.fit(image_list, class_list)
        print("Model successfully trained!")

    def predict(self, frame):
        # Preprocess the frame and make predictions using the model
        return self.model.predict([frame])[0]

This code initializes the model and provides functions to train the model and make predictions.

Combining Everything

In the main file, we will combine the camera, GUI, and model to create the complete program. Here’s a simplified version of the code structure:

from app import App
from camera import Camera
from model import Model

def main():
    camera = Camera()
    model = Model()
    app = App(camera, model)
    app.start()

if __name__ == "__main__":
    main()

This code initializes the camera, model, and GUI, and starts the main program loop.

Further reading:  Understanding LSTM (Long Short Term Memory)

Conclusion

Congratulations! You now have a Python script that uses machine learning to count your bicep curl reps. By following the steps outlined in this article, you can accurately track your reps while focusing on your workout. Happy lifting!

For more informative articles and tutorials on various topics related to technology, visit Techal.

Note: The provided code is a simplified version for illustration purposes. Additional error handling and optimization may be required for a production-ready implementation.

YouTube video
Counting Your Bicep Curl Reps with Python and AI