Kyle Berry
Kyle Berry

Reputation: 141

Tesseract.js hangs in console

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!

Upvotes: 1

Views: 3140

Answers (2)

Kyle Berry
Kyle Berry

Reputation: 141

/*
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)}

Upvotes: 4

gemart
gemart

Reputation: 376

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

Upvotes: 2

Related Questions