Permalink
Browse files

Convert data collector's input text data format to JSON.

Signed-off-by: Bofu Chen (bafu) <bofu@dt42.io>
  • Loading branch information...
Bofu Chen (bafu)
Bofu Chen (bafu) committed Oct 9, 2017
1 parent 454d483 commit 0586f73f575d47f4bc17add30ad2c514a6300574
Showing with 41 additions and 11 deletions.
  1. +7 −6 config.js
  2. +4 −4 data_collector.js
  3. +30 −1 inference/agent.js
@@ -45,6 +45,7 @@ config.topicNotifyLINE = padTopicBase('notify/line');
config.topicDashboardLog = padTopicBase('dashboard/log');
config.topicDashboardSnapshot = padTopicBase('dashboard/snapshot');
config.topicDashboardInferenceResult = padTopicBase('dashboard/inferenceResult');
config.topicJSONInferenceResult = padTopicBase('data/jsonInferenceResult');
// IP camera
config.ipcameraSnapshot = '';
@@ -57,19 +58,19 @@ config.boardcameraImageHeight = 480;
config.storageDirPath = '';
// email notification
config.senderEmail = 'SENDER_EMAIL';
config.senderPassword = 'SENDER_PASSWORD';
config.receiverEmail = 'RECEIVER_EMAIL';
config.senderEmail = '';
config.senderPassword = '';
config.receiverEmail = '';
// for compatibility
config.sender_email = config.senderEmail;
config.sender_password = config.senderPassword;
config.receiver_email = config.receiverEmail;
// Authentication and channel information for LINE
config.LINETargetUserID = 'LINE_TARGET_USER_ID';
config.LINEChannelSecret = 'LINE_CHANNEL_SECRET';
config.LINEChannelAccessToken = 'LINE_CHANNEL_ACCESS_TOKEN';
config.LINETargetUserID = '';
config.LINEChannelSecret = '';
config.LINEChannelAccessToken = '';
// make config importable
module.exports = config;
@@ -27,7 +27,7 @@ const client = mqtt.connect(broker);
const topicActionLog = config.topicActionLog;
const topicActionInference = config.topicActionInference;
const topicDashboardSnapshot = config.topicDashboardSnapshot;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const topicJSONInferenceResult = config.topicJSONInferenceResult;
const storageDirPath = config.storageDirPath;
@@ -95,8 +95,8 @@ function callbackSaveData(topic, message) {
*/
fs.createReadStream(config.snapshot)
.pipe(fs.createWriteStream(detectionImage));
} else if (topic == topicDashboardInferenceResult) {
console.log('Get ' + topicDashboardInferenceResult);
} else if (topic == topicJSONInferenceResult) {
console.log('Get ' + topicJSONInferenceResult);
const detectionJSON = path.join(
storageDirPath,
@@ -119,7 +119,7 @@ client.on('connect', () => {
client.subscribe(topicActionLog);
client.subscribe(topicActionInference);
client.subscribe(topicDashboardSnapshot);
client.subscribe(topicDashboardInferenceResult);
client.subscribe(topicJSONInferenceResult);
log(`log client: connected to ${broker} successfully.`);
});
@@ -30,6 +30,7 @@ const topicActionLog = config.topicActionLog;
const topicActionInference = config.topicActionInference;
const topicDashboardSnapshot = config.topicDashboardSnapshot;
const topicDashboardInferenceResult = config.topicDashboardInferenceResult;
const topicJSONInferenceResult = config.topicJSONInferenceResult;
const topicNotifyLINE = config.topicNotifyLINE;
const inferenceEngine = config.inferenceEngine;
@@ -47,6 +48,29 @@ function saveBufferToImage(b, filepath) {
});
}
const parseDarknet = function(str) {
let [label, confidence, x, y, width, height] = str.split(' ');
let result = {
label: label,
confidence: parseFloat(confidence),
top: parseInt(y),
bottom: parseInt(y) + parseInt(height),
left: parseInt(x),
right: parseInt(x) + parseInt(width)
};
return result
}
const darknetToJSON = function(data) {
let dataStrList = data.toString().replace(/\n$/, '').split('\n');
let jsonResult = [];
for (let i in dataStrList) {
let item = `${dataStrList[i]}`;
jsonResult.push(parseDarknet(item));
}
return jsonResult;
};
client.on('connect', () => {
client.subscribe(topicActionInference);
log(`inference client: connected to ${broker} successfully.`);
@@ -86,9 +110,15 @@ client.on('message', (t, m) => {
console.log('Written snapshot to dashboard image directory: ' + dashboard_image_path);
client.publish(topicDashboardSnapshot, 'snapshot.jpg');
})
client.publish(topicDashboardInferenceResult,
result.toString().replace(/(\n)+/g, '<br />'));
} else if (inferenceEngine === 'detector') {
console.log('Snapshot is created by detector, only notify dashboard to update.');
client.publish(topicDashboardSnapshot, 'snapshot.jpg');
client.publish(topicDashboardInferenceResult,
result.toString().replace(/(\n)+/g, '<br />'));
client.publish(topicJSONInferenceResult,
JSON.stringify(darknetToJSON(result)));
// Delete intermediate files.
//
@@ -104,7 +134,6 @@ client.on('message', (t, m) => {
}
client.publish(topicNotifyLINE, dashboard_image_path);
client.publish(topicDashboardInferenceResult, result.toString().replace(/(\n)+/g, '<br />'))
})
} else {
console.log('rename event for ' +

0 comments on commit 0586f73

Please sign in to comment.