Search code examples
javascriptnode.jsocrtesseract.js

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!


Solution

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