AWS Rekognition Javascript SDK Using Bytes

The ReSognition AWS Javascript api states that for the rekognition.compareFaces (params, ...) method, "SourceImage" and "TargetImage" can accept "bytes" or S3. I want to use "bytes" which can be "(Buffer, Typed Array, Blob, String )" compareFaces JS API

When I transfer a lowercase encoding of Base64 images, the JS SDK is again transcoded (for example, with double encoding). Therefore, the server responds with an error saying

{"__ type": "InvalidImageFormatException", "Message": "Invalid image encoding"}

Did anyone help using the JS API SDK using base64 encoded images? (not s3) or any JS examples for using the "Bytes" parameter will help.

+4
source share
4 answers

From this AWS Rekognition JS SDK, an incorrect image encoding error worked.

Convert base64 image encoding to ArrayBuffer:

function getBinary(base64Image) {

   var binaryImg = atob(base64Image);
   var length = binaryImg.length;
   var ab = new ArrayBuffer(length);
   var ua = new Uint8Array(ab);
   for (var i = 0; i < length; i++) {
     ua[i] = binaryImg.charCodeAt(i);
    }

    return ab;
}

Go to rekognition as the Bytes parameter:

    var data = canvas.toDataURL('image/jpeg');
    var base64Image = data.replace(/^data:image\/(png|jpeg|jpg);base64,/, "")
    var imageBytes = getBinary(base64Image);

    var rekognitionRequest = {
      CollectionId: collectionId,
      Image: {
        Bytes: imageBytes
      }
    };
0
source

I ran into a similar problem when reading in a file in Node as a byte of a byte array and sending it to Rekognition.

I solved it instead of reading in the base64 view, and then turned it into a buffer like this:

const aws = require('aws-sdk');
const fs = require('fs');

var rekognition = new aws.Rekognition({
  apiVersion: '2016-06-27'
});

// pull base64 representation of image from file system (or somewhere else)
fs.readFile('./test.jpg', 'base64', (err, data) => {

  // create a new buffer out of the string passed to us by fs.readFile()
  const buffer = new Buffer(data, 'base64');

  // now that we have things in the right type, send it to rekognition
  rekognition.detectLabels({
      Image: {
        Bytes: buffer
      }
    }).promise()
    .then((res) => {

      // print out the labels that rekognition sent back
      console.log(res);

    });

});

, : Expected params.Image.Bytes to be a string, Buffer, Stream, Blob, or typed array object.

0

, , .

Node (, ... ",".

var params = {
                CollectionId: collectionId,
                Image: {
                    Bytes: new Buffer(imageBytes, 'base64')
                    }
};

JS atob Array :

function getBinary(base64Image) {

   var binaryImg = Buffer.from(base64Image, 'base64').toString();
   var length = binaryImg.length;
   var ab = new ArrayBuffer(length);
   var ua = new Uint8Array(ab);
   for (var i = 0; i < length; i++) {
     ua[i] = binaryImg.charCodeAt(i);
    }

    return ab;
}
0

I had the same problem as you, and I will tell you how I solved it.

Amazon Rekognition Supports JPEG and PNG Image Type

This means that if you enter an image file encoded in other formats, such as webp, you always get the same error.

After changing image formats that are not encoded with jpeg or png to jpeg, I could solve this problem.

Hope you solve this problem!

0
source

Source: https://habr.com/ru/post/1675160/


All Articles