"InvalidImageSize","message":“图像尺寸太小". [英] "InvalidImageSize", "message": "Image size is too small."

查看:250
本文介绍了"InvalidImageSize","message":“图像尺寸太小".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Node.js中使用Microsoft的Face API,但我无法加载本地图像.我究竟做错了什么?谢谢

Trying to use Microsoft's Face API in Node.js but I am not able to load local images. What am I doing wrong? Thanks

我正在与网络摄像头连接,并将视频绘制到画布标签上.

I'm interfacing with a webcam and drawing the video out onto a canvas tag.

var canvas = document.getElementById("myCanvas"); // get the canvas from the page
var ctx = canvas.getContext("2d");

我已检查我是否正在使用

I have checked that I am getting an image using

var filename = new Date();
var imgData = canvas.toDataURL('image/jpeg');
var link = document.getElementById('saveImg');
link.href = imgData;
link.download = filename;
link.click();

并且图像保存得很好...但是我然后尝试执行以下操作:

and the image is saved fine...but I then try to do the following:

sendRequest(makeblob(imgData));

function sendRequest(imageURL) {
  var returnData;
  const request = require('request');
  const subscriptionKey = '...';

  const uriBase = 'https://eastus.api.cognitive.microsoft.com/face/v1.0/detect';

  // Request parameters.
  const params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': ''
  };

  const options = {
    uri: uriBase,
    qs: params,
    body: '"' + imageURL + '"',
    headers: {
      'Content-Type': 'application/octet-stream',
      'Ocp-Apim-Subscription-Key': subscriptionKey
    }
  };

  request.post(options, (error, response, body) => {
    if (error) {
      console.log('Error: ', error);
      return;
    }
    let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
    returnData = jsonResponse;
  });
  return returnData;
}

makeblob = function (dataURL) {
        var BASE64_MARKER = ';base64,';
        if (dataURL.indexOf(BASE64_MARKER) == -1) {
            var parts = dataURL.split(',');
            var contentType = parts[0].split(':')[1];
            var raw = decodeURIComponent(parts[1]);
            return new Blob([raw], { type: contentType });
        }
        var parts = dataURL.split(BASE64_MARKER);
        var contentType = parts[0].split(':')[1];
        var raw = window.atob(parts[1]);
        var rawLength = raw.length;

        var uInt8Array = new Uint8Array(rawLength);

        for (var i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i);
        }

        return new Blob([uInt8Array], { type: contentType });
    }

这只是返回

{
  "error": {
    "code": "InvalidImageSize",
    "message": "Image size is too small."
  }
}

我还应该如何对图像进行解码/编码?

How else am I supposed to de/encode the image?

推荐答案

我知道我来晚了,但是我找到了可以肯定对您有所帮助的东西. 遗憾的是,如此处所述,Emotion和Face API不支持分块传输. 解决方法"是在发出Web请求之前同步加载图像位.该代码段应如下所示:

I know I am late here, but I have find something which surely helps you. Sadly, the Emotion and Face APIs do not support chunked transfers, as noted here. The 'workaround' is to load the image bits synchronously prior to making the web request. The code snippet for that should be like this:

const request = require('request');
const fs = require('fs');

function sendRequest(imageData) {
    const uriBase = 'https://eastus.api.cognitive.microsoft.com/face/v1.0/detect';

    // Request parameters.
    const params = {
        'returnFaceId': 'true',
        'returnFaceLandmarks': 'false',
        'returnFaceAttributes': ''
    };

    const options = {
        uri: uriBase,
        qs: params,
        body: fs.readFileSync(imgData),
        headers: {
          'Content-Type': 'application/octet-stream',
          'Ocp-Apim-Subscription-Key': subscriptionKey
        }
    };

    request.post(options, (error, response, body) => {
      if (error) {
        console.log('Error: ', error);
        return;
      }
      let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
      returnData = jsonResponse;
    });
    return returnData;
}

这篇关于"InvalidImageSize","message":“图像尺寸太小".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆