-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
216 lines (186 loc) · 8.26 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CaptionImage
{
static class Program
{
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
const string subscriptionKey = "28e9a946d07547cf8fbf89179921445a";
// Replace or verify the region.
//
// You must use the same region in your REST API call as you used to obtain your subscription keys.
// For example, if you obtained your subscription keys from the westus region, replace
// "westcentralus" in the URI below with "westus".
//
// NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using
// a free trial subscription key, you should not need to change this region.
//
// Also, if you want to use the celebrities model, change "landmarks" to "celebrities" here and in
// requestParameters to use the Celebrities model.
const string uriBase = "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze";
static Dictionary<string, string> diction = new Dictionary<string, string>();
static List<string> fileList = new List<string>();
public static async Task<string> StartMyTask(string imagepath)
{
return MakeAnalysisRequest(imagepath).Result;
// code to execute once foo is done
}
static string htmlprefix = @"<html>
<head ><style>
table, tr, th, td {
border: 1px solid black;
}<style></head>
<body>
<Table>";
static string htmlpostfix = @"</Table>
</body>
</html>";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Domain-Specific Model:");
Console.Write("Enter the path to an images you wish to analzye for Description : ");
string imageFilePath = Console.ReadLine();
var files = Directory.GetFiles(imageFilePath);
string content = "";
foreach (var file in files)
{
fileList.Add(file);
//diction.Add(file, "");
// Execute the REST API call.
var dis = StartMyTask(file);
diction.Add(file, dis.Result);
content += "<tr><td><image src=\"" + file.Replace("\\", "\\\\") + "\"></td>";
content += "<td>" + dis.Result + "</td></tr>";
}
string all = htmlprefix + content + htmlpostfix;
Console.WriteLine(all);
File.WriteAllText("C:\\videoextract\\stroy.html", all);
Console.WriteLine("\nPlease wait a moment for the results to appear. Then, press Enter to exit ...\n");
Console.ReadLine();
}
/// <summary>
/// Gets a thumbnail image from the specified image file by using the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file to use to create the thumbnail image.</param>
static async Task<string> MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. Change "landmarks" to "celebrities" here and in uriBase to use the Celebrities model.
string requestParameters = "visualFeatures=Description&details=Celebrities&language=en";
// 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);
string description = "";
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));
dynamic jObj = JsonConvert.DeserializeObject(contentString);
if (jObj != null && jObj.description != null && jObj.description.captions != null)
{
Console.WriteLine(imageFilePath);
foreach (dynamic obj in jObj.description.captions)
{
Console.WriteLine(obj.text);
description = obj.text;
}
}
}
return description;
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
/// <summary>
/// Formats the given JSON string by adding line breaks and indents.
/// </summary>
/// <param name="json">The raw JSON string to format.</param>
/// <returns>The formatted JSON string.</returns>
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();
}
}
}