Mazzon
Mazzon

Reputation: 31

how to use ocr in node.js with buffer image?

I need to create an API with the propose is to get the value from an image, without need to save the image on the server, only like a buffer. but I try and nothing

when I test with the URL 'https://tesseract.projectnaptha.com/img/eng_bw.png' works fine, but when I try using with buffer or local file didn't work

service:

import * as Tesseract from 'tesseract.js';

export const readImage = async (file: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    console.log(file);
    Tesseract.recognize(
      file,
      'eng',
      { logger: m => console.log(m) },
    ).then((text) => {
      console.log(text);
      resolve(null);
    }).catch(err => reject(err));
  });
};

controller:

import {Request, Response} from "express";
import * as OcrService from './Ocr.service';

export const readImage = async (req: Request, res: Response) => {
    try {
        const encoded = req.file.buffer;
        const document = await OcrService.readImage(encoded);
        return res.status(200).send(document);
    } catch (e) {
        return res.status(400).send(e);
    }
};

config/multer:

import * as multer from 'multer';
import * as path from 'path';

const storage = multer.memoryStorage();
export const upload = multer({
  storage: storage,
  fileFilter: function(req, file, callback) {
    var ext = path.extname(file.originalname);
    if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') 
    {
      return callback(new Error('Only images are allowed'));
    }
    callback(null, true);
  },
  limits: {
    fileSize: 1024 * 1024,
  },
});

null POST / 200 1469.552 ms - 0

"my text example" POST / 200 1469.552 ms - 0

Upvotes: 1

Views: 2912

Answers (1)

sunknudsen
sunknudsen

Reputation: 7320

Here is working code I use behind an Express API that ingests images as Base64 strings.

This is a very basic example that responds with the text found in the image.

const tesseract = require("tesseract.js")

app.post("/api/v1/tesseract", function(req, res) {
  const buffer = Buffer.from(req.body.imgBase64, "base64")
  tesseract
    .recognize(buffer, "eng")
    .then(result => {
      res.status(200).send(result.data.text)
    })
    .catch(error => {
      res.status(500).send(error.message)
    })
})

Upvotes: 1

Related Questions