使用字节的AWS Rekognition JavaScript SDK

sri*_*ttu 5 javascript sdk base64 amazon-web-services amazon-rekognition

在AWS Rekognition的Javascript API规定,对于 rekognition.compareFaces(params,...)法,SourceImage并TargetImage可以采取Bytes或S3Object.我想用Bytes哪个可以

"字节 - (缓冲区,类型数组,Blob,字符串)"

图像字节的Blob最多5 MB.

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

{"__ type":"InvalidImageFormatException","消息":"无效的图像编码"}

有没有人设法使用base64编码图像(而不是)使用compareFaces JS SDK APIS3Object?或使用Bytesparam的任何JavaScript示例都会有所帮助.

Mic*_*nis 4

AWS Rekognition JS SDK 无效图像编码错误线程中的技术有效。

将 base64 图像编码转换为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;
}
Run Code Online (Sandbox Code Playgroud)

作为参数传递到rekognitionBytes:

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
  }
};
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

2885 次

最近记录:

7 年,9 月 前