Search code examples
androidazurenullazure-cognitive-servicesface-api

Face API Attribute Emotion Gives Null Object Error


I am working with the Microsoft Face API to detect attributes of faces such as age, gender, and emotion. The following code is working for me: faces[position].faceAttributes.age and I am able to get the estimated age. (faces[] is an array of the type Face )

However, when I try to get the probability that the face is happy, I am running into the following error:

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

This is how I am getting the probability that the person is happy:
faces[position].faceAttributes.emotion.happiness

Similarly, when I try: faces[position].faceAttributes.emotion, it returns null.

I know that faces[position].faceAttributes is not null because it works for other attributes like age and gender but I am unable to figure out why it is not working for emotions. Does anyone know why this is occurring and what I can do to get it to work?


Update:

For those who are experiencing the same problem, in the AsnycTask where you are processing the faces, you must include the attributes you wish to detect otherwise it says that it is a null object reference when you refer to them later. Initially, I had FaceServiceClient.FaceAttributeType.Smile and that was why it was giving me an error when trying to determine emotions. The following code goes in the doInBackground method:

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

Solution

  • It looks like you are not asking for emotion attributes during your request. All these face attributes are optional, so the returned object will only contain the one you asked. For example with the following request:

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

    I will got the following reply, where the JSON, when parsed, send an object with null for emotion

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

    If I add emotion in the returnFaceAttributes of the request:

    [{
      "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
        }
      }
    }]
    

    I don't know how you used the API (direct calling or using a package), but you should definitely have a look to these parameters of they are available at your level. If they are not, use directly the API (doc: https://westeurope.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/)