Created
May 13, 2019 15:11
-
-
Save OlafenwaMoses/0b1817fb5379ad27708730347979079b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment