1

Problem: I have a very simple script using tesseract.js locally (which getting to work was a pain of its own), and when i run the function, it logs my text and hangs without ever moving onto anything after the tesseract function.

var Tesseract   = require('tesseract.js');
Tesseract.workerOptions.langPath = './eng.traineddata';

function parseImg(img){
  Tesseract.recognize(img)
.then(result => console.log(result.text))
};

parseImg('./undefined.jpeg')

The function does work, and it does log the text from my image, but i cant figure out how to break from the function. Any guidance would be greatly appreciated!

2 Answers 2

4
/*
For anyone else who may have this problem...
I found the answer to my own question in the github 
examples 
for Tesseract.js
*/


var Tesseract   = require('tesseract.js');
Tesseract.workerOptions.langPath = './eng.traineddata';

function parseImg(img){
  Tesseract.recognize(img)
    .then(result =>{
        console.log(result.text)
        //This guy right here
        process.exit()
    })
};

parseImg('./undefined.jpeg')

/* 
Years later, but with new knowledge comes new solutions. 
Figured i would update this the way i would have done it 
now. 
*/

import Tesseract from 'tesseract'
Tesseract.workerOptions.langPath = './eng.traineddata';

async function parseImg(){
    const result = await Tesseract.recognize(img);
    console.log(result)
    return result
}parseImg().catch(e){console.error(e.stack)}
2
  • 1
    I have a similar proble I installed tesseract.js but after I call recognize nothing happens it's like everything is hanging did you install anything else besides tesseract.js ?
    – Nicu
    Commented Dec 18, 2018 at 7:03
  • 1
    i think that will close everything... in nodejs. Am i right?
    – gumuruh
    Commented Sep 21, 2020 at 7:58
2

In your then (or better yet a finally) block you just need to add Tesseract.terminate();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.