Skip to content

Instantly share code, notes, and snippets.

@jeromewu
Last active April 26, 2020 07:50
Show Gist options
  • Save jeromewu/b021249770385396201823d7962ed758 to your computer and use it in GitHub Desktop.
Save jeromewu/b021249770385396201823d7962ed758 to your computer and use it in GitHub Desktop.
const { createWorker } = require('tesseract.js');
const image = 'https://tesseract.projectnaptha.com/img/eng_bw.png';
const worker = createWorker();
let isReady = false;
// Called as early as possible
(async ()=> {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
isReady = true;
})();
// Do other stuffs
(async (img) => {
if (isReady) {
const { data: { text } } = await worker.recognize(img);
console.log(text);
}
})(image);
@mr-deamon
Copy link

Hey!
Sorry, beginner here! How can I assure, that the second part only runs, as soon the first is finished? I mean this just skips if isReady is still false. But how to wait for it to be ready? Is there a way?

Thank you so much!

@jeromewu
Copy link
Author

A quick way (but not the best) is to use setInterval to wait until the the first part is over, you can reference the code below:

const { createWorker } = require('tesseract.js');                                                  
const image = 'https://tesseract.projectnaptha.com/img/eng_bw.png';                                
const worker = createWorker();
let isReady = false;
// Called as early as possible
(async ()=> {
  await worker.load();
  await worker.loadLanguage('eng');
  await worker.initialize('eng');
  isReady = true;
})();

const doOCR = (img) => { 
  const timer = setInterval(async () => {
    if (isReady) {
      clearInterval(timer);
      console.log("ready");
      const { data: { text } } = await worker.recognize(img);
      console.log(text); 
    } else {
      console.log("not ready");
    }
  }, 500)
};

doOCR(image);

@mr-deamon
Copy link

actually, thats pretty much how i did it now. the only difference is, that i created some kind of queue to process new images. works quite well. right now I'm trying to use multiple workers to speed things up.
have a good weekend and stay safe!

@jeromewu
Copy link
Author

Yeah, queue is a better way to solve the issue, especially when you want to keep the order of the execution.
Multiple workers should be able to speed up and hope you can fulfill your needs. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment