InvalidImageSize图像大小太小或太大 - HTML5,Node.js,

时间:2017-03-31 18:38:07

标签: node.js html5 express html5-canvas microsoft-cognitive

参考:Microsoft Cognitive Services Emotion API. Error: 'Image size is too small or too big.'

调用Microsoft认知服务进行情感识别我收到错误:400 Bad Request:{" error":{" code":" InvalidImageSize",& #34;消息":"图片尺寸太小或太大。"}}

它在调试模式下在Node.js-Server上本地运行。

客户端将他/她的图像发送到服务器,该服务器添加订阅密钥并将请求传递给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"这可能会导致问题。 (如本文所述: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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:-1)

这很可能无济于事,我也有使用python的同样问题,但是如果你要发送一个url链接它会工作正常。这是Microsoft Server方面的问题,需要解决

问候