0

我已经在 Azure(微软)情感 API 上构建了一个应用程序,但它刚刚与他们的认知服务面部 API 合并。我正在使用网络摄像头将图像(二进制数据)发送到他们的服务器进行分析,并用于获取 xml 作为回报。(在这个例子中,我已经注释掉了一些旧代码。试图修复它)。

function saveSnap(data){
    // Convert Webcam IMG to BASE64BINARY to send to EmotionAPI
    var file = data.substring(23).replace(' ', '+');
    var img = Base64Binary.decodeArrayBuffer(file);


    var ajax = new XMLHttpRequest();

    // On return of data call uploadcomplete function.
    ajax.addEventListener("load", function(event) {
      uploadcomplete(event);
    }, false);

    // AJAX POST request
    ajax.open("POST", "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=emotion","image/jpg");
      ajax.setRequestHeader("Content-Type","application/json");
      //ajax.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml");
      ajax.setRequestHeader("Ocp-Apim-Subscription-Key","subscription_key");
      ajax.send(img);
}

现在我从他们的网站上了解到该调用返回一个 JSON。但我就是无法让它工作。我可以看到有数据返回,但我什至如何从中获取 JSON。我可能错过了一些重要的东西,希望有人能帮助我。:) 当我仍然可以使用 Emotion API 时,该程序正在运行。

function uploadcomplete(event){
  console.log("complete");
  console.log(event);
    //var xmlDoc = event.target.responseXML;
    //var list = xmlDoc.getElementsByTagName("scores");
  console.log(JSON.stringify(event));
4

1 回答 1

0

需要解决的几个问题:

  1. 您需要等待 POST 响应,而不仅仅是上传完成。
  2. application/octet-stream如果您按原样上传二进制文件,您需要将内容类型设置为。
  3. 您需要将订阅密钥设置为实际值(您可能在将代码粘贴到此处之前已经这样做了。)

.

function saveSnap(data) {
  // Convert Webcam IMG to BASE64BINARY to send to EmotionAPI
  var file = data.substring(23).replace(' ', '+');
  var img = Base64Binary.decodeArrayBuffer(file);

  ajax = new XMLHttpRequest();

  ajax.onreadystatechange = function() {
    if (ajax.readyState == XMLHttpRequest.DONE) {
      console.log(JSON.stringify(ajax.response));
    }
  }

  ajax.open('post', 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=emotion');
  ajax.setRequestHeader('Content-Type', 'application/octet-stream');
  ajax.setRequestHeader('Ocp-Apim-Subscription-Key', key);
  ajax.send(img);
}
于 2018-03-04T02:09:18.093 回答