如何从Microsoft认知服务的响应中获得价值

时间:2018-10-26 23:50:56

标签: arrays json

我正在向“ Microsoft认知服务”发出http请求;并且我得到了正确的答案,但是我无法从响应中获得价值。

这是我的剧本:

'use strict';

const request = require('request');

// Replace <Subscription Key> with your valid subscription key. const subscriptionKey = 'xyz'; // here my API key

// You must use the same location in your REST call as you used to get your // subscription keys. For example, if you got your subscription keys from // westus, replace "westcentralus" in the URL below with "westus". const uriBase = 'https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect';

const imageUrl =
    'https://images.unsplash.com/photo-1534559529872-1a83a6cbc03d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=56820e7b0db7b8a7f10804cd157f61ef&w=1000&q=80'
    // Request parameters. const params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
        'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise' };

const options = {
    uri: uriBase,
    qs: params,    //  json: true, // dies nachtäglich eingefügt ev. löschen
    body: '{"url": ' + '"' + imageUrl + '"}',
    headers: {
        'Content-Type': 'application/json',
        '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, '  ');   console.log('JSON Response\n');   console.log(jsonResponse); });

这是我得到的答复:

[    {
      "faceId":"f469c0b3-fbb8-4238-a52a-54fd515a2bca",
      "faceRectangle":{
         "top":327,
         "left":253,
         "width":426,
         "height":426
      },
      "faceAttributes":{
         "smile":0.005,
         "headPose":{
            "pitch":0,
            "roll":0.7,
            "yaw":-4.4
         },
         "gender":"female",
         "age":23,
         "facialHair":{
            "moustache":0,
            "beard":0,
            "sideburns":0
         },
         "glasses":"NoGlasses",
         "emotion":{
            "anger":0,
            "contempt":0,
            "disgust":0,
            "fear":0,
            "happiness":0.005,
            "neutral":0.994,
            "sadness":0.001,
            "surprise":0
         },
         "blur":{
            "blurLevel":"low",
            "value":0
         },
         "exposure":{
            "exposureLevel":"goodExposure",
            "value":0.64
         },
         "noise":{
            "noiseLevel":"low",
            "value":0
         },
         "makeup":{
            "eyeMakeup":true,
            "lipMakeup":true
         },
         "accessories":[

         ],
         "occlusion":{
            "foreheadOccluded":false,
            "eyeOccluded":false,
            "mouthOccluded":false
         },
         "hair":{
            "bald":0.02,
            "invisible":false,
            "hairColor":[
               {
                  "color":"blond",
                  "confidence":0.99
               },
               {
                  "color":"gray",
                  "confidence":0.74
               },
               {
                  "color":"other",
                  "confidence":0.35
               },
               {
                  "color":"brown",
                  "confidence":0.31
               },
               {
                  "color":"red",
                  "confidence":0.15
               },
               {
                  "color":"black",
                  "confidence":0.06
               }
            ]
         }
      }    } ]

现在我想这样做来获取“ faceID”或faceAttributes.gender或faceAttributes.age的值

我已尝试使用以下命令进行操作,但始终会遇到以下错误。

console.log(jsonResponse.faceId);

日志消息:

  

script.js.facedetect_node:未定义

console.log(jsonResponse.faceId[0]);

日志消息:

  

未捕获的异常:无法读取未定义的属性“ 0”

console.log(jsonResponse.faceAttributes[0]);

console.log(jsonResponse.faceAttributes.age[0]);

日志消息

  

TypeError:无法读取未定义的属性“ 0”

我想您已经看到我是一个新手-出于这个原因,如果您对如何从json中获取所提及的值有所了解,我将不胜感激。

cu schroedinger

0 个答案:

没有答案