Open Nav

Workout Detector in Python

Author avatar

Jakub Domaszewicz

Updated onDecember 3, 2021

How many push-ups can you do? A team from Stupid Hackathon, a hackathon at the Pomeranian Hackerspace, wanted to build an application with the Aidlab chest strap that encourages people to lead active, healthy lifestyles.

We'll show you how to make a very simple app that will communicate with Aidlab through the Aidlab SDK. When you’re finished, your Python script will present the ocurrence of workout events in the console, including:

  • push-ups
  • sit-ups
  • burpees
  • jumps
  • squats
  • pull-ups

Prerequisites: Connecting with Aidlab requires a Bluetooth compatible machine.

Start with these three steps:

  1. Create an empty directory with a file.
  2. Install the AidlabSDK with pip installer:
pip install AidlabSDK

Basic activities counting is a relatively simple task as only two components are needed. Firstly, the accelerometer signal, which shows the changes in acceleration during the exercise/movement execution. The second is the orientation.

To successfully detect an activity, tiny acceleration changes during the workout are measured along with the information on whether the person is facing the floor. This is exactly what Aidlab does behind the scenes if we subscribe to the motion and orientation signals:

devices = await AidlabManager.scan()
if len(devices) > 0:
    aidlab = devices[0]
    await aidlab.connect(DataType.MOTION, DataType.ORIENTATION)

Getting the correct orientation is a bit tricky, fortunately though, subscribing to the motion and orientation signals starts the workout detection automatically (through a co-processor known as workout detector), meaning we don't have to reinvent the wheel. Workout detector has many useful functions, one of them being a push-up counter.

To handle the workout events we have to create a WorkoutDetector class. That class should implement DeviceDelegate:

import asyncio
from aidlab import DeviceDelegate 

class WorkoutDetector(DeviceDelegate):

    async def run(self):
        devices = await AidlabManager.scan()
        if len(devices) > 0:
            await devices[0].connect(self, [DataType.MOTION, DataType.ORIENTATION])
            while True:
                await asyncio.sleep(1)

    def did_connect(self, device):
        print("Connected to:", device.address)

    def did_disconnect(self, device):
        print("Disconnected from:", device.address)

    def did_detect_exercise(self, device, exercise):

Now, the callback from workout detector is invoked every time a new activity is recognized.

For further experimentation, we encourage you to play with the raw motion data (accelerometer and quaternion of orientation) in order to build your own workout detector.

The full code is available here, in the file.

Back to Blog



Join our community for exclusive health tips, product updates, and more.


Aidlab's Logo

Aidlab™ is a registered trademark. Copyright © 2024

Aidlab™ is a registered trademark. Copyright © 2024