mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 15:07:42 +02:00
OCR Command
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
+2
-1
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user