Today we will talk about a project aimed at recognizing certain types of human physical activity. This is done using the Intel Edison board, to which the ADXL345 accelerometer is connected.
Data on what exactly the user is busy can find many uses. This is especially true of wearable electronics. For example, in the health care environment, such information can be used to monitor patients, in sports, to analyze the characteristics of exercise and fitness tracking.
In our project, the Support Vector Machine (SVM) method is used to analyze accelerometer data. The software is implemented using the popular library LIBSVM. The code is written in two versions: in Python and Node.js.
The system can recognize the following types of physical activity: running, walking, lifting and lowering the stairs. She is also able to respond to a state of rest. We collect accelerometer readings at some intervals, extract the values ​​of interest to us, in this case, the acceleration values ​​along the X, Y and Z axes. Then these data are used to create a machine learning system that recognizes exactly what a person is doing.
')
Hardware implementation
This is how the ADXL345 accelerometer is connected to Intel Edison.
Connect Accelerometer to EdisonPython implementation
â–ŤSetting LIBSVM
First you need to download the
LIBSVM library and transfer the LibSVM archive to the Intel Edison root folder using WINSCP. Then the archive should be unpacked with the following command:
tar –xzf libsvm-3.21.tar.gz
Now you need to run make in the libsvm-3.21 folder, then make in libsvm-3.21 / python.
After that, create a python script predict-activity.py in the libsvm-3.21 / python directory.
Accelerometer reading
Here is how we read the accelerometer data.
import pyupm_adxl345 as adxl345 # I2C- adxl = adxl345.Adxl345(0) while True: adxl.update() # raw = adxl.getRawValues() # force = adxl.getAcceleration() # (g) forceX=format(force[0],'.2f') forceY=format(force[1],'.2f') forceZ=format(force[2],'.2f') sleep(2)
â–Ť Classification of various types of physical activity
The training data file contains entries for the following types of physical activity:
0 - walking.
1 - run.
2 - raising or lowering the stairs.
3 - rest.
Here is a fragment of this file:
Data file for system learningâ–ŤSelection of the best value of the parameter C
The parameter C in the implementation of the support vector method allows you to control the relationship between SVM errors on training data and maximize the width of the border between classes. It is used in training the model and indicates that emissions are taken into account when finding support vectors. We select this parameters using the grid search method.
from svmutil import * import numpy as nu param = svm_parameter("-q -h 0") y, x = svm_read_problem('activity.ds') problem = svm_problem(y[:100], x[:100]) results = [] for c in range(-10,20): for g in range(-10,5): param.C, param.gamma = 2**c, 2**g m = svm_train(problem,param) p_lbl, p_acc, p_val = svm_predict(y[100:],x[100:],m) results.append([param.C, param.gamma, p_acc[0]]) bestIdx = nu.argmax(nu.array(results)[:,2]) print results[bestIdx]
Grid Search Resultsâ–Ť Classification of physical activity
Here is the code that does what determines what the person is doing.
# LIBSVM: from svmutil import * # svm_problem, activity.ds y, x = svm_read_problem('activity.ds') # y – , , x – , X, Y, Z m = svm_train(y[0:], x[0:], '-c 0.03125 -h 0 -b 1 -q') #y[0:] x[0:] , #-h #-c : C C-SVC #-q: values=[[float(forceX),float(forceY), float(forceZ)]] #forceX,forceY forceZ p_labels,p_acc,p_values = svm_predict([0]*len(values),values,m]) # svm_predict(), : #p_labels: # p_acc: #p_values: ( '-b 1') print p_labels #
Implementation on Node.js
â–ŤPreparation for work with the node-svm package
Create a folder for the project in the board’s home directory and install node-svm:
npm install node-svm
Copy the build and lib folders from node-modules / node-svm to the project folder. Next, install the packages necessary for node-svm to work. This requires a command of this type:
npm install <package-name>
We will need the following packages:
Stringify object.
- Mout.
- Graceful-fs.
- Optimist.
- Osenv.
- Numeric.
- Q.
- underscore
â–ŤWork with accelerometer
Here is the code to read the accelerometer reading.
var adxl345 = require('jsupm_adxl345'); var adxl = new adxl345.Adxl345(0); setInterval(function() { adxl.update(); // var raw = adxl.getRawValues(); // var force = adxl.getAcceleration(); // (g) var rawvalues = raw.getitem(0) + " " + raw.getitem(1) + " " + raw.getitem(2); //console.log("Raw Values: " + rawvalues); var forceX=force.getitem(0).toFixed(2); var forceY=force.getitem(1).toFixed(2); var forceZ=force.getitem(2).toFixed(2); }, 2000);
Now you can write a program to analyze and classify user activity using the value of the C parameter obtained after performing a grid search using Python.
var so = require('stringify-object'); var svm = require('./lib'); var numeric = require('numeric'); var fs = require('fs'); var fileName = './activity.ds'; // var clf = new svm.CSVC({ c: 0.03125, normalize: false, reduce: false, }); // svm.read(fileName) .then(function (dataset) { return clf.train(dataset) .progress(function (progress) { console.log('training progress: %d%', Math.round(progress * 100)); }); }) .spread(function (model, report) { console.log('SVM trained. \nReport:\n%s', so(report)); }).done(function () { console.log('done.'); }); // var prediction=clf.predictSync([forceX, forceY, forceZ]); var probability=clf.predictProbabilitiesSync([forceX, forceY, forceZ]); console.log(prediction);
The results of the program on Node.jsResults
As you can see, on Intel Edison, you can create a system for recognizing human physical activity, which can be used in the field of wearable devices. To do this, you only need to connect the accelerometer to the board and write a program. Edison's computational capabilities and the amount of its RAM is enough to implement the resource-intensive SVM algorithm.
You can easily recreate our system using the above description and this
code . However, and most importantly, we hope that our story will inspire you to develop your own innovative IoT projects.