· 6 years ago · Jan 22, 2020, 08:56 AM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Net.Http;
7using System.Net.Http.Headers;
8using System.Web;
9using System.IO;
10using System.Collections;
11using System.Runtime.InteropServices;
12using Newtonsoft.Json.Linq;
13using Newtonsoft.Json;
14using System.Web.Script.Serialization;
15
16namespace ACT_SKUSKA
17{
18 class Program
19 {
20
21 // Replace <Subscription Key> with your valid subscription key.
22 const string subscriptionKey = "1ef2a67d893a4e93a8f186e3ef0c7b59";
23 static List<string> list = new List<string>();
24 static string helper = "";
25
26 // replace <myresourcename> with the string found in your endpoint URL
27 const string uriBase =
28 "https://faceappcloudy.cognitiveservices.azure.com/face/v1.0/detect";
29
30 const string imageFilePath = @"D:\Skola\ACT\Skuska\famiily-inner-pic.jpg";
31
32 static void Main(string[] args)
33 {
34
35 // Get the path and filename to process from the user.
36 Console.WriteLine("Detect faces:");
37 //Console.Write(
38 //"Enter the path to an image with faces that you wish to analyze: ");
39
40 if (File.Exists(imageFilePath))
41 {
42 try
43 {
44 MakeAnalysisRequest(imageFilePath);
45 Console.WriteLine("\nWait a moment for the results to appear.\n");
46 }
47 catch (Exception e)
48 {
49 Console.WriteLine("\n" + e.Message + "\nPress Enter to exit...\n");
50 }
51 }
52 else
53 {
54 Console.WriteLine("\nInvalid file path.\nPress Enter to exit...\n");
55 }
56
57 Console.ReadLine();
58 }
59 // Gets the analysis of the specified image by using the Face REST API.
60 static async void MakeAnalysisRequest(string imageFilePath)
61 {
62 HttpClient client = new HttpClient();
63
64 // Request headers.
65 client.DefaultRequestHeaders.Add(
66 "Ocp-Apim-Subscription-Key", subscriptionKey);
67
68 // Request parameters. A third optional parameter is "details".
69 string requestParameters = "returnFaceId=true&returnFaceLandmarks=false" +
70 "&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses," +
71 "emotion,hair,makeup,occlusion,accessories,blur,exposure,noise";
72
73 // Assemble the URI for the REST API Call.
74 string uri = uriBase + "?" + requestParameters;
75
76 HttpResponseMessage response;
77
78 // Request body. Posts a locally stored JPEG image.
79 byte[] byteData = GetImageAsByteArray(imageFilePath);
80
81 using (ByteArrayContent content = new ByteArrayContent(byteData))
82 {
83 // This example uses content type "application/octet-stream".
84 // The other content types you can use are "application/json"
85 // and "multipart/form-data".
86 content.Headers.ContentType =
87 new MediaTypeHeaderValue("application/octet-stream");
88
89 // Execute the REST API call.
90 response = await client.PostAsync(uri, content);
91
92 // Get the JSON response.
93 string contentString = await response.Content.ReadAsStringAsync();
94
95 // Display the JSON response.
96 Console.WriteLine("\nResponse:\n");
97 //Console.WriteLine(JsonPrettyPrint(contentString));
98 //var address = new JavaScriptSerializer().Deserialize<dynamic>(contentString);
99 JsonPrettyPrint(contentString);
100 Console.WriteLine(list.Count);
101
102 //Console.WriteLine(address[1]);
103
104 Console.WriteLine("\nPress Enter to exit...");
105 }
106 }
107 // Returns the contents of the specified file as a byte array.
108 static byte[] GetImageAsByteArray(string imageFilePath)
109 {
110 using (FileStream fileStream =
111 new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
112 {
113 BinaryReader binaryReader = new BinaryReader(fileStream);
114 return binaryReader.ReadBytes((int)fileStream.Length);
115 }
116 }
117
118 // Formats the given JSON string by adding line breaks and indents.
119 static public void JsonPrettyPrint(string json)
120 {
121 //if (string.IsNullOrEmpty(json))
122 // return string.Empty;
123
124 json = json.Replace(Environment.NewLine, "").Replace("\t", "");
125
126
127
128 StringBuilder sb = new StringBuilder();
129 bool quote = false;
130 bool ignore = false;
131 int offset = 0;
132 int indentLength = 3;
133
134 foreach (char ch in json)
135 {
136 switch (ch)
137 {
138 case '"':
139 if (!ignore) quote = !quote;
140 break;
141 case '\'':
142 if (quote) ignore = !ignore;
143 break;
144 }
145
146 if (quote)
147 sb.Append(ch);
148 else
149 {
150 switch (ch)
151 {
152 case '{':
153 case '[':
154 sb.Append(ch);
155 sb.Append(Environment.NewLine);
156 sb.Append(new string(' ', ++offset * indentLength));
157 break;
158 case '}':
159 case ']':
160 sb.Append(Environment.NewLine);
161 sb.Append(new string(' ', --offset * indentLength));
162 sb.Append(ch);
163 break;
164 case ',':
165 sb.Append(ch);
166 sb.Append(Environment.NewLine);
167 sb.Append(new string(' ', offset * indentLength));
168 break;
169 case ':':
170 sb.Append(ch);
171 sb.Append(' ');
172 break;
173 default:
174 if (ch != ' ') sb.Append(ch);
175 break;
176 }
177 }
178 }
179 string[] lines = sb.ToString().Split('\n');
180
181 foreach (string line in lines)
182 {
183 Console.WriteLine(line);
184 if (line.Contains("faceId") || line.Contains("age"))
185 {
186 list.Add(line);
187 }
188
189 }
190
191
192
193 }
194 }
195}