Tesseract.js in N8N

Hi,

I am trying to use tesseract.js inside the N8N. I have installed and exported the tesseract in the NODE_ALLOWED environment variable and it’s all fine. I have the code in my VS code and executed the code via N8N execute node and i am getting the expected result. What i am trying is how i can make the code to be used by a function Node instead of execute Node. Please find the code below and kindly suggest how i can use this code inside function node…

const { createWorker } = require('tesseract.js');


//const value = process.argv[2]
// if (!value) {
//     console.error('Need the image to process')
//     process.exit(1)
// }

const worker = createWorker();

(async () => {
  await worker.load();
  await worker.loadLanguage('eng');
  await worker.initialize('eng');
  const { data: { text } } = await worker.recognize(<IMAGE PATH>);
  console.log(text);
  await worker.terminate();
})();

please suggest…

Try without ‘{}’

const createWorker = require('tesseract.js');

Hey @Sathya_Narayanan,

Are you getting an error message?

Hi,

I get the following error.

 Details
Stack
TypeError: Cannot read property 'every' of undefined
    at Object.normalizeItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/NodeExecuteFunctions.js:752:23)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:97:34)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:454:47

And here is my code on the function node.

const { createWorker } = require('tesseract.js');


// const value = process.argv[2]
// if (!value) {
//     console.error('Need the image to process')
//     process.exit(1)
// }

const worker = createWorker();

(async () => {
  await worker.load();
  await worker.loadLanguage('eng');
  await worker.initialize('eng');
  const { data: { text } } = await worker.recognize('/tmp/9616595-MemoryUtilization.png');
  return text;
  await worker.terminate();
})();

My sample workflow.

That looks like something is undefined although I am not sure what, I will set aside some time next week to have a proper look. If it was me though I would probably look at making it a custom node as it would give you a bit more control.

Hi Jon,

Thanks, i have no idea as to how to make this as a custom node and most importantly, the code is working find when i use execute node and even in my vscode. This undefined is only when i try this code on a function node.

Hey @Sathya_Narayanan,

The tricky bit there is while most javascript will run in the function node there are some bits that are a bit more complicated to use.

Maybe another temporary solution would be to use the execute command node to run the javascript directly inside the container

Yes, that’s what I did and it’s working perfectly fine, but what I am trying to achive is to remove the execute node and use a function node to directly call the code…This gives me more flexibility and I don’t have to maintain the code as a file inside the containers…

The function node it’s already wrapped with an async, so no need for the one if the example. Also, you need to return an object, not only the text as you did above. it should be:

const { createWorker } = require('tesseract.js')

const worker = createWorker({

logger: m => console.log(m)

});

await worker.load();

await worker.loadLanguage('eng');

await worker.initialize('eng');

const { data: { text } } = await worker.recognize('https://tesseract.projectnaptha.com/img/eng_bw.png');

await worker.terminate();

return { text }
1 Like

Thank you … Worked :slight_smile:

Thank you … Worked :slight_smile: