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.
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.
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.
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.
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.
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
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With