摘要:整體架構基于阿里云的架構運行效果阿里云產品平臺函數計算表格存儲存儲人臉識別設備采購名稱圖片購買攝像頭淘寶樹莓派淘寶樹莓派設備端開發目錄結構在目錄下創建文件夾,在創建文件夾,配置文件文件程序安裝依賴
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.設備采購名稱 | 圖片 | 購買 | ||
---|---|---|---|---|
攝像頭 | ![image.png | left | 155x144.9410029498525](https://cdn.nlark.com/yuque/0... "") | 淘寶 |
樹莓派 | ![image.png | left | 155x144.82939632545933](https://cdn.nlark.com/yuque/0... "") | 淘寶 |
在/home/pi目錄下創建 iot文件夾,
在/home/pi/iot創建 photos文件夾,iot.cfg配置文件,iot.py文件
4.3 Python3程序 4.3.1 安裝依賴pip3 install oss2 pip3 install picamera pip3 install aliyun-python-sdk-iot-client4.3.2 iot.cfg配置文件
[IOT] productKey = xxx deviceName = xxx deviceSecret = xxx [OSS] ossAccessKey = xxx ossAccessKeySecret = xxx ossEndpoint = xxx ossBucketId = xxx4.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 application/json " + md5Body + " application/json " + options.headers.Date + " /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開發
7. 拍照指令觸發器阿里云IoT
/** * 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); } });
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/42595.html
摘要:近幾年行動通訊人工智能物聯網及相關是熱門應用,而且未來也展現出萬物皆智能的趨勢。這其中邊緣運算也稱邊緣計算是一股即將興起的風潮。甚至應用于物聯網非實時性小型傳感器網絡的智能邊緣運算的普通單芯片如系列也可都算是邊緣運算。近幾年行動通訊、人工智能、物聯網及相關是熱門應用,而且未來也展現出「萬物皆智能(AIoT = AI + IoT)」的趨勢。不論是家電、交通、零售、虛擬擴增實境(AR/VR/XR...
摘要:識別出人臉后,要算寬度,要在一定的寬度才算人臉,可以裁剪出正方形發送檢索人臉。 人臉簽到 花了一個星期做了人臉簽到的demo,github地址歡迎star,在線預覽大屏幕demo 先說些廢話,以前做年會的抽獎,感覺好傻,現在正好在學threejs,就想做個這樣的場景來抽獎的方式,又在學人臉識別的知識,就想做人臉識別簽到,就想都做在瀏覽器中 體驗完整過程 1.微信掃描注冊上傳頭像 s...
摘要:同時通過云攝像頭的圖像采集,圖像分析,結合人臉識別,以及阿里沉淀的人臉底庫,我們目前正在做銀泰場內的智能客流系統。文/阿里云MVP 銀泰技術高級經理 賈爽 相關免費課程《銀泰新零售上云解決方案精講》上線中立足實戰 講透經典案例 助你快速理解新零售 第一節學習地址第二節學習地址 安全策略的部署和風險防控先來說一下邊界安全,下圖是銀泰邊界安全所接入的所有阿里云產品: 我們結合了阿里云安全產...
閱讀 1876·2021-09-28 09:36
閱讀 2426·2021-09-08 09:35
閱讀 3067·2019-08-30 15:53
閱讀 1554·2019-08-30 14:08
閱讀 665·2019-08-29 18:40
閱讀 2843·2019-08-29 13:57
閱讀 2702·2019-08-29 13:55
閱讀 681·2019-08-26 13:45