微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Amazon Rekogntion图像:错误InvalidImageFormatException:请求的图像格式无效

如何解决Amazon Rekogntion图像:错误InvalidImageFormatException:请求的图像格式无效

我正在尝试比较从Node.Js应用程序调用AWS Rekognition的面孔。比较S3存储桶上的两个图像时,一切都很好,但是当我尝试从客户端(React Native / Expo应用程序)上载本地图像以与存储在此存储桶中的另一个图像进行比较时,出现错误{{1} }。

此图像是jpeg 250px正方形,已作为有效的base64字符串发送(已通过atob测试)。显然,它符合此处提出的要求:https://docs.aws.amazon.com/rekognition/latest/dg/limits.html

下面是一些代码段:

捕获图像:

InvalidImageFormatException: Request has invalid image format

编辑图像:

const takeImgHandler = async () => {
    const img = await ImagePicker.launchCameraAsync(getImgProps);
    editImg(img);
};

设置对我的服务器的detectFaces调用

const editImg = async img => {
   ...
    const actions = [
      { resize: { 250,250 } },];

    const saveOptions = {      
      base64: true,};

    const edited = await ImageManipulator.manipulateAsync(img.uri,actions,saveOptions);
    setState({ ...state,img: edited });    
};

服务器控制程序运行此功能

// sourceImg is appState.img.base64
const compareImagesHandler = async sourceImg => {
    const targetimage = {
      S3Object: {
        Bucket: 'my-bucket-name',Name: 'image-name.jpg',},}; 

    const sourceImage = {
      Bytes: sourceImg,};

const comparison = await ajax({ method: 'POST',url: `url-to-server-route`,data: { sourceImage,targetimage }});
    console.log('comparison: >>>>>> ',comparison);
    return comparison;
};

解决方法

解决了!

需要两个调整。首先,使用sourceImgencodeURIComponent文件进行编码:

const sourceImage = encodeURIComponent(sourceImg);

在服务器上,我应该创建一个缓冲区,而不是发送base64字符串:

const imageBuffer = Buffer.from(decodeURIComponent(SourceImage),'base64');

因此,发送给AWS的正文应为:

const params = {    
    SourceImage: {
      Bytes: imageBuffer,}
    TargetImage,SimilarityThreshold: 50,};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。