R
To begin with, we shall prepare the necessary:Library Newtonsoft.Json for easy work with JSON.API key https://azure.microsoft.com/try/cognitive-services/?api=face-api produces two keys and a server that accepts them, and is designed for convenience in the station:const string Key = "12345678901234567890";
Next, for convenience, we will separate server (location) for API:const string Location = "westcentralus";
Now we need two API addresses: https://westcentralus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236 - determines what the image is and gives it 24 hours.static readonly string DetectApi = $"https://{Location}.api.cognitive.microsoft.com/face/v1.0/detect";
https://westcentralus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a - checks two Id images.static readonly string VerifyApi = $"https://{Location}.api.cognitive.microsoft.com/face/v1.0/verify";
Now we need to make a simple method for sending POST to the server,' he's gonna accept the site for a further episode in JSON, point the key in the heading (authorization), send and give the answer:private static async Task<string> SendRequest(string url, object data)
{
string result;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Key);
var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
result = await response.Content.ReadAsStringAsync();
}
return result;
}
Now, for convenience, we will create a class that will contain url images and its Id:class Image
{
public Image(string url)
{
Url = url;
}
public string Url { get; set; }
public string Id { get; set; }
}
The last thing we need is a collection of our paintings:var images = new List<Image>
{
new Image("https://st.kp.yandex.net/images/actor_iphone/iphone360_6793.jpg"),
new Image("https://images2.cosmopolitan.ru/upload/img_cache/f15/f15b62dc9a5b8584b1bc87b50ce25e83_fitted_740x0.jpg")
};
With the training done, let's get those Id images now, so we'll just go through the collection and, for each facility, we'll follow the DetectApi address:foreach (var image in images)
{
var data = new { url = image.Url };
var result = await SendRequest(DetectApi, data);
var json = JArray.Parse(result);
image.Id = (string) json[0]["faceId"];
}
var data = new { url = image.Url }; - form an object (anonymous type) that will be in the method SendRequest == sync, corrected by elderman == @elder_man { "url": "http://example.com/1.jpg" }♪var result = await SendRequest(DetectApi, data); - As a result, a response from API with JSON object (Array) which will contain the necessary response. faceId and other data.var json = JArray.Parse(result); - a password at JArray.image.Id = (string) json[0]["faceId"]; - since it's a mass with one object, we're taking it from the first site. faceId♪ We're moving into string and we're going to make it into ours ♪ image♪As a result of all these actions, we will have a mass images They're in every picture.Well, with Id, we only have to check them through. VerifyApi, write a simple method that will take 2 string (Id), send a request and result back to us:private static async Task<JObject> Verify(string faceId1, string faceId2)
{
var data = new {faceId1, faceId2};
var result = await SendRequest(VerifyApi, data);
return JObject.Parse(result);
}
Call for:var verify = await Verify(images[0].Id, images[1].Id);
Let's get the data out.Console.WriteLine($"Результат: {verify["isIdentical"]}");
Console.WriteLine($"Уверен на {verify["confidence"]}");
In the end, they receive:Результат: True
Уверен на 0,61441
Marathet (in the form of errors and settings only in your places) will lead on your own, do almost on your knees, but it kind of works.Good luck studying C#!