在Microsoft认知服务中使用Face API

时间:2017-03-30 11:56:23

标签: c# microsoft-cognitive

如何在面部检测api中发送二进制数据

https://westus.api.cognitive.microsoft.com/face/v1.0/detect

当我发送二进制数据时,它给出了以下错误

{
  "error": {
    "code": "InvalidImage",
    "message": "Decoding error, image format unsupported."
  }
}

谢谢, Saroj

2 个答案:

答案 0 :(得分:0)

String AzureURL = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect";
String FaceID = "";

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriotionKey);
string queryString = "returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender";

queryString = "returnFaceId=true&returnFaceLandmarks=false";
string uri = AzureURL + "?" + queryString;

HttpResponseMessage response;
string JSONString;


using (var content = new ByteArrayContent(ImageBytes))
{
      content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
      response = client.PostAsync(uri, content).Result;
      JSONString = response.Content.ReadAsStringAsync().Result;

      JSONString = JSONString.Replace("[", "");
      JSONString = JSONString.Replace("]", "");

      var definition = new { faceId = "" };
      var FaceGUID = JsonConvert.DeserializeAnonymousType(JSONString, definition);

      if(JSONString != "")
          FaceID = FaceGUID.faceId;
}

答案 1 :(得分:0)

我不认为你可以将图像上传为字节。根据文件, POST的正文添加一个面部图像只需要一个URL。 https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523b