AWS Rekognition JS SDK Invalid image encoding error
Asked Answered
S

3

5

Building a simple AWS Rekognition demo with React, using <input type="file">

Getting Invalid image encoding error.

let file = e.target.files[0];
let reader = new FileReader();

reader.readAsDataURL(file);

reader.onloadend = () => {
  let rekognition = new aws.Rekognition();

  var params = {
    Image: { /* required */
      Bytes: reader.result,
    },
    MaxLabels: 0,
    MinConfidence: 0.0
  };

  rekognition.detectLabels(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });

enter image description here

GitHub repo: https://github.com/html5cat/vision-test/

GitHub Issue: https://github.com/html5cat/vision-test/issues/1

Stasiastasis answered 25/4, 2017 at 0:15 Comment(0)
M
6

You can try converting the reader.result into binary bytes.

function getBinary(encodedFile) {
        var base64Image = encodedFile.split("data:image/jpeg;base64,")[1];
        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);
        }

        var blob = new Blob([ab], {
          type: "image/jpeg"
        });

        return ab;
      }

You can essentially set the response of the above method for Bytes:

 Bytes: getBinary(reader.result),
Muggy answered 30/4, 2017 at 3:25 Comment(4)
Thank you so much!Donne
You might want to consider png as well - see docs.aws.amazon.com/rekognition/latest/dg/…Messidor
replacing both instances of jpeg with png did the trick for me! Thanks!!!!Platinum
Can anyone explain me the code or provide any references?Marjie
M
4

In case someone is doing this on the Node side, I was running into a similar issue when reading in a file in as a byte array buffer and sending it to Rekognition.

I solved it by instead reading in the base64 representation, then turning it into a buffer like this:

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

const 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 base64 buffer out of the string passed to us by fs.readFile()
  const buffer = Buffer.from(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);

    });
    
});

This might also be relevant to people getting the: Expected params.Image.Bytes to be a string, Buffer, Stream, Blob, or typed array object message.

Mendie answered 2/8, 2017 at 0:7 Comment(0)
C
0

The return value from ReadAsDataUrl includes a prefix indicating the data's MIME-TYPE and encoding. ("image/png;base64, IVBORsdafasdfasf...").

However, the Rekognition API expects only the encoded bytes of the image, without any prefixes.

Try reader.result.split(',')[1] filtering out the prefix and passing only the encoded bytes in your request.

Cleland answered 25/4, 2017 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.