Search code examples
azureazure-cognitive-servicesface-api

Microsoft Azure Face API - Face detect (no result)


While using Face detect service of Face API V1.0 I encounter no errors but no result also. I am working on c#. API key is valid and shows result on other codes.

Any help is appreciated. Thanks in advance.

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);
}

}
}
}

Solution

  • As evilsnobu mentioned that if you want to get response, you need to return MakeRequest() result.

    If you want to use the web image resource, you need to add it's url as post body.

    Please have a try to use the following code to get the expeceted result.

    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://learn.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

    If C# SDK is possible, you could use Microsoft.ProjectOxford.Face to do that. For more information about how to use Microsoft.ProjectOxford.Face please refer to this tutorial.

    Reference:

    How to Detect Faces in Image

    Update:

    If you want to use the following API

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

    The faceIds is an array, please have a try to use the following code:

     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);