【问题标题】:Microsoft cognitive services face API call微软认知服务面临 API 调用
【发布时间】:2018-08-11 20:45:22
【问题描述】:

我在 Azure (microsoft) 情感 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


    【解决方案1】:

    需要解决的几个问题:

    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-04-18
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2017-12-19
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多