InvalidImageSize 图片尺寸太小或太大 - HTML5, Node.js,

InvalidImageSize Image size is too small or too big - HTML5, Node.js,

参考:

调用 Microsoft 认知服务进行情绪识别时出现错误:400 错误请求:{"error":{"code":"InvalidImageSize","message":"Image size is too small or too big."}}

它以调试模式在 Node.js-服务器上本地运行。

客户端向服务器发送 his/her 图片,服务器添加订阅密钥并将请求通过管道发送给 Microsoft。

客户代码:

function processPicture() {
  var process = function(blob){
    var http = new XMLHttpRequest();
    var url = "/home/upload";
    http.open("POST", url, true);
    http.setRequestHeader('Content-Type', 'application/octet-stream');
    http.onreadystatechange = function() {
      if(http.readyState == 4 && http.status == 200) {
          alert(http.responseText);
      }
    }
    http.send(blob);
  }
  canvas.toBlob(process, "image/png", 0.70);    
}

服务器代码:

router.post('/upload', function(req, res){
    var url = 'https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize';

    req.headers = [];
    req.headers['content-type'] = 'application/octet-stream';
    req.headers['ocp-apim-subscription-key'] = '88ab62d300284ddXXXXXXXXXXXXXX';
    req.pipe(request({
        qs: req.query,
        uri: url,
        headers: req.headers
    })
    ).on('response', function(pres) {
        res.writeHead(pres.statusCode, pres.headers);
        pres.pipe(res);
    });    
});

我看不到我正在使用 "chunked transfer encoding request",这可能会导致问题。 (就像在这个post中提到的:https://social.msdn.microsoft.com/Forums/en-US/6fb47e0d-fc9e-44f0-af3d-66887e10a72c/face-api-error-invalidimagesize-image-size-is-too-small-or-too-big-for-each-request?forum=mlapi

我尝试了不同的图片尺寸

这里是 API-参考:https://westus.dev.cognitive.microsoft.com/docs/services/5639d931ca73072154c1ce89/operations/563b31ea778daf121cc3a5fa

这很可能无济于事,我在使用 python 时也遇到了同样的问题,但是如果您要发送 url link 它会正常工作。这是 Microsoft 服务器端问题,需要解决

问候

使用中间件和创建新请求对我有用:


app.use(bodyParser.raw({type: "application/octet-stream", limit: '50mb'}));

router.post('/upload', function(req, res){
    var url = 'https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize';
    var requestObject = {
        headers: {
            'content-type':'application/octet-stream',
            'ocp-apim-subscription-key':'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        },
        url: url,
        body: req.body
    };
    var handleResponse = function(error, response, body){
        res.send(body);
    };
    request.post(requestObject, handleResponse);
});