Face API 属性 Emotion 给出空对象错误

Face API Attribute Emotion Gives Null Object Error

我正在使用 Microsoft Face API 检测面部属性,例如年龄、性别和情绪。以下代码对我有用:faces[position].faceAttributes.age 并且我能够获得估计年龄。 (faces[]Face 类型的数组)

然而,当我尝试获取面部是快乐的概率时,我运行陷入以下错误:

Attempt to read from field 'double com.microsoft.projectoxford.face.contract.Emotion.happiness' on a null object reference.

这就是我得到这个人快乐的概率的方法:
faces[position].faceAttributes.emotion.happiness

同样,当我尝试:faces[position].faceAttributes.emotion,它 returns 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
};

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

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
  }
}]

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

[{
  "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 (doc: https://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/)