https://sssbin.tistory.com/188
AWS ๊ธฐ๋ฐ ์ฃผ์ฐจ ๊ด๋ฆฌ ์์คํ (2) Lambda ์ค์
https://sssbin.tistory.com/187 ๋ฒํธํ ์ด๋ฏธ์ง๋ฅผ S3์ ์ ๋ก๋ํ๊ณ request ํ ํฝ์ publish ํ๊ณ ์ด๋ ๋๋คํจ์๊ฐ ํธ์ถ๋์ด rekognition ์งํ ํ detect ํ " data-og-host="sssbin.tistory.com" data-og-source-ur..
sssbin.tistory.com
1. camera.js
// aws iot core
var awsIot = require('aws-iot-device-sdk');
// file system
var fs = require('fs');
// aws
var AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-2';
AWS.config.apiVersions = {
"s3" : '2006-03-01'
};
// ๋๋ฐ์ด์ค ์ค์
var camera = awsIot.device({
keyPath: './credentials/camera/private.pem.key',
certPath: './credentials/camera/certificate.pem.crt',
caPath: './credentials/camera/AmazonRootCA1.pem',
clientId : 'camera',
host: '์๋ํฌ์ธํธ'
});
var keys = require("./credentials/access-keys.js");
// s3 ์ค์
var s3 = new AWS.S3({
"accessKeyId" : keys.access_key,
"secretAccessKey" : keys.secret_key
});
// s3์ ์ฌ์ง ์
๋ก๋ํ ํ๋ผ๋ฏธํฐ
var params = {
Bucket : '๋ฒํท์ด๋ฆ',
Key : '๋ฒํท์ ์
๋ก๋ํ ์ฌ์ง ์ด๋ฆ', // S3 object
Body: fs.createReadStream("๋ด ์ปดํจํฐ์์ ์
๋ก๋ํ ์ฌ์ง ๊ฒฝ๋ก๋ช
")
}
// s3์ ์ฌ์ง ์
๋ก๋
function createObject(params) {
return new Promise(function (resolve, reject) {
s3.upload(params, function (err, data) {
if (err) reject(err);
else resolve(data);
})
});
}
// createObject()์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ
async function createDeploymentPackage (params) {
try {
var res = await createObject(params);
console.log(res);
} catch (err) { console.log(err); }
}
// create S3 object
createDeploymentPackage(params);
// carRecog/request ๋ก publish
camera.on('connect', function() {
console.log('Camera connected');
var message = {'detect' : 'carRecog/detect/car', 'image' : 'newcar'};
camera.publish('carRecog/request', JSON.stringify(message));
console.log('publish to carRecog/request -> '+JSON.stringify(message));
});
์๋ํฌ์ธํธ๋ AWS IoT > ์ค์ ์ ์๋ค.
S3 ๋ฒํท์ ์ฌ์ง์ ์ ๋ก๋ํ๊ณ
carRecog/request ํ ํฝ์ message๋ฅผ publish
2. index.js (๋๋ค์ฝ๋)
var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({
endpoint: '์๋ํฌ์ธํธ',
});
var client = new AWS.Rekognition();
var bucket = '๋ฒํท ์ด๋ฆ';
exports.handler = async function (event) {
var photo_target = event.image + '.jpg';
const params = {
Image: {
S3Object: {
Bucket: bucket,
Name: photo_target
},
},
};
var detectedText;
await client.detectText(params, function(err, response) { // rekognition
if (err) {
console.log(err, err.stack); // handle error if an error occurred
} else {
console.log(`Detected Text for: ${photo_target}`)
console.log(response)
response.TextDetections.forEach(label => {
console.log(`Detected Text: ${label.DetectedText}`),
console.log(`Type: ${label.Type}`),
console.log(`ID: ${label.Id}`),
console.log(`Parent ID: ${label.ParentId}`),
console.log(`Confidence: ${label.Confidence}`),
console.log(`Polygon: `)
console.log(label.Geometry.Polygon)
if (label.Id == 0) detectedText = label.DetectedText.replace(/ /g, "");
// ์ธ์ํ ์ ์ฒด ํ
์คํธ -> ๊ณต๋ฐฑ ์ ๊ฑฐ ํ ์ ์ฅ
})
}
}).promise();
var params_topic = {
topic: event.detect,
payload: JSON.stringify({ 'image': event.image, 'command': detectedText}),
qos: 1 // 0: ํ๋ฒ ์ ์ก, 1: ๋ฐ์ ๋๊น์ง ์ ์ก, 2: ๋ฐ์ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
};
var res = await iotdata.publish(params_topic).promise(); // detect topic ์ผ๋ก publish
return { 'statusCode': 200, 'result': res };
}
์์ ์ค์ ํ๋ ํธ๋ฆฌ๊ฑฐ๋ฅผ ํตํด carRecog/request์ ๋ฉ์์ง๋ฅผ ๋ฐ์์จ๋ค.
rekognition api ํธ์ถํด์ ๋ฉ์์ง๋ก ๋ฐ์์จ image์ text detection ์ํ ํ
detect ํ ํฝ์ผ๋ก ๊ฒฐ๊ณผ๊ฐ์ publish ํ๋ค.
3. manage.js
// aws iot core
var awsIot = require('aws-iot-device-sdk');
// aws IoT Core๋ก๋ถํฐ ๋ฐ์์์ผ ํ๋ ์ ๋ณด
var manage = awsIot.device({
keyPath: './credentials/manage/private.pem.key',
certPath: './credentials/manage/certificate.pem.crt',
caPath: './credentials/manage/AmazonRootCA1.pem',
clientId : 'manage',
host: '์๋ํฌ์ธํธ'
});
// connect
manage.on('connect', () => {
console.log('Manage connected');
// subscribe
manage.subscribe('carRecog/detect/car');
console.log(topic + " sub!!");
});
// message arrive
manage.on('message', (topic, message) => {
console.log(topic + "-> message arrive");
if (topic == 'carRecog/detect/car') {
var msg = JSON.parse(message.toString());
console.log(msg.command);
}
});
carRecog/detect/car ํ ํฝ์ subscribe ํ๊ณ ์๋ค๊ฐ
๋ฉ์์ง๊ฐ ๋ค์ด์ค๋ฉด ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ
ํ ์คํธ
๋ฉ์์ง๊ฐ ์ ์ ๋ฌ๋๊ณ ๋ค์ด์ค๋์ง ๋ณด๊ธฐ ์ํด
setInterval 3์ด๋ก ์ฃผ๊ณ ํ ์คํธํ๋ค
ํ ์คํธํ ์ฌ์ง์ ์ด๊ฑด๋ฐ rekognition์ด ํ๊ธ์ ์ง์ํ์ง ์์์ ์ซ์๋ง ๋ณด๋ด์ก๋ค,,
ํ ํฝ์ ๋ฉ์์ง๊ฐ ์ ๋ค์ด์ค๋์ง ๋ณด๊ณ ์ถ๋ค๋ฉด
AWS IoT > MQTT ํ ์คํธ ํด๋ผ์ด์ธํธ
์์ ์ฃผ์ ๊ตฌ๋ ํด์ ๋ฉ์์ง ๋ค์ด์ค๋์ง ๋ณด๋ฉด ๋๋ค!
'๐ > cloudIOT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
AWS ๊ธฐ๋ฐ ์ฃผ์ฐจ ๊ด๋ฆฌ ์์คํ (2) Lambda ์ค์ (0) | 2022.06.16 |
---|---|
AWS ๊ธฐ๋ฐ ์ฃผ์ฐจ ๊ด๋ฆฌ ์์คํ (1) AWS IoT Core (0) | 2022.06.16 |
AWS EC2 - MQTT ์ด์ฉํ๊ธฐ (0) | 2022.05.17 |
Amazon Rekognition ์ด๋ฏธ์ง์์ ํ ์คํธ ๊ฐ์ง (nodejs) (0) | 2022.05.16 |
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด OS ์ค์น & ๋ ธํธ๋ถ ์๊ฒฉ ์ ์ (0) | 2022.04.04 |