kykint

Implement OCR using tesseract

......@@ -17,6 +17,12 @@ const languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'
const db = require('./db');
db.init();
// Tesseract module for image recognition
const tesseract = require('node-tesseract-ocr');
// fs module for saving/removing image file upon/after recognition
const fs = require('fs');
// /echo [whatever]
bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
......@@ -184,6 +190,55 @@ bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
}
});
// When an image file is received, temporarily save it in current directory
// and use tesseract to recognize its text. The texts are then translated
// and sent to the user.
bot.on('photo', (msg) => {
const user = msg.from;
const chatId = msg.chat.id;
const photo = msg.photo[2];
const photoId = photo.file_id;
// Download the image, which will later be deleted to avoid git detection
bot.downloadFile(photoId, '.').then(function (filePath) {
console.log('Image downloaded: ', filePath);
// Begin tesseract OCR
tesseract.recognize(filePath).then(function (text) {
console.log('OCR result: ', text);
// Translate the result
translate(user, text).then(function (result) {
// Send recognized text to user
bot.sendMessage(chatId, result);
// Delete the image
fs.unlink(filePath, function (error) {
if (error) {
console.log('Error deleting image: ', error);
} else {
console.log('Successfully deleted file ', filePath);
}
});
});
}).catch(function (error) {
// OCR failed
console.log('OCR error: ', error);
// Delete the image
fs.unlink(filePath, function (error) {
if (error) {
console.log('Error deleting image: ', error);
} else {
console.log('Successfully deleted file ', filePath);
}
});
});
}).catch(function (error) {
// Image download failed
console.log('Image download error: ', error);
});
});
// /l(anguage)
// Let user select the language he wants his message to translate to.
// When triggered, bot will send an inline keyboard message with a list
......
......@@ -417,6 +417,11 @@
}
}
},
"node-tesseract-ocr": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/node-tesseract-ocr/-/node-tesseract-ocr-0.1.0.tgz",
"integrity": "sha512-gUp+fy8Wiy2ZZQiSe33ApfyaBPOOlTz8nrHjZqWGAX4l06lW01uO6ABA5KWBhO3B1DmWl1Y9T1aLasfXYpm2Dg=="
},
"oauth-sign": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
......
......@@ -12,6 +12,7 @@
"dependencies": {
"mongodb": "^3.2.6",
"node-telegram-bot-api": "^0.30.0",
"node-tesseract-ocr": "^0.1.0",
"request": "^2.88.0"
}
}
......