Advertisement
Guest User

C scherp

a guest
Dec 6th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Net.Http;
  8. using System.Net.Http.Headers;
  9.  
  10. namespace FaceDetection2
  11. {
  12. class Program
  13. {
  14. // Replace <Subscription Key> with your valid subscription key.
  15. const string subscriptionKey = "230b4a3b313e43b9aab10e464672a601";
  16.  
  17. // replace <myresourcename> with the string found in your endpoint URL
  18. const string uriBase =
  19. "https://facedetectionscrum.cognitiveservices.azure.com/face/v1.0/detect";
  20.  
  21. static void Main(string[] args)
  22. {
  23.  
  24. // Get the path and filename to process from the user.
  25. Console.WriteLine("Detect faces:");
  26. Console.Write(
  27. "Enter the path to an image with faces that you wish to analyze: ");
  28. string imageFilePath = Console.ReadLine();
  29.  
  30. if (File.Exists(imageFilePath))
  31. {
  32. try
  33. {
  34. MakeAnalysisRequest(imageFilePath);
  35. Console.WriteLine("\nWait a moment for the results to appear.\n");
  36. }
  37. catch (Exception e)
  38. {
  39. Console.WriteLine("\n" + e.Message + "\nPress Enter to exit...\n");
  40. }
  41. }
  42. else
  43. {
  44. Console.WriteLine("\nInvalid file path.\nPress Enter to exit...\n");
  45. }
  46. Console.ReadLine();
  47. }
  48.  
  49. // Gets the analysis of the specified image by using the Face REST API.
  50. static async void MakeAnalysisRequest(string imageFilePath)
  51. {
  52. HttpClient client = new HttpClient();
  53.  
  54. // Request headers.
  55. client.DefaultRequestHeaders.Add(
  56. "Ocp-Apim-Subscription-Key", subscriptionKey);
  57.  
  58. // Request parameters. A third optional parameter is "details".
  59. string requestParameters = "returnFaceAttributes=age";
  60.  
  61. // Assemble the URI for the REST API Call.
  62. string uri = uriBase + "?" + requestParameters;
  63.  
  64. HttpResponseMessage response;
  65.  
  66. // Request body. Posts a locally stored JPEG image.
  67. byte[] byteData = GetImageAsByteArray(imageFilePath);
  68.  
  69. using (ByteArrayContent content = new ByteArrayContent(byteData))
  70. {
  71. // This example uses content type "application/octet-stream".
  72. // The other content types you can use are "application/json"
  73. // and "multipart/form-data".
  74. content.Headers.ContentType =
  75. new MediaTypeHeaderValue("application/octet-stream");
  76.  
  77. // Execute the REST API call.
  78. response = await client.PostAsync(uri, content);
  79.  
  80. // Get the JSON response.
  81. string contentString = await response.Content.ReadAsStringAsync();
  82.  
  83. // Display the JSON response.
  84. Console.WriteLine("\nResponse:\n");
  85. Console.WriteLine(JsonPrettyPrint(contentString));
  86. Console.WriteLine("\nPress Enter to exit...");
  87. }
  88. }
  89.  
  90. // Returns the contents of the specified file as a byte array.
  91. static byte[] GetImageAsByteArray(string imageFilePath)
  92. {
  93. using (FileStream fileStream =
  94. new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
  95. {
  96. BinaryReader binaryReader = new BinaryReader(fileStream);
  97. return binaryReader.ReadBytes((int)fileStream.Length);
  98. }
  99. }
  100. // Formats the given JSON string by adding line breaks and indents.
  101. static string JsonPrettyPrint(string json)
  102. {
  103. if (string.IsNullOrEmpty(json))
  104. return string.Empty;
  105.  
  106. json = json.Replace(Environment.NewLine, "").Replace("\t", "");
  107.  
  108. StringBuilder sb = new StringBuilder();
  109. bool quote = false;
  110. bool ignore = false;
  111. int offset = 0;
  112. int indentLength = 3;
  113.  
  114. foreach (char ch in json)
  115. {
  116. switch (ch)
  117. {
  118. case '"':
  119. if (!ignore) quote = !quote;
  120. break;
  121. case '\'':
  122. if (quote) ignore = !ignore;
  123. break;
  124. }
  125.  
  126. if (quote)
  127. sb.Append(ch);
  128. else
  129. {
  130. switch (ch)
  131. {
  132. case '{':
  133. case '[':
  134. sb.Append(ch);
  135. sb.Append(Environment.NewLine);
  136. sb.Append(new string(' ', ++offset * indentLength));
  137. break;
  138. case '}':
  139. case ']':
  140. sb.Append(Environment.NewLine);
  141. sb.Append(new string(' ', --offset * indentLength));
  142. sb.Append(ch);
  143. break;
  144. case ',':
  145. sb.Append(ch);
  146. sb.Append(Environment.NewLine);
  147. sb.Append(new string(' ', offset * indentLength));
  148. break;
  149. case ':':
  150. sb.Append(ch);
  151. sb.Append(' ');
  152. break;
  153. default:
  154. if (ch != ' ') sb.Append(ch);
  155. break;
  156. }
  157. }
  158. }
  159.  
  160. return sb.ToString().Trim();
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement