Exercising is an important part to good health and well-being. Setting exercise/workout goals is a simple way to motivate and keep yourself on track. Of course, it's not so easy to get yourself up to exercise, let alone record your progress as you do it. You need to bring your notebook or whip out your cellphone to record the repetitions and sets, which increases distraction and lowers productivity.
This project tackles the analog way of logging and analyzing workouts/exercises by automating this process. The device autonomously logs the type of exercise and number of repetitions using AI Studio. The data is then visualized using Python and PyQt5.
I am using a Raspberry Pi as the gateway for my SmartEdge agile device. I used Alasdair Allan's guide found here: https://blog.hackster.io/hands-on-with-the-smartedge-agile-b7b7f02b5d4b. The guide also goes through linking the SmartEdge Agile device to the Raspberry pi gateway. I suggest going over it as it is intuitive and easy to follow. As a quick guide, this is what I did:
- SSH into the raspberry pi
- Runthe commands:
wget https://brainium.blob.core.windows.net/public/raspberry/bluez.run sudo sh bluez.run
wget https://brainium.blob.core.windows.net/public/raspberry/brainium-gateway-latest.run
sudo sh ./brainium-gateway-latest.run
sudo reboot now
- Link the SmartEdge Agile device to the gateway using the Brainium Portal
Artificial Intelligence training time! I don't have much background with training AI so it's really handy that most of the process is streamlined and automated through Brainium. It is easy to create models by creating sample sets to the motion that you want the AI to recognize. The drawback is that the initial signal needs to be captured cleanly or else the following data will be construed. Which means, the data set will not represent the action that you want recognized correctly.
A detailed walkthrough can be referenced from Brainium's webinar: https://www.hackster.io/videos/330. A quick breakdown of the steps I took:
- In the 'Motion Recognition' pane, create a new motion
- Hit the 'Record New Set' to acquire data from the device
- Set the device to get the data from and set the number of repetitions then click 'Start Record'
- Do the actions to be recognized then hit "Rec' and 'Save' when done
- Selectallthe training sets and click 'Generate Model'button
Your training set will look like the image below, but better (hopefully).
It is important to do the motion to be recognized in between long intervals or space them apart. In this way, the data will look cleaner and be more reliable once the data is used to train the model.
Anyawys, after doing several recording and training sessions, the 'Learning Pipeline' and 'Model List' tab should look like this:
I am using Visual Studio Code as my code editor and Python as my language of choice. VS Code needs some initial setup before python can run properly on it. A handy guide can be found here: https://code.visualstudio.com/docs/python/python-tutorial
Here is the breakdown of the steps I took:
- Install Python3.7
- Install the Python extension for VS Code
- Create a workspace folder using a command line interface (command prompt, powershell, etc.)
mkdir hello
cd hello
code .
- Run a "Hello World" script as a sanity check
msg = "Hello World"
print(msg)
This only applies if you are working with Windows and VS Code. This can be skipped if you already have something setup.
Creating the Graphical User Interface (GUI)According to the interwebz, the best practice is to create an environment so we don't mess with the original python libraries/files. This makes it easier to start from scratch if something catastrophic happens.
python -m venv .venv
.venv\scripts\activate
Now install pyqt5 and the designer tools for our GUI needs
python -m pip install pyqt5
python -m pip install pyqt5-tools
After the installation, run the PyQt Designer with the command:
designer
It'll open a window like the figure below. Highlight 'MainWindow' and click 'Create'. This should give you a blank slate where you can drag and drop the elements that you want in the window. For this mini-project, I am using Labels, LCD Numbers, and Progress Bars.
I played around with sizing and placement to make it look more presentable.
We can then save the file as "GymBuddy_GUI.ui" within our work folder. As a another sanity check, we can run a python script that shows the ui file we just saved.
from PyQt5 import QtWidgets, uic
import sys
app = QtWidgets.QApplication([])
win = uic.loadUi("GymBuddy_GUI.ui") #specify the location of your .ui file
win.show()
sys.exit(app.exec())
If the GUI doesn't show up, it's time for some troubleshooting. Make sure the file is saved within the folder or specify the actual location of the file as labeled on the python script.
Convert the GUI.ui file to.pyIn order to import the GUI into another python script, we need to convert the.ui file to a.py file. We can use the handy command pyuic5 as seen below.
pyuic5 GymBuddy_GUI.ui -o GymBuddy_GUI.py
Create and Edit the CodeThe final ingredient is the GymBuddyTracker.py file. You can download/copy the file and add in your credentials:
- mqtt_password - also known as the External access token found on Brainium's 'Profile' tab
- user_id - this is also found on the 'Profile' tab under 'User ID'
- device_id - this is the SmartEdge Agile device's ID (not to be confused with the gateway ID!!!!)
mqtt_user_name = 'oauth2-user'
mqtt_password = 'insertyourmqttpasswordhere' # copy and paste here your external access token
user_id = 'insertyourusedidhere' # copy and paste here your user id
device_id = 'insertyourdeviceidhere' # copy and paste here your device id
The progress bar can be customized by changing the maximum values within the setup_ui(). My exercise goals are the following: 30 push-ups, 30 squats, and 10 pulI-ups, so I have set the .setMaximum values to 30, 30, and 10 respectively.
self.ui.progressBar.setMinimum(0)
self.ui.progressBar.setMaximum(30)
self.ui.progressBar.setValue(0)
self.ui.progressBar_2.setMinimum(0)
self.ui.progressBar_2.setMaximum(30)
self.ui.progressBar_2.setValue(0)
self.ui.progressBar_3.setMinimum(0)
self.ui.progressBar_3.setMaximum(10)
self.ui.progressBar_3.setValue(0)
Optional: Convert the Pyhton Script to an ExecutableHere's a fact: Double-clicking > typing and running a script.
To make life easier, convert the Python script to an executable using the command:
pyinstaller --onefile --windowed GymBuddyTracker.py
This will generate a GymBuddyTracker.exe inside a 'dist' folder. Running this as is will give an error because it needs the certificate to run and connect to the server properly. So cut and paste.exe file and place it with the cert file.
Run it and Exercise!Setting an exercise goal is fulfilling when met. But it gets tedious keeping track of everything manually. With the help of SmartEdge Agile and Brainium, it is easy to keep track of your exercise routine. The project is a prototype of what can be done. It can be expanded further to keep track of a team's exercising efforts or simply a type of leaderboard for exercises in a gym.
Comments