我正在使用 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
};