65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Intel® Edison 板的加速度计预测设备中的用户活动

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (2投票s)

2016年8月19日

CPOL

3分钟阅读

viewsIcon

7452

此项目介绍如何使用连接到 Intel® Edison 开发板的 ADXL345 加速计生成的加速度数据来识别某些类型的人体物理活动。

获取新的 Intel® 物联网开发者套件,这是一个完整的硬件和软件解决方案,允许开发人员使用 Intel® Galileo 和 Intel® Edison 开发板创建令人兴奋的新解决方案。访问 Intel® 物联网开发者专区

此项目介绍如何使用连接到 Intel® Edison 开发板的 ADXL345 加速计生成的加速度数据来识别某些类型的人体物理活动。

人体活动识别具有广泛的应用,尤其是在可穿戴设备中。收集的数据可用于监控患者活动、运动员的锻炼模式、健身追踪等。

我们将使用支持向量机学习算法来实现这一点。该项目使用 LIBSVM 库实现,并分别用 Python 和 node.js 完成。

要识别的活动集包括跑步、行走、上下楼梯和休息。我们在一组时间间隔内收集加速度计数据,提取特征,在本例中,特征是沿 x、y 和 z 轴的加速度值。然后,我们使用这些数据来构建一个训练模型,该模型将进行活动分类。

项目代码可以从这里下载:下载

硬件实现。

下图显示了 ADXL345 到 Intel® Edison 开发板的引脚连接。

第 1 部分。Python 实现。

设置 LIBSVM

下载 LIBSVM 库,并使用 WINSCP 将 LibSVM 压缩文件夹传输到 Intel® Edison 开发板根目录。然后运行以下命令解压缩它

tar –xzf libsvm-3.21.tar.gz

在 libsvm-3.21 目录中运行 make

在 libsvm-3.21/python 目录中运行 make

在 libsvm-3.21/python 目录中创建 predict-activity.py python 脚本

从 ADXL345 加速度计读取数据

import pyupm_adxl345 as adxl345
# Create an I2C accelerometer object
adxl = adxl345.Adxl345(0)
while True:
    adxl.update() # Update the data
    raw = adxl.getRawValues() # Read raw sensor data
    force = adxl.getAcceleration() # Read acceleration force (g)
   
    forceX=format(force[0],'.2f')
    forceY=format(force[1],'.2f')
    forceZ=format(force[2],'.2f')

sleep(2)

对各种活动进行分类

训练文件包含分类为以下各项的各种活动的实例;

0-行走

1-跑步/慢跑

2-上下楼梯

3-休息

以下是训练文件一小部分的屏幕截图

运行网格搜索以确定 C 的最佳值

参数C 控制 SVM 在训练数据上的误差与裕量最大化之间的权衡。C 在训练阶段使用,并说明在计算支持向量时,有多少异常值被考虑在内。

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]

对各种用户活动进行分类

#load the LIBSVM shared library:
from svmutil import *
#Construct an svm_problem instance where the activity.ds is the dataset with the training data
y, x = svm_read_problem('activity.ds')
where y is the tuple of labels representing the various activities and x is a tuple of data instances representing acceleration values from x,y and z axes

m = svm_train(y[0:], x[0:], '-c 0.03125 -h 0 -b 1 -q')
#y[0:] and x[0:] means we train the model on the whole dataset array from the first instance[0] to the last
#-h disable shrinking heuristics
#-c : set the cost parameter C of C-SVC
#-q: quiet mode (no outputs)

values=[[float(forceX),float(forceY), float(forceZ)]]
#forceX,forceY and forceZ are data instances of the accelerometer values in x,y,z axes

 p_labels,p_acc,p_values = svm_predict([0]*len(values),values,m])
#pass the accelerometer values for prediction in the svm_predict() method which returns 
#p_labels: a list of predicted labels   p_acc: a tuple including accuracy and p_values: a list of decision values or probability estimates (if '-b 1' is specified)
print p_labels
#output the predicted labels to represent the predicted activity

第 2 部分。Node.Js 实现

在开发板的主根文件夹中创建一个新项目目录并安装 node-svm

npm install node-svm

将 build 和 lib 文件夹从 node-modules/node-svm 复制到项目目录。

接下来,运行以下命令安装 node-svm 所需的软件包

npm install <package-name>

这些包是 Stringify-object、Mout、Graceful-fs、Optimist、Osenv、Numeric、Q 和 underscore。

从 ADXL345 加速度计读取数据

Reading data from the ADXL345 accelerometer
var adxl345 = require('jsupm_adxl345');
var adxl = new adxl345.Adxl345(0);

setInterval(function()
{
    adxl.update(); // Update the data
    var raw = adxl.getRawValues(); // Read raw sensor data
    var force = adxl.getAcceleration(); // Read acceleration force (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);

我们现在可以编写代码来使用我们在第 1 部分中使用 Python 运行网格搜索确定的 c 值对用户活动进行分类。

var so = require('stringify-object');
var svm = require('./lib');
var numeric = require('numeric');
var fs = require('fs');
var fileName = './activity.ds';

//create the classifier
var clf = new svm.CSVC({
    c: 0.03125,
    normalize: false,
    reduce: false,
});

//Build the training model
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.');
    });

//Classify data instance at some interval
var prediction=clf.predictSync([forceX, forceY, forceZ]);
var probability=clf.predictProbabilitiesSync([forceX, forceY, forceZ]);
console.log(prediction);

摘要

该项目涵盖了如何使用网格搜索确定分类器的训练参数,以及运行分类器以能够使用 Python 和 node.js 中的 LIBSVM 准确预测各种活动。SVM 需要大量的处理能力和内存;Intel® Edison 开发板有效地满足了这些要求,从而使该项目获得成功。你会做什么?

© . All rights reserved.