how to use Tesseract.recognize in nodejs

Viewed 1911

I want to make an OCR program but I fund some problems during the declaration of 'Tesseract.recognize' method

here is my code :

const express = require('express');

const fs= require('fs');

const multer = require('multer');

const Tesseract = require('Tesseract.js');

const app = express();

// app.use(bodyParser.urlencoded({extended: true}))

const PORT = process.env.PORT | 5000;   

var Storage = multer.diskStorage({
    destination: function (req, file, callback) {
      callback(null, 'images')
    },
    filename: function (req, file, callback) {
      callback(null, file.orignalname);
    }
});
var upload = multer({
 storage: Storage 
}).array('image', 3);
//route
app.post('/', (req, res) => {});

app.post('/upload', (req, res) => {
    console.log(req.file);
    upload(req, res , err => {
        if (err) {
            console.log(err);
            return res.send('somthing went wrong');
        }
        return res.send('file uploaded successfully');
    });
});

var image = fs.readFileSync(__dirname + '/images/cv.jpg', 
    {
        encoding:null
    });

Tesseract.recognize(image)
    .progress(function(p) {
        console.log('progress', p);
    })
    .then(function(result) {
        res.send('result', result);
    });

app.listen(PORT, () => {
    console.log('Server running on PORT ${PORT}')
});

and this is my terminal result:

.progress(function(p) {
     ^

TypeError: Tesseract.recognize(...).progress is not a function
    at Object.<anonymous> (C:\Users\de\Desktop\MR.Azmani Project\ANGULAR\ocr\server.js:46:6)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
[nodemon] app crashed - waiting for file changes before starting...

and this it what is the result how should look like in the treminal during the process enter image description here

and this is the final result in the termilal

enter image description here

thank you

1 Answers

.progress was removed in version 2 of tesseract.js (there's a blog post about that here. Version one is still on Github here, and probably still works, so you can npm i tesseract.js@1.0.19 to get the behavior you're expecting, or see the docs and examples for the current version to get your code updated for v2.

Related