kykint

Switch to google cloud vision for OCR

Also separate text recognition code into a function
......@@ -17,8 +17,9 @@ 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');
// Import Google Cloud client library and create a client
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient(config.gcloud);
// fs module for saving/removing image file upon/after recognition
const fs = require('fs');
......@@ -136,6 +137,50 @@ function translate(user, message) {
});
}
/**
* Detect and read text from an image file using Google Cloud Vision API.
*
* @param {*} fileId Telegram-provided file id of image to read text from
*/
function detectText(fileId) {
return new Promise(function (resolve, reject) {
// Download the image, which will later be deleted to avoid git detection
bot.downloadFile(fileId, '.').then(function (filePath) {
console.log('Image downloaded: ', filePath);
visionClient.documentTextDetection(filePath)
.then(function (result) {
const fullTextAnnotation = result[0].fullTextAnnotation;
const text = fullTextAnnotation.text;
console.log('Text detection result: ', text);
resolve(text);
// 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) {
console.log(error);
reject(error);
// Delete the image
fs.unlink(filePath, function (error) {
if (error) {
console.log('Error deleting image: ', error);
} else {
console.log('Successfully deleted file ', filePath);
}
});
});
});
});
}
// [Any normal message which is not a command (not starting with '/')]
bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => {
const user = msg.from;
......@@ -199,43 +244,16 @@ bot.on('photo', (msg) => {
const photo = msg.photo[msg.photo.length - 1]; // Choose largest image possible
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);
}
});
// Detect text from given image
detectText(photoId).then(function (text) {
// Translate the result
translate(user, text).then(function (result) {
// Send recognized text to user
bot.sendMessage(chatId, result);
});
}).catch(function (error) {
// Image download failed
console.log('Image download error: ', error);
// Text detection failed
console.log('Text detection error: ', error);
});
});
......
......@@ -10,5 +10,9 @@ module.exports = {
// https://developers.naver.com/apps/#/register
client_id: 'XXXXXXXXXXXXXXXXXXXX',
client_secret: 'XXXXXXXXXX'
},
gcloud: {
projectId: 'your_project_name',
keyFilename: '/path/to/your/keyfile.json'
}
}
......
This diff is collapsed. Click to expand it.
......@@ -10,9 +10,9 @@
"author": "강수인",
"license": "MIT",
"dependencies": {
"@google-cloud/vision": "^1.0.0",
"mongodb": "^3.2.6",
"node-telegram-bot-api": "^0.30.0",
"node-tesseract-ocr": "^0.1.0",
"request": "^2.88.0"
}
}
......