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

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

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2016年11月16日

CPOL

3分钟阅读

viewsIcon

20170

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

获取全新的 Intel® IoT 开发者套件,这是一个完整的硬件和软件解决方案,使开发者可以使用 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.