Read / Recognize ISBN barcode in Node.js using Aspose.Barcode REST API error

Hi Team ,

I am using node SDK for barcode recognize. This is failing for few of the images
https://testops.lifedata.ai/api/storage/workspace/21b9f06a-cf29-4246-a626-04618e4ddfdb/3eaa79ee-27cb-4b4b-a86a-742e15b11d76/1626968513420_IMG-20210722-WA0007.jpg
Also for few URLS we are getting 405 error
Eg : https://media.messagebird.com/v1/media/1c8b4b5b-8c36-482c-a104-0326b83d7a6b

Also recognize is taking more than 20sec to respond.
Pls help me in getting these sorted and getting higher rate of recognition

@Shwetha07

We are sorry for the inconvenience. I tested the scenario using your shared image. I noticed the processing time issue but the barcode is recognized without any issue. The API method recognizes two barcodes in the image. I have logged a ticket BARCODECLOUD-193 to investigate the processing time issue.

const imageBuffer = fs.readFileSync('1626968513420_IMG-20210722-WA0007.jpg');

const request = new Barcode.PostBarcodeRecognizeFromUrlOrContentRequest();
request.preset = Barcode.PresetType.HighPerformance;
request.image = imageBuffer;
const recognized = await api.postBarcodeRecognizeFromUrlOrContent(request);

const barcodes = recognized.body.barcodes;
console.log(barcodes.length);

const barcode = barcodes[1];
console.log(barcode.type);
console.log(barcode.barcodeValue);

In reference to the URL issue, please share your sample code along with sample URLs. We will investigate the issue and will guide you.

@tilal.ahmad Thanks for the update
Its taking more than 50sec for recognition. Is there specific image format to be used? what is the average time for each req?

Below is the node js code I am using for barcode recognition

async function recognizeBarcode(url) {
const request = new Barcode.PostBarcodeRecognizeFromUrlOrContentRequest();
request.timeout = 50000;
request.checksumValidation = "Off";
request.preset = "HighPerformance"
request.url = url;
var ISBNValue = null;

await api.postBarcodeRecognizeFromUrlOrContent(request)
    .then(result => {
        console.log("response is ", result.body.barcodes)
    })
    .catch(err => {
        console.log(err);
    })

}

We are facing to the below URLS
https://media.messagebird.com/v1/media/2bf9baa3-6873-45e5-8dbc-0cd1586c6090
https://media.messagebird.com/v1/media/3a799119-beed-4a7c-a610-fc5a4d568bd8
https://media.messagebird.com/v1/media/69051e73-4f79-4864-9e65-de6ef5ed92bf

Hello Shwetha07,
The best way to speed up recognition is to specify barcode type with request.type = “ISBN”. It allows scanner not to try other barcode type. To prevent fake barcode recognition please do not turn checksumValidation off.

It’s better to download image first to avoid 405 errors and then send it to recognition with request.image parameter.

I’ve slightly changed your code to show how to download image:

async function recognizeBarcode(data) {
    const request = new Barcode.PostBarcodeRecognizeFromUrlOrContentRequest();
    request.timeout = 50000;
    request.preset = "HighPerformance";
    request.type = "ISBN";
    request.image = data;
    var ISBNValue = null;

    await api.postBarcodeRecognizeFromUrlOrContent(request)
        .then(result => {
            ISBNValue = result.body.barcodes[0].barcodeValue;
            console.log("response is ", ISBNValue)
        })
        .catch(err => {
            console.log(err);
        });
}

and added function

const https = require('https');

async function downloadUrl(url) {
    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            const data = [];
            res.on('data', (chunk) => {
                data.push(chunk);
            }).on('end', () => {
                let buffer = Buffer.concat(data);
                resolve(buffer);
            });
        }).on('error', (err) => {
            reject(err);
        });
    });
}

they should work together

async function downloadAndRecognize(url) {
    const buffer = await downloadUrl(url);
    const result = await recognizeBarcode(buffer);
}

Error 405 occurs because server that serves image could not handle HTTP HEAD request which we are using to check URL before processing. This error could be easily simulated with curl command

curl -I https://media.messagebird.com/v1/media/1c8b4b5b-8c36-482c-a104-0326b83d7a6b
HTTP/2 405 
date: Fri, 23 Jul 2021 16:16:58 GMT
content-type: text/plain; charset=utf-8
content-length: 19
allow: GET, OPTIONS
x-content-type-options: nosniff
strict-transport-security: max-age=15724800

Average time for each recognition mostly depends on image size barcode type hints and server performance. There is no specific image format (PNG is the best option to save barcodes) but other format should not affect performance too much.
I’ve checked recognition speed with and without barcode type hint and get at least twice faster performance with explicit barcode type “ISBN”. As far as I can see, your images has pretty big resolution and could be resized at least half of their original size. You can try reduce image size at half or quarter size (try to use integer scaling factor like 2, 3, 4, 5 etc).