1

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

1 Answer 1

1

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)
    })
})
Sign up to request clarification or add additional context in comments.

Comments

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.