Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Rekognition JS SDK Invalid image encoding error

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

like image 292
Yuriy Dybskiy Avatar asked Apr 25 '17 00:04

Yuriy Dybskiy


2 Answers

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),
like image 52
srikanth Nutigattu Avatar answered Nov 17 '22 04:11

srikanth Nutigattu


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');

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 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.

like image 27
Sean Avatar answered Nov 17 '22 06:11

Sean