Microsoft Azure Face API-人脸检测(无结果)

时间:2018-07-24 07:04:29

标签: azure microsoft-cognitive face-api

使用Face API V1.0的面部检测服务时,我没有遇到错误,但也没有结果。我正在使用C#。 API密钥有效,并在其他代码上显示结果。

感谢您的帮助。预先感谢。

Code is as follows:

using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;

namespace CSHttpClientSample
{
static class Program
{
static void Main()
{
MakeRequest();
Console.WriteLine("Hit ENTER to exit...");
Console.ReadLine();
}

static async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);

// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "9b0bd0ce75d040769834af2339b93e1d");

// Request parameters
queryString["returnFaceId"] = "true";
queryString["returnFaceLandmarks"] = "false";
queryString["returnFaceAttributes"] = "Age";
//var uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect" + queryString;
var uri = "https://eastasia.api.cognitive.microsoft.com/face/v1.0/detect" + queryString;

HttpResponseMessage response;

// Request body
byte[] byteData = Encoding.UTF8.GetBytes("https://i.kinja-img.com/gawker-media/image/upload/s--0MPvwvU0--/c_scale,f_auto,fl_progressive,q_80,w...");

using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

response = await client.PostAsync(uri, content);
}

}
}
}

1 个答案:

答案 0 :(得分:0)

正如evilsnobu提到的那样,如果要获得响应,则需要返回MakeRequest()结果。

如果要使用Web图片资源,则需要将其网址添加为帖子正文。

请尝试使用下面的代码来获得适当的结果。

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 static void Main(string[] args)
 {
   var response = MakeRequest().Result;
   var content = response.Content.ReadAsStringAsync().Result
 }

 static async Task<HttpResponseMessage> MakeRequest()
  {
            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "your key");

            // Request parameters
            queryString["returnFaceId"] = "true";
            queryString["returnFaceLandmarks"] = "false";
            queryString["returnFaceAttributes"] = "Age";
            var uri = "https://eastasia.api.cognitive.microsoft.com/face/v1.0/detect?" + queryString; //add ? before query string

            var data = new JObject
            {
                ["url"] =
                    "https://docs.microsoft.com/en-us/azure/cognitive-services/face/images/facefindsimilar.queryface.jpg"
            };
            var json = JsonConvert.SerializeObject(data);
            var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await client.PostAsync(uri, stringContent);

            return response;

        }

enter image description here

如果可以使用C#SDK,则可以使用 Microsoft.ProjectOxford.Face 来完成。有关如何使用Microsoft.ProjectOxford.Face的更多信息,请参阅此tutorial

参考:

How to Detect Faces in Image

更新

如果您想使用以下API

 https://[location].api.cognitive.microsoft.com/face/v1.0/group

faceIds是一个数组,请尝试使用以下代码:

 var jArray = new JArray
   {
        "cd920c85-d05e-46e2-bb9e-2b0f67024ba2",
        "56001f6e-6c48-4a9d-800d-3cc693001197",
        "2ae34ecc-eb17-4260-a385-0db699ee17c4"
   };

 var data = new JObject
        {
           ["faceIds"] = jArray

        };
 var json = JsonConvert.SerializeObject(data);