OCR Timeout

This commit is contained in:
Dragon Fire
2021-03-15 17:06:53 -04:00
parent 330253542d
commit 7812715e86
4 changed files with 20 additions and 10 deletions
+10 -1
View File
@@ -1,7 +1,7 @@
const Command = require('../../structures/Command');
const { createWorker } = require('tesseract.js');
const { reactIfAble } = require('../../util/Util');
const { LOADING_EMOJI_ID, SUCCESS_EMOJI_ID } = process.env;
const { LOADING_EMOJI_ID, SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env;
module.exports = class OcrCommand extends Command {
constructor(client) {
@@ -32,7 +32,16 @@ module.exports = class OcrCommand extends Command {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
let timedOut = false;
setTimeout(async () => {
timedOut = false;
await worker.terminate();
}, 30000);
const { data: { text } } = await worker.recognize(image);
if (timedOut) {
await reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌');
return msg.reply('Scanning took longer than 30 seconds, so I\'ve given up.');
}
await worker.terminate();
await reactIfAble(msg, this.client.user, SUCCESS_EMOJI_ID, '✅');
if (!text) return msg.reply('There is no text in this image.');