๐Ÿ“/cloudIOT

AWS ๊ธฐ๋ฐ˜ ์ฃผ์ฐจ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ (3) ์ฝ”๋“œ

sssbin 2022. 6. 16. 12:42

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 ํ…Œ์ŠคํŠธ ํด๋ผ์ด์–ธํŠธ 

์—์„œ ์ฃผ์ œ ๊ตฌ๋…ํ•ด์„œ ๋ฉ”์‹œ์ง€ ๋“ค์–ด์˜ค๋Š”์ง€ ๋ณด๋ฉด ๋œ๋‹ค!