In this tutorial you will learn how to get started with Machine Learning on your Bangle.js watch. Specifically you will build and train a model learning to recognize different movements of your watch hand. The steps include how to collect data, how to use Edge Impulse for the machine learning part and how to finally upload the learned model back to the watch and utilise it there.
Hardware
Software
Gesture Test
on your watch from the Bangle App LoaderThis part will guide you how to use your watch to collect multiple samples for one gesture type at a time.
event="left";
event="left";
for twitching your watch hand left and later on event="right";
for the opposite directionevent="<gesture>";
where <gesture>
is the hand movement you will collect;
!Gesture collecting code:
// ******* Gesture collecting code ********
name="Gesture";
event="left";
var fname = 1;
function gotGesture(d) {
var f = require("Storage").open(event + "." + fname + ".csv", "a");
print("timestamp, x, y, z");
f.write("timestamp, x, y, z\n");
for (var j=0;j<d.length;j+=3) {
print(j +", ", d[j] + ", " + d[j+1] + ", " + d[j+2] );
f.write(j + ", " + d[j] + ", " + d[j+1] + ", " + d[j+2] +"\n" );
}
g.clear();
g.setColor(1,1,1);
var my = g.getHeight()/2;
var sy = my/128;
var sx = g.getWidth()/(50*3);
g.drawLine(0,my,g.getWidth(),my);
for (var i=0;i<d.length-3;i+=3) {
for (var c=0;c<3;c++) {
g.setColor(c==0,c==1,c==2);
g.drawLine(i*sx, my+d[i+c]*sy, (i+3)*sx, my+d[i+c+3]*sy);
}
}
g.flip(1);
}
Bangle.on('gesture',gotGesture);
This part will guide you how to transfer the .CSV-files from your watch to your computer.
left.1.csv (StorageFile)
Save
(the floppy disc icon) for one file at a time and save the files to a folder of your choice, e.g. to c:\temp
This part will guide you how to split the .CSV-files you've downloaded from your watch into separate .CSV-files. The reason for this is that Edge Impulse requires one .CSV-file per sample.
PATENTS = ...
) with the full path and filename for the first file you want to split. I.e. the file you downloaded in previous steps.'timestamp, x, y, z'
in the original file and for each time (= sample) it finds, create a new file.left.1.csv (StorageFile)-15.csv
where -15
at the end is a running number.import re
PATENTS = 'C:/temp/left.1.csv (StorageFile)'
def split_file(filename):
# Open file to read
with open(filename, "r") as r:
# Counter
n=0
# Start reading file line by line
for i, line in enumerate(r):
# If line match with template -- <?xml --increase counter n
if re.match(r'timestamp, x, y, z', line):
n+=1
# This "if" can be deleted, without it will start naming from 1
# or you can keep it. It depends where is "re" will find at
# first time the template. In my case it was first line
if i == 0:
n = 0
# Write lines to file
with open("{}-{}.csv".format(PATENTS, n), "a") as f:
f.write(line)
split_file(PATENTS)
In this part you will learn how to upload the sample files you've created earlier, create a machine learning model, train and finally analyse it. This tutorial will only cover the essential steps needed for Bangle.js. To learn more about Edge Impulse, see e.g. getting started and continuous motion recognition.
Accelerometer data
when asked for the type of data you are dealing with.Let's get started
Data acquisition
from the left hand menuUpload existing data
Choose files
left.1.csv (StorageFile)-0.csv
.Automatically split between training and testing
and Infer from filename
should both be selectedBegin upload
- this will now quickly upload the files to your project.Done. Files uploaded successful: 85. Files that failed to upload: 0.
Job completed
left
and right
in this example) were automatically inferred from the filenames you used.An impulse takes raw data, uses signal processing to extract features, and then uses a learning block to classify new data. These steps will create an impulse.
Create impulse
Raw Data
processing blockClassification (Keras)
learning blockSave Impulse
Raw data
from the left hand menuSave parameters
which will take you to the second tab.Generate features
Feature explorer
. This gives you a 3D view of how well your data can be clustered into different groups. In an ideal situation all similar samples should be clustered into same group with a clear distinction between groups. If that's not the case, no worries at this point, the neural network algorithm will in many cases still be able to do a very good job!Here you will train the neural network and analyse its performance.
NN Classifier
from the left hand menuNumber of training cycles
to 100. This is another parameter to tweak, the higher this number is, the longer time the training will take, but also the better the network will perform, at least until it can't improve anymore.Start training
Here you will download the trained model to your computer.
Dashboard
from the left hand menuDownload block output
and click on the icon next to NN Classifier model TensorFlow Lite (int8 quantized)
This part will guide you how to transfer the model file from your computer to Bangle.js.
Upload a file
.tfmodel
and click Ok
left,right
.tfnames
and click Ok
Finally you will be able to test how well the trained model performs in real life! Just a few steps left.
left
or right
, will be shown in the left window in Espruino Web IDE as well as on your watch display.Bangle.on('aiGesture',(gesture,raw)=>print(gesture,raw));
Bangle.on('aiGesture',(gesture)=>{
E.showMessage(gesture);
setTimeout(()=>g.clear(), 1000);
});
First of all, hopefully you with this short tutorial were successful in training and recognising gesture events from your Bangle.js. Hopefully it also inspires you to try to improve the performance, e.g. by collecting more samples, by collecting more event types or by tweaking the different parameters and settings in Edge Impulse.
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.