-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
110 lines (90 loc) · 3.99 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
namespace FaceDetection;
public class Program
{
// DOC: https://docs.microsoft.com/cs-cz/azure/cognitive-services/face/QuickStarts/CSharp
// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<<YOUR SUBSCRIPTION KEY>>";
// NOTE: You must use the same region in your REST call as you used to
// obtain your subscription keys. For example, if you obtained your
// subscription keys from westus, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase = "https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect";
public static void Main(string[] args)
{
// Get the path and filename to process from the user.
Console.WriteLine("Detect faces:");
Console.Write("Enter the path to an image with faces that you wish to analyze: ");
string imageFilePath = Console.ReadLine();
if (File.Exists(imageFilePath))
{
try
{
MakeAnalysisRequest(imageFilePath);
Console.WriteLine("\nWait a moment for the results to appear.\n");
}
catch (Exception e)
{
Console.WriteLine("\n" + e.Message + "\nPress Enter to exit...\n");
}
}
else
{
Console.WriteLine("\nInvalid file path.\nPress Enter to exit...\n");
}
Console.ReadLine();
}
// Gets the analysis of the specified image by using the Face REST API.
private static async void MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "returnFaceLandmarks=false" +
"&returnFaceAttributes=headPose,glasses," +
"occlusion,accessories,blur,exposure,noise";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using ByteArrayContent content = new(byteData);
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettify(contentString));
Console.WriteLine("\nPress Enter to exit...");
}
// Returns the contents of the specified file as a byte array.
private static byte[] GetImageAsByteArray(string imageFilePath)
{
using FileStream fileStream = new(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
private static string JsonPrettify(string json)
{
using var stringReader = new StringReader(json);
using var stringWriter = new StringWriter();
var jsonReader = new JsonTextReader(stringReader);
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Newtonsoft.Json.Formatting.Indented };
jsonWriter.WriteToken(jsonReader);
return stringWriter.ToString();
}
}