Face Detection

The face detection API detects faces and returns their coordinates. It functions similarly to the face recognition API except that it does not perform recognition.

Example

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace appone
{
class Response {
public bool success {get;set;}
public Face[] predictions {get;set;}
}
class Face {
public string gender {get;set;}
public float confidence {get;set;}
public int y_min {get;set;}
public int x_min {get;set;}
public int y_max {get;set;}
public int x_max {get;set;}
}
class App {
static HttpClient client = new HttpClient();
public static async Task detectFace(string image_path){
var request = new MultipartFormDataContent();
var image_data = File.OpenRead(image_path);
request.Add(new StreamContent(image_data),"image",Path.GetFileName(image_path));
var output = await client.PostAsync("http://localhost:80/v1/vision/face",request);
var jsonString = await output.Content.ReadAsStringAsync();
Response response = JsonConvert.DeserializeObject<Response>(jsonString);
Console.WriteLine(jsonString);
}
static void Main(string[] args){
detectFace("family.jpg").Wait();
}
}
}

Response

{'predictions': [{'x_max': 712, 'y_max': 261, 'x_min': 626, 'confidence': 0.99990666, 'y_min': 145}, {'x_max': 620, 'y_max': 288, 'x_min': 543, 'confidence': 0.99986553, 'y_min': 174}, {'x_max': 810, 'y_max': 242, 'x_min': 731, 'confidence': 0.99986434, 'y_min': 163}, {'x_max': 542, 'y_max': 279, 'x_min': 477, 'confidence': 0.99899536, 'y_min': 197}], 'success': True}

We can use the coordinates returned to extract the faces from the image

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;
namespace appone
{
class Response {
public bool success {get;set;}
public Face[] predictions {get;set;}
}
class Face {
public string gender {get;set;}
public float confidence {get;set;}
public int y_min {get;set;}
public int x_min {get;set;}
public int y_max {get;set;}
public int x_max {get;set;}
}
class App {
static HttpClient client = new HttpClient();
public static async Task recognizeFace(string image_path){
var request = new MultipartFormDataContent();
var image_data = File.OpenRead(image_path);
request.Add(new StreamContent(image_data),"image",Path.GetFileName(image_path));
var output = await client.PostAsync("http://localhost:80/v1/vision/face",request);
var jsonString = await output.Content.ReadAsStringAsync();
Response response = JsonConvert.DeserializeObject<Response>(jsonString);
var i = 0;
foreach (var user in response.predictions){
var width = user.x_max - user.x_min;
var height = user.y_max - user.y_min;
var crop_region = new Rectangle(user.x_min,user.y_min,width,height);
using(var image = Image.Load(image_path)){
image.Mutate(x => x
.Crop(crop_region)
);
image.Save(i.ToString() + "_.jpg");
}
i++;
}
}
static void Main(string[] args){
recognizeFace("family.jpg").Wait();
}
}
}