Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Rekognition JavaScript SDK using Bytes

The AWS Rekognition Javascript API states that for rekognition.compareFaces(params,...) method, the SourceImage and TargetImage can take Bytes or S3Object. I want to use the Bytes which can be

"Bytes — (Buffer, Typed Array, Blob, String)"

Blob of image bytes up to 5 MBs.

When I pass the Base64 encoded string of the images, the JS SDK is re-encoding again (i.e double encoded). Hence server responding with error saying

{"__type":"InvalidImageFormatException","Message":"Invalid image encoding"}

Did anyone manage to use the compareFaces JS SDK API using base64 encoded images (not S3Object)? or any JavaScript examples using Bytes param would help.

like image 594
srikanth Nutigattu Avatar asked Apr 19 '17 11:04

srikanth Nutigattu


People also ask

Does AWS Rekognition store data?

Amazon Rekognition provides two types of API operations. They are non-storage operations where no information is stored by Amazon Rekognition, and storage operations where certain facial information is stored by Amazon Rekognition.

What algorithm does Rekognition use?

Rekognition Image uses deep neural network models to detect and label thousands of objects and scenes in your images, and we are continually adding new labels and facial recognition features to the service.

Can we use AWS Rekognition for media processing?

With a few clicks, you can integrate artificial intelligence into your media workflows with Amazon Rekognition, a deep learning-based image and video analysis service. Amazon Rekognition can identify objects, people, text, scenes, and activities, as well as detect inappropriate content from media files you provide.

Can I use JavaScript in AWS?

The AWS SDK for JavaScript supports three runtimes: JavaScript for browser, Node. js for server, React Native for mobile development. It also supports cross-runtime: a service client package can be run on browsers, Node. js, and React-Native without code change.


1 Answers

The technique from this AWS Rekognition JS SDK Invalid image encoding error thread worked.

Convert the base64 image encoding to a 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;
}

Pass into rekognition as 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
  }
};
like image 146
Michael Dennis Avatar answered Oct 18 '22 08:10

Michael Dennis