使用字节的AWS Rekognition JavaScript SDK [英] AWS Rekognition JavaScript SDK using Bytes

查看:97
本文介绍了使用字节的AWS Rekognition JavaScript SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS Rekognition Javascript API 指出< a href =http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Rekognition.html#compareFaces-property =nofollow noreferrer> rekognition.compareFaces(params,。 ..) 方法, SourceImage TargetImage 可以字节 S3Object 。我想使用 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,最大为5 MB。

Blob of image bytes up to 5 MBs.

当我传递 Base64 编码的图像字符串时,JS SDK再次重新编码(即双重编码)。因此服务器响应错误说

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:无效图像
编码 }

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

是否有人设法使用 compareFaces JS SDK API 使用base64编码的图像(不是 S3Object )?或使用 Bytes param的任何JavaScript示例都会有所帮助。

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.

推荐答案

该技术从这个 AWS Rekognition JS SDK无效的图像编码错误线程工作。

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

将base64图像编码转换为 ArrayBuffer

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

传入 rekognition as Bytes 参数:

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

这篇关于使用字节的AWS Rekognition JavaScript SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆