AWS Rekognition JS SDK Invalid image encoding error

穿精又带淫゛_ 提交于 2019-12-24 00:54:06

问题


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

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

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


回答1:


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),



回答2:


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

    });

});

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.




回答3:


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.



来源:https://stackoverflow.com/questions/43599556/aws-rekognition-js-sdk-invalid-image-encoding-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!