Microsoft认知服务面临API调用

问题描述 投票:0回答:1

我在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));
json ajax microsoft-cognitive azure-cognitive-services api-cognitive-services
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);
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.