Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Net.Http;
- using System.Net.Http.Headers;
- namespace FaceDetection2
- {
- class Program
- {
- // Replace <Subscription Key> with your valid subscription key.
- const string subscriptionKey = "230b4a3b313e43b9aab10e464672a601";
- // replace <myresourcename> with the string found in your endpoint URL
- const string uriBase =
- "https://facedetectionscrum.cognitiveservices.azure.com/face/v1.0/detect";
- 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.
- static async void MakeAnalysisRequest(string imageFilePath)
- {
- HttpClient client = new HttpClient();
- // Request headers.
- client.DefaultRequestHeaders.Add(
- "Ocp-Apim-Subscription-Key", subscriptionKey);
- // Request parameters. A third optional parameter is "details".
- string requestParameters = "returnFaceAttributes=age";
- // 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 ByteArrayContent(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(JsonPrettyPrint(contentString));
- Console.WriteLine("\nPress Enter to exit...");
- }
- }
- // Returns the contents of the specified file as a byte array.
- static byte[] GetImageAsByteArray(string imageFilePath)
- {
- using (FileStream fileStream =
- new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
- {
- BinaryReader binaryReader = new BinaryReader(fileStream);
- return binaryReader.ReadBytes((int)fileStream.Length);
- }
- }
- // Formats the given JSON string by adding line breaks and indents.
- static string JsonPrettyPrint(string json)
- {
- if (string.IsNullOrEmpty(json))
- return string.Empty;
- json = json.Replace(Environment.NewLine, "").Replace("\t", "");
- StringBuilder sb = new StringBuilder();
- bool quote = false;
- bool ignore = false;
- int offset = 0;
- int indentLength = 3;
- foreach (char ch in json)
- {
- switch (ch)
- {
- case '"':
- if (!ignore) quote = !quote;
- break;
- case '\'':
- if (quote) ignore = !ignore;
- break;
- }
- if (quote)
- sb.Append(ch);
- else
- {
- switch (ch)
- {
- case '{':
- case '[':
- sb.Append(ch);
- sb.Append(Environment.NewLine);
- sb.Append(new string(' ', ++offset * indentLength));
- break;
- case '}':
- case ']':
- sb.Append(Environment.NewLine);
- sb.Append(new string(' ', --offset * indentLength));
- sb.Append(ch);
- break;
- case ',':
- sb.Append(ch);
- sb.Append(Environment.NewLine);
- sb.Append(new string(' ', offset * indentLength));
- break;
- case ':':
- sb.Append(ch);
- sb.Append(' ');
- break;
- default:
- if (ch != ' ') sb.Append(ch);
- break;
- }
- }
- }
- return sb.ToString().Trim();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement