人脸API属性情感产生空对象错误

时间:2018-12-27 01:05:08

标签: android azure null microsoft-cognitive face-api

我正在使用Microsoft Face API来检测面孔的属性,例如年龄,性别和情感。以下代码对我有用:faces[position].faceAttributes.age,我能够估算出年龄。 ({faces[]Face类型的数组)

但是,当我尝试获取面孔高兴的可能性时,我遇到了以下错误:

  

尝试从空对象引用中读取字段“ double com.microsoft.projectoxford.face.contract.Emotion.happiness”。

这就是我得到此人快乐的可能性的方法:
    faces[position].faceAttributes.emotion.happiness

类似地,当我尝试:faces[position].faceAttributes.emotion时,它返回null

我知道faces[position].faceAttributes不是null,因为它适用于年龄和性别等其他属性,但是我无法弄清楚为什么它不能有效地表达情感。有谁知道为什么会这样,我该怎么做才能使它正常工作?


更新:

对于遇到相同问题的人,在AsnycTask处理人脸的地方,必须包含要检测的属性,否则当您引用人脸时,它表示该对象为空对象后来。最初,我有FaceServiceClient.FaceAttributeType.Smile,这就是为什么它在尝试确定情感时给我一个错误的原因。以下代码采用doInBackground方法:

FaceServiceClient.FaceAttributeType[] faceAttr = new FaceServiceClient.FaceAttributeType[]{
    FaceServiceClient.FaceAttributeType.HeadPose,
    FaceServiceClient.FaceAttributeType.Age,
    FaceServiceClient.FaceAttributeType.Gender,
    FaceServiceClient.FaceAttributeType.Emotion,
    FaceServiceClient.FaceAttributeType.FacialHair
};

1 个答案:

答案 0 :(得分:1)

在请求期间,您似乎并没有要求提供emotion属性。所有这些face属性都是可选的,因此返回的对象将只包含您要求的对象。例如,以下请求:

POST https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender HTTP/1.1
Host: westeurope.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••

{
    "url": "https://pbs.twimg.com/profile_images/907936570342338560/qHCX1E2B_400x400.jpg"
}

我将收到以下答复,其中JSON在解析后发送带有null的对象以表达情感

[{
  "faceId": "e97a0554-99a9-44f7-9de9-614ef6d8843b",
  "faceRectangle": {
    "top": 117,
    "left": 106,
    "width": 219,
    "height": 219
  },
  "faceAttributes": {
    "gender": "male",
    "age": 32.0
  }
}]

如果我在请求的emotion中添加returnFaceAttributes

[{
  "faceId": "da7c416f-3eb4-4132-8b3e-e317daf58c7d",
  "faceRectangle": {
    "top": 117,
    "left": 106,
    "width": 219,
    "height": 219
  },
  "faceAttributes": {
    "gender": "male",
    "age": 32.0,
    "emotion": {
      "anger": 0.0,
      "contempt": 0.0,
      "disgust": 0.0,
      "fear": 0.0,
      "happiness": 1.0,
      "neutral": 0.0,
      "sadness": 0.0,
      "surprise": 0.0
    }
  }
}]

我不知道您如何使用API​​(直接调用或使用包),但是您一定要看看这些参数,它们在您的级别可用。如果不是,请直接使用API​​(文档:https://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/