Tessaract error for discord

34 views
Skip to first unread message

SrPeter

unread,
Jul 16, 2023, 2:08:06 AM7/16/23
to tesseract-ocr
Hi!
Im trying to create a discord bot that read images, but Im getting this error:

Error occurred during OCR: TypeError: worker.load is not a function
    at Client.<anonymous> (/srv/inkietud/index.js:29:26)
    at Client.emit (node:events:513:28)
    at MessageCreateAction.handle (/srv/inkietud/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14)
    at module.exports [as MESSAGE_CREATE] (/srv/inkietud/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/srv/inkietud/node_modules/discord.js/src/client/websocket/WebSocketManager.js:354:31)
    at WebSocketManager.<anonymous> (/srv/inkietud/node_modules/discord.js/src/client/websocket/WebSocketManager.js:238:12)
    at WebSocketManager.emit (/srv/inkietud/node_modules/@vladfrangu/async_event_emitter/dist/index.js:282:31)
    at WebSocketShard.<anonymous> (/srv/inkietud/node_modules/@discordjs/ws/dist/index.js:1103:51)
    at WebSocketShard.emit (/srv/inkietud/node_modules/@vladfrangu/async_event_emitter/dist/index.js:282:31)
    at WebSocketShard.onMessage (/srv/inkietud/node_modules/@discordjs/ws/dist/index.js:938:14)


This is my part of the code
client.on('messageCreate', async (message) => {
    if (message.author.bot) return;

    console.log(`Received a message from ${message.author.username}: ${message.content}`);

    if (message.attachments.size > 0 && message.attachments.first().url) {
        const imageUrl = message.attachments.first().url;

        console.log(`Received an image from ${message.author.username}: ${imageUrl}`);

        // Process the image with Tesseract OCR
        try {
            await worker.load();
            await worker.loadLanguage('es'); // Replace 'eng' with the appropriate language code
            await worker.initialize('es');

            const { data: { text } } = await worker.recognize(imageUrl);
            console.log('Extracted text:', text);

            await worker.terminate();

            // Send the extracted text as a message in the same channel
            message.channel.send(`Extracted text: ${text}`);
        } catch (error) {
            console.error('Error occurred during OCR:', error);
            message.channel.send(`Sorry, something went wrong. Please try again later.`);
        }
    }
});
Reply all
Reply to author
Forward
0 new messages