From e23f3ce6d74f84a9a8f0d9d8d6cef3c2f8cffe7e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 9 Nov 2020 14:59:02 -0500 Subject: [PATCH] OCR Command --- README.md | 3 ++- commands/analyze/ocr.js | 45 +++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 commands/analyze/ocr.js diff --git a/README.md b/README.md index f2773c0e..43715ea1 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 555 +Total: 556 ### Utility: @@ -535,6 +535,7 @@ Total: 555 * **gender:** Determines the gender of a name. * **has-transparency:** Determines if an image has transparency in it. * **image-size:** Determines the size of an image. +* **ocr:** Performs Optical Character Recognition on an image. * **read-qr-code:** Reads a QR Code. * **safe-url:** Determines if a URL is safe or not. * **scrabble-score:** Responds with the scrabble score of a word. diff --git a/commands/analyze/ocr.js b/commands/analyze/ocr.js new file mode 100644 index 00000000..dc70487d --- /dev/null +++ b/commands/analyze/ocr.js @@ -0,0 +1,45 @@ +const Command = require('../../structures/Command'); +const { createWorker } = require('tesseract.js'); +const { reactIfAble } = require('../../util/Util'); +const { LOADING_EMOJI_ID, SUCCESS_EMOJI_ID } = process.env; + +module.exports = class OcrCommand extends Command { + constructor(client) { + super(client, { + name: 'ocr', + aliases: ['tesseract', 'optical-character-recognition'], + group: 'analyze', + memberName: 'ocr', + description: 'Performs Optical Character Recognition on an image.', + throttling: { + usages: 1, + duration: 60 + }, + args: [ + { + key: 'image', + prompt: 'What image would you like to perform OCR on?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 2048 }) + } + ] + }); + } + + async run(msg, { image }) { + await reactIfAble(msg, this.client.user, LOADING_EMOJI_ID, '💬'); + const worker = createWorker(); + await worker.load(); + await worker.loadLanguage('eng'); + await worker.initialize('eng'); + const { data: { text } } = await worker.recognize(image); + await worker.terminate(); + await reactIfAble(msg, this.client.user, SUCCESS_EMOJI_ID, '✅'); + if (text.length > 2000) { + return msg.reply('The result was over 2000 characters, so here\'s a TXT file.', { + files: [{ attachment: Buffer.from(text), name: 'ocr.txt' }] + }); + } + return msg.reply(text); + } +}; diff --git a/package.json b/package.json index e10fa281..59bee8d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.39.6", + "version": "119.40.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -58,6 +58,7 @@ "random-js": "^2.1.0", "rss-parser": "^3.9.0", "stackblur-canvas": "^2.4.0", + "tesseract.js": "^2.1.4", "winston": "^3.3.3" }, "optionalDependencies": {