0

在使用人脸 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);
}

}
}
}
4

1 回答 1

1

正如 evilsnobu 提到的,如果你想得到响应,你需要返回 MakeRequest() 结果。

如果你想使用网络图片资源,你需要添加它的url作为post body。

请尝试使用以下代码来获得预期的结果。

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;

        }

在此处输入图像描述

如果 C# SDK 是可能的,您可以使用 Microsoft.ProjectOxford.Face来做到这一点。有关如何使用 Microsoft.ProjectOxford.Face 的更多信息,请参阅本教程

参考:

如何检测图像中的人脸

更新:

如果您想使用以下 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);
于 2018-07-24T09:55:16.570 回答