树莓派+阿里云IoT人脸识别场景实战-阿里云开发者社区

开发者社区> 《IoT物联网技术》> 正文

树莓派+阿里云IoT人脸识别场景实战

简介: 树莓派+阿里云IoT人脸识别场景实战
+关注继续查看

1.整体架构

基于阿里云的Serverless架构

2.阿里云产品

IoT平台:https://www.aliyun.com/product/iot

函数计算:https://www.aliyun.com/product/fc

表格存储:https://www.aliyun.com/product/ots

OSS存储:https://www.aliyun.com/product/oss

人脸识别:https://data.aliyun.com/product/face

3.设备采购

名称 图片 购买
摄像头 1535443297032-5e1393f0-6a8c-48f3-bc41-a5 淘宝
树莓派 1535443375085-a5ca4389-931f-4967-b08c-4e 淘宝

4.树莓派设备端开发

4.1 Enable Camera

image.png | left | 300x273.015873015873

4.2 目录结构

  1. 在/home/pi目录下创建 iot文件夹,
  2. 在/home/pi/iot创建 photos文件夹,iot.cfg配置文件,iot.py文件

image.png | left | 400x252.38095238095238

4.3 Python3程序

4.3.1 安装依赖

pip3 install oss2
pip3 install picamera
pip3 install aliyun-python-sdk-iot-client

4.3.2 iot.cfg配置文件

[IOT]
productKey = xxx
deviceName = xxx
deviceSecret = xxx

[OSS]
ossAccessKey = xxx
ossAccessKeySecret = xxx
ossEndpoint = xxx
ossBucketId = xxx

4.3.3 iot.py应用程序

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import oss2
from picamera import PiCamera
import time
import aliyunsdkiotclient.AliyunIotMqttClient as AliyunIot
import configparser

config = configparser.ConfigParser()
config.read('iot.cfg')

# IoT
PRODUCE_KEY = config['IOT']['productKey']
DEVICE_NAME = config['IOT']['deviceName']
DEVICE_SECRET = config['IOT']['deviceSecret']

HOST = PRODUCE_KEY + '.iot-as-mqtt.cn-shanghai.aliyuncs.com'
SUBSCRIBE_TOPIC = "/" + PRODUCE_KEY + "/" + DEVICE_NAME + "/control";
# oss
OSS_AK = config['OSS']['ossAccessKey']
OSS_AK_SECRET = config['OSS']['ossAccessKeySecret']
OSS_ENDPOINT = config['OSS']['ossEndpoint']
OSS_BUCKET_ID = config['OSS']['ossBucketId']

auth = oss2.Auth(OSS_AK, OSS_AK_SECRET)
bucket = oss2.Bucket(auth, OSS_ENDPOINT, OSS_BUCKET_ID)

camera = PiCamera()
camera.resolution = (720,480)

# Take a photo first, then upload photo to oss
def take_photo():
    ticks = int(time.time())
    fileName = 'raspi%s.jpg' % ticks
    filePath = '/home/pi/iot/photos/%s' % fileName
    # take a photo
    camera.capture(filePath)
    # upload to oss
    bucket.put_object_from_file('piPhotos/'+fileName, filePath)


def on_connect(client, userdata, flags, rc):
    print('subscribe '+SUBSCRIBE_TOPIC)
    client.subscribe(topic=SUBSCRIBE_TOPIC)


def on_message(client, userdata, msg):
    print('receive message topic :'+ msg.topic)
    print(str(msg.payload))
    take_photo()


if __name__ == '__main__':
    client = AliyunIot.getAliyunIotMqttClient(PRODUCE_KEY,DEVICE_NAME, DEVICE_SECRET, secure_mode=3)
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(host=HOST, port=1883, keepalive=60)
    # loop
    client.loop_forever()

5.函数计算开发

5.1 index.js应用程序

const request = require('request');
const url = require('url');
const crypto = require('crypto');
const TableStore = require('tablestore');
const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;

const config = require("./config");

//iot client
const iotClient = new RPCClient({
    accessKeyId: config.accessKeyId,
    secretAccessKey: config.secretAccessKey,
    endpoint: config.iotEndpoint,
    apiVersion: config.iotApiVersion
});
//ots client
const otsClient = new TableStore.Client({
    accessKeyId: config.accessKeyId,
    secretAccessKey: config.secretAccessKey,
    endpoint: config.otsEndpoint,
    instancename: config.otsInstance,
    maxRetries: 20
});

const options = {
    url: config.dtplusUrl,
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-type': 'application/json'
    }
};

module.exports.handler = function(event, context, callback) {

    var eventJson = JSON.parse(event.toString());

    try {
        var imgUrl = config.ossEndpoint + eventJson.events[0].oss.object.key;

        options.body = JSON.stringify({ type: 0, image_url: imgUrl });
        options.headers.Date = new Date().toUTCString();
        options.headers.Authorization = makeDataplusSignature(options);

        request.post(options, function(error, response, body) {

            console.log('face/attribute response body' + body)
            const msg = parseBody(imgUrl, body)
            //
            saveToOTS(msg, callback);

        });
    } catch (err) {
        callback(null, err);
    }
};

parseBody = function(imgUrl, body) {

    body = JSON.parse(body);
    //face_rect [left, top, width, height],
    const idx = parseInt(10 * Math.random() % 4);
    const age = (parseInt(body.age[0])) + "岁";
    const expression = (body.expression[0] == "1") ? config.happy[idx] : config.normal[idx];
    const gender = (body.gender[0] == "1") ? "帅哥" : "靓女";
    const glass = (body.glass[0] == "1") ? "戴眼镜" : "火眼金睛";

    return {
        'imgUrl': imgUrl,
        'gender': gender,
        'faceRect': body.face_rect.join(','),
        'glass': glass,
        'age': age,
        'expression': expression
    };
}

//pub msg to WebApp by IoT
iotPubToWeb = function(payload, cb) {
    co(function*() {
        try {
            //创建设备
            var iotResponse = yield iotClient.request('Pub', {
                ProductKey: config.productKey,
                TopicFullName: config.topicFullName,
                MessageContent: new Buffer(JSON.stringify(payload)).toString('base64'),
                Qos: 0
            });
        } catch (err) {
            console.log('iotPubToWeb err' + JSON.stringify(err))
        }

        cb(null, payload);
    });
}

saveToOTS = function(msg, cb) {

    var ots_data = {
        tableName: config.tableName,
        condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),

        primaryKey: [{ deviceId: "androidPhoto" }, { id: TableStore.PK_AUTO_INCR }],

        attributeColumns: [
            { 'imgUrl': msg.imgUrl },
            { 'gender': msg.gender },
            { 'faceRect': msg.faceRect },
            { 'glass': msg.glass },
            { 'age': msg.age },
            { 'expression': msg.expression }
        ],

        returnContent: { returnType: TableStore.ReturnType.Primarykey }
    }

    otsClient.putRow(ots_data, function(err, data) {

        iotPubToWeb(msg, cb);
    });
}

makeDataplusSignature = function(options) {

    const md5Body = crypto.createHash('md5').update(new Buffer(options.body)).digest('base64');

    const stringToSign = "POST\napplication/json\n" + md5Body + "\napplication/json\n" + options.headers.Date + "\n/face/attribute"
    // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )]
    const signature = crypto.createHmac('sha1', config.secretAccessKey).update(stringToSign).digest('base64');

    return "Dataplus " + config.accessKeyId + ":" + signature;
}

5.2 config.js配置文件

module.exports = {
    accessKeyId: '账号ak',
    secretAccessKey: '账号ak secret',
    iotEndpoint: 'https://iot.cn-shanghai.aliyuncs.com',
    iotApiVersion: '2018-01-20',
    productKey: 'web大屏产品pk',
    topicFullName: 'web大屏订阅识别结果的topic',

//可选,如果不保存结果,不需要ots
    otsEndpoint: 'ots接入点',
    otsInstance: 'ots实例',
    tableName: 'ots结果存储表',
}

6. Web端App开发

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>阿里云IoT</title>
    <style type="text/css">
    body {
        line-height: 1.6;
        font-family: Arial, Helvetica, sans-serif;
        margin: 0;
        padding: 0;
        background: url(http://iot-face.oss-cn-shanghai.aliyuncs.com/iot-face-yq.png) no-repeat;
        background-color: #202124;
    }

    .face-msg {
        display: inline;
        font-size: 32px;
        color: #5FFFF8;
        padding: 30px 160px 0px 60px;
    }
    </style>
</head>

<body>
    <div style="padding: 190px 10px 0px 360px;">
        <div class="face-msg" id='glass' style="color: #5FFFF8"></div>
        <div class="face-msg" id='gender' style="color: #FF5FE5"></div>
        <div class="face-msg" id='age' style="color: #FFDD5F"></div>
        <div class="face-msg" id='expression' style="color: #FC4D4D"></div>
    </div>
    <!-- -->
    <div style="position: relative;padding: 145px 10px 0px 165px;">
        <div style="position: absolute;">
            <canvas id="myCanvas" width="720px" height="480px"></canvas>
        </div>
        <img id='imageUrl' src="" width="720px" height="480px" />
    </div>
    <script type="text/javascript" src="http://iot-face.oss-cn-shanghai.aliyuncs.com/zepto.min.js"></script>
    <script src="http://iot-face.oss-cn-shanghai.aliyuncs.com/mqttws31.min.js" type="text/javascript"></script>
    <script type="text/javascript">

    $(document).ready(function() {

        initMqtt();
    });

    var client;

    function initMqtt() {
        //模拟设备参数
        var mqttClientId = Math.random().toString(36).substr(2);
        client = new Paho.MQTT.Client("public.iot-as-mqtt.cn-shanghai.aliyuncs.com", 443, mqttClientId);

        // set callback handlers
        var options = {
            useSSL: false,
            userName: '替换iotId',
            password: '替换iot token',
            keepAliveInterval: 60,
            onSuccess: onConnect,
            onFailure: function(e) {
                console.log(e);
            }
        };

        client.onConnectionLost = onConnectionLost;
        client.onMessageDelivered = onMessageDelivered;
        client.onMessageArrived = onMessageArrived;

        // connect the client
        client.connect(options);
    }

    // called when the client connects
    function onConnect() {
        // Once a connection has been made, make a subscription
        client.subscribe("替换订阅数据更新topic");
    }

    // called when the client loses its connection
    function onConnectionLost(responseObject) {
        if (responseObject.errorCode !== 0) {
            console.error("onConnectionLost:", responseObject);
        }
    }

    function onMessageArrived(message) {
        fillData(JSON.parse(message.payloadString))
    }

    function onMessageDelivered(message) {
        console.log("onMessageDelivered: [" + message.destinationName + "] --- " + message.payloadString);
    }

    function fillData(data) {

        $("#age").html(data.age);
        $("#expression").html(data.expression);
        $("#gender").html(data.gender);
        $("#glass").html(data.glass);

        $("#imageUrl").attr("src", data.imgUrl);

        var rect = data.faceRect.split(","); //"270,22,202,287"

        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.strokeStyle = '#03A9F4';
        ctx.lineWidth = 2;

        ctx.beginPath();
        ctx.rect(rect[0], rect[1], rect[2], rect[3]);
        ctx.stroke();
    };
    </script>
</body>

</html>

7. 拍照指令触发器

/**
 * package.json 添加依赖:"@alicloud/pop-core": "1.5.2"
 */
const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;

const options = {
    accessKey: "替换ak",
    accessKeySecret: "替换ak Secret",
};

//1.初始化client
const client = new RPCClient({
    accessKeyId: options.accessKey,
    secretAccessKey: options.accessKeySecret,
    endpoint: 'https://iot.cn-shanghai.aliyuncs.com',
    apiVersion: '2018-01-20'
});

const params = {
    ProductKey: "a1p35XsaOS7",
    TopicFullName: "相机指令topic",
    MessageContent: new Buffer('{"action":"takephoto"}').toString('base64'),
    Qos: "0"
};

co(function*() {
    try {
        //3.发起API调用
        const response = yield client.request('Pub', params);
        console.log(JSON.stringify(response));
    } catch (err) {
        console.log(err);
    }
});

IoT物联网技术 公共账号

1526534749776-9c13a944-f5bd-4a1a-981c-96

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
谷歌Android things物联网硬件接入阿里云IoT物联网套件实战
谷歌Android things物联网硬件接入阿里云IoT物联网套件云端实战,DHT12传感器,甲醛传感器,NXP i.MX7D开发板
2883 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
4151 0
打造更适合IoT场景的消息队列实践
随着接触客户越来越多,也越来越颠覆了我对“传统队列”(kafka、rocketmq、rabbitmq...)的看法。 当然本文不是说“传统队列”做得不好, 这些队列系统经过多年打磨,在高性能、海量堆积、消息可靠性等诸多方面都已经做得非常极致了,都做得非常的优秀。 但今天我觉得大家在设计方案时动不动任何一个异步、系统解耦等就来选用队列,然后线上又频繁出一些问题,这些问题的背后我们得看看到底什么场景适合、什么场景过渡使用了、有没有更好解法, 尤其今天IoT领域场景复杂,既有面向自身SAAS业务又要承担多租户PAAS平台化模式,面临更多的队列方面问题,拿来和大家讨论分享。
2439 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
4623 0
IoT平台实现RRPC同步调用最佳实战
满足云端下发指令给设备端,同时需要设备端返回响应结果的场景
5170 0
带你读《OpenCV 4计算机视觉项目实战 (原书第2版)》之一:OpenCV入门
本书首先介绍OpenCV的入门知识及安装,然后介绍OpenCV的基础知识,包括用户界面、矩阵运算、滤波器和直方图等,之后介绍复杂的计算机视觉算法,包括对象分割和分类、视频监控、对象跟踪等,后探讨对象跟踪、文本识别、机器学习和人脸检测等高级技术。阅读本书之后,你将掌握常用和新的计算机视觉技术,并学会从零开始构建各类OpenCV项目。
1819 0
Ionic 入门与实战之第一章:Ionic 介绍与相关学习资源
原文发表于我的技术博客 本文是「Ionic 入门与实战」系列连载的第一章,主要对 Ionic 的概念、发展历程、适配的移动平台等知识进行了介绍,并分享了 Ionic 相关的学习资源。原文发表于我的技术博客 1. Ionic 简介 在前端技术发展越来越快的今天,Hybrid App 开发技术也受到了很大的影响。
1285 0
+关注
《IoT物联网技术》
公众号《IoT物联网技术》文章同步
87
文章
7
问答
文章排行榜
最热
最新
相关电子书
更多
《SaaS模式云原生数据仓库应用场景实践》
立即下载
文娱运维技术
立即下载
《看见新力量:二》电子书
立即下载