0

当我InvalidImageFormatException试图将图像传递给AWS Rekognition. 我向Image URL发出 HTTP 请求,下载图像并将缓冲区传递给 aws 函数。

这是它的代码:

const AWS = require('aws-sdk');
const axios = require('axios');
let rekognition = new AWS.Rekognition({apiVersion: '2016-06-27', region: 'us-east-1'});

let url = 'http://www.xsjjys.com/data/out/43/WHDQ-511895361.jpg'

axios({
    method: 'GET',
    url,
    responseType: 'arraybuffer'
})
    .then(response => {
            console.log(response)
            let params = {
                    Image: {
                            Bytes: response.data
                    },
                    Attributes: ['ALL']
            };

            rekognition.detectFaces(params, (err, data) => {
                    if(err) console.log(err);
                    else console.log(JSON.stringify(data));
            });
    })
    .catch(err => console.log(err));

我不明白这个错误可能是什么原因。它适用于其他图像,但对某些图像会引发错误。这可能是什么原因?

这是跟踪:

{ InvalidImageFormatException: Invalid image encoding
        at Request.extractError (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/protocol/json.js:48:27)
        at Request.callListeners (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
        at Request.emit (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
        at Request.emit (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/request.js:683:14)
        at Request.transition (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/request.js:22:10)
        at AcceptorStateMachine.runTo (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/state_machine.js:14:12)
        at /home/suhail/nodejs/test/node_modules/aws-sdk/lib/state_machine.js:26:10
        at Request.<anonymous> (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/request.js:38:9)
        at Request.<anonymous> (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/request.js:685:12)
        at Request.callListeners (/home/suhail/nodejs/test/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
    message: 'Invalid image encoding',
    code: 'InvalidImageFormatException',
    time: 2018-04-03T13:28:43.404Z,
    requestId: 'edd8fccf-3742-11e8-95a3-c58fca411044',
    statusCode: 400,
    retryable: false,
    retryDelay: 55.96279161227613 }
4

1 回答 1

0

您的图片链接“ http://www.xsjjys.com/data/out/43/WHDQ-511895361.jpg ”重定向到 HTML 页面:

curl -v "http://www.xsjjys.com/data/out/43/WHDQ-511895361.jpg"    (510ms)
*   Trying 144.76.74.241...
* TCP_NODELAY set
* Connected to www.xsjjys.com (144.76.74.241) port 80 (#0)
> GET /data/out/43/WHDQ-511895361.jpg HTTP/1.1
> Host: www.xsjjys.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Tue, 03 Apr 2018 13:59:42 GMT
< Content-Type: text/html
< Content-Length: 178
< Location: http://www.xsjjys.com/511895361.html
< Connection: keep-alive
< Expires: Tue, 03 Apr 2018 13:59:42 GMT
< Cache-Control: max-age=0
< Cache-Control: no-store, no-cache, must-revalidate
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host www.xsjjys.com left intact

这意味着您传递的是 HTML 数据而不是图像数据,这就是 Rekognition 不能使用它的原因。看起来像是该站点上的某种反盗链设置。您可能需要传递一些 cookie 或正确的 HTTP 引用标头。但就目前而言,我会简单地尝试在其他地方托管的图片。

于 2018-04-03T14:02:15.023 回答