我们尝试使用SDK使用微软的cognitives服务。我们使用接口IFaceOperatiosn至极内有由像流发送图片的方法:DetectWithStreamWithHttpMessagesAsync。当我们试图使用它,我们达到APIErrorException至极的消息是错误的请求,但不知道哪里出了问题,所以有我们的代码:
public async Task<List<FaceAPI.Face>> DetectFace(string picture)
{
try
{
Stream img = new FileStream(picture, FileMode.Open);
var res = await detect.DetectWithStreamWithHttpMessagesAsync(img);
List<FaceAPI.Face> result = new List<FaceAPI.Face>();
for (int i = 0; i < res.Body.Count; i++)
{
result.Add(new FaceAPI.Face { Age = (double)res.Body[i].FaceAttributes.Age, Bald = res.Body[i].FaceAttributes.Hair.Bald > 0.5 ? true : false, Beard = res.Body[i].FaceAttributes.FacialHair.Beard > 0.5 ? true : false, Gender = res.Body[i].FaceAttributes.Gender.Value.Equals(Gender.Male) ? true : false, Glasses = res.Body[i].FaceAttributes.Glasses.Value.Equals(GlassesType.NoGlasses) ? false : true, Hair = res.Body[i].FaceAttributes.Hair.HairColor.ToString(), Moustache = res.Body[i].FaceAttributes.FacialHair.Moustache > 0.5 ? true : false,Rectangle=new System.Drawing.Rectangle { X = res.Body[i].FaceRectangle.Left, Y = res.Body[i].FaceRectangle.Top, Height = res.Body[i].FaceRectangle.Height, Width = res.Body[i].FaceRectangle.Width } });
}
return result;
}
catch (APIErrorException e)
{
Debug.WriteLine(e.Message);
return null;
}
catch (SerializationException e)
{
Debug.WriteLine(e.Message);
return null;
}
catch (ValidationException e)
{
Debug.WriteLine(e.Message);
return null;
}
}
通常情况下它返回脸部列表
你需要把你想要检索这样的检测至极属性:
var requiredFaceAttributes = new FaceAttributeType[] {
FaceAttributeType.Age,
FaceAttributeType.Hair,
FaceAttributeType.Gender,
FaceAttributeType.Smile,
FaceAttributeType.FacialHair,
FaceAttributeType.Glasses
};
var res = await detect.DetectWithStreamWithHttpMessagesAsync(picture,true,true,requiredFaceAttributes);