Permalink
Browse files

Use the result image from object detection instead of camera

for LINE notification
  • Loading branch information...
hzliu123 committed May 30, 2017
1 parent 0b19ac3 commit 087eb8e2a566181a23867eabe54a83dce9a2dc65
Showing with 24 additions and 32 deletions.
  1. +1 −1 camera.js
  2. +2 −0 inference/agent.js
  3. +1 −1 journal.js
  4. +20 −30 line.js
@@ -59,7 +59,7 @@ client.on('message', (t, m) => {
log('camera client: cannot get image.');
} else {
log('camera client: publishing image.');
client.publish(topicNotifyLINE, data);
// client.publish(topicNotifyLINE, data);
client.publish(topicActionInference, data);
}
});
@@ -30,6 +30,7 @@ const topicActionLog = config.topicActionLog;
const topicActionInference = config.topicActionInference;
const topicDashboardSnapshot = config.topicDashboardSnapshot;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const topicNotifyLINE = config.topicNotifyLINE;
const inferenceEngine = config.inferenceEngine;

function log(m) {
@@ -107,6 +108,7 @@ client.on('message', (t, m) => {
console.log('Unknown owner ' + inferenceEngine);
}

client.publish(topicNotifyLINE, dashboard_image_path);
client.publish(topicDashboardInferenceResult, result.toString().replace(/(\n)+/g, '<br />'))
})
} else {
@@ -54,7 +54,7 @@ client.on('connect', () => {

client.on('message', (t, m) => {
// secretly save a copy of the image
if (t == topicNotifyEmail) {
if (t === topicNotifyEmail) {
const filename = 'snapshot.jpg';
saveBufferToImage(m, snapshot);
client.publish(topicDashboardSnapshot, filename);
50 line.js
@@ -17,9 +17,6 @@

'use strict';

const fs = require('fs');
const path = require('path');
const moment = require('moment');
const mqtt = require('mqtt');
const line = require('@line/bot-sdk');
const imgur = require('imgur');
@@ -29,6 +26,7 @@ const broker = config.brokerHost;
const client = mqtt.connect(broker);
const topicActionLog = config.topicActionLog;
const topicNotifyLINE = config.topicNotifyLINE;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const targetUserID = config.LINETargetUserID;

// create LINE SDK config
@@ -45,50 +43,42 @@ function log(m) {
console.log(m);
}

function saveBufferToImage(b, filepath) {
fs.writeFile(filepath, b, (e) => {
if (e)
log(`LINE client: cannot save buffer to image.`);
else
log(`LINE client: saved buffer to image ${filepath} successfully.`);
});
}

client.on('connect', () => {
client.subscribe(topicNotifyLINE);
log(`LINE client: connected to ${broker} successfully.`);
client.subscribe(topicDashboardInferenceResult);
log(`client connected to ${broker} successfully.`);
});

client.on('message', (t, m) => {
const size = m.length;
log(`LINE client: on topic ${t}, received ${size} bytes.`)
log(`client on topic ${t}, received ${size} bytes.`)

if (t === topicDashboardInferenceResult) {
const result = m.toString();
LINEClient.pushMessage(targetUserID, { type: 'text', text: result });
return;
}

// save image to file and upload it to imgur for display in LINE message
const now = moment().format('YYYYMMDD-HHmmss');
const snapshot = `snapshot-${now}.jpg`
const snapshot_path = path.join('/tmp', snapshot)
saveBufferToImage(m, snapshot_path);
const snapshot_path = m.toString();
imgur.uploadFile(snapshot_path)
.then((json) => {
var imgur_link = json.data.link;
imgur_link = imgur_link.replace('http:\/\/', 'https:\/\/');
log(`LINE client: An image has been uploaded to imgur. link: ${imgur_link}`);
var imgurLink = json.data.link;
imgurLink = imgurLink.replace('http:\/\/', 'https:\/\/');
log(`An image has been uploaded to imgur. link: ${imgurLink}`);

// Image can only be delivered via 'https://' URL, 'http://' doesn't work
LINEClient.pushMessage(targetUserID, [{ type: 'text',
text: now },
{ type: 'image',
originalContentUrl: imgur_link,
previewImageUrl: imgur_link }
])
LINEClient.pushMessage(targetUserID, { type: 'image',
originalContentUrl: imgurLink,
previewImageUrl: imgurLink })
.then((v) => {
log(`LINE client: message sent to ${targetUserID} successfully.`);
log(`A message sent to ${targetUserID} successfully.`);
})
.catch((err) => {
log(`LINE client: an error occurred, ${err}.`);
log(`An error occurred, ${err}.`);
});
})
.catch((err) => {
log(`LINE client: an error occurred. ${err}`);
log(`An error occurred. ${err}`);
});
});

0 comments on commit 087eb8e

Please sign in to comment.