/
recognizeProxy.cs
469 lines (413 loc) · 19.3 KB
/
recognizeProxy.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
using System.Drawing;
using System.Collections;
namespace recognize
{
enum RecognitionMode { single, multi };
/// <summary>
/// Recognition response
/// </summary>
class recognitionResponse
{
public int status { get; set; }
public string message { get; set; }
public List<recognitionObject> objects { get; set; }
/// <summary>
/// Draws frames on image
/// </summary>
/// <param name="image">Input image</param>
/// <returns>Image with frames</returns>
public Image drawFrames(Image image)
{
if (this.status == 0)
{
Pen blackPen = new Pen(Color.Red, 3);
Font drawFont = new Font("Arial", 32);
SolidBrush drawBrush = new SolidBrush(Color.Red);
using (var graphics = Graphics.FromImage(image))
{
foreach (var obj in this.objects)
{
recognitionPoint[] locations = obj.location.ToArray();
graphics.DrawLine(blackPen, locations[0].x, locations[0].y, locations[1].x, locations[1].y);
graphics.DrawLine(blackPen, locations[1].x, locations[1].y, locations[2].x, locations[2].y);
graphics.DrawLine(blackPen, locations[2].x, locations[2].y, locations[3].x, locations[3].y);
graphics.DrawLine(blackPen, locations[3].x, locations[3].y, locations[0].x, locations[0].y);
graphics.DrawString(obj.id, drawFont, drawBrush, locations[0].x, locations[0].y);
}
}
return image;
}
return null;
}
}
class recognitionObject
{
public string id { get; set; }
public string name { get; set; }
public List<recognitionPoint> location { get; set; }
}
class recognitionPoint
{
public int x { get; set; }
public int y { get; set; }
}
/// <summary>
/// Class to handle requests to recognize.im API.
/// </summary>
class recognizeProxy
{
private CookieContainer cookieJar;
private string clientId;
private string apiKey;
private string clapiKey;
//These are the limits for query images:
//for SingleIR
private double SINGLEIR_MAX_FILE_SIZE = 500.0; //KBytes
private int SINGLEIR_MIN_DIMENSION = 100; //pix
private double SINGLEIR_MIN_IMAGE_SURFACE = 0.05; //Mpix
private double SINGLEIR_MAX_IMAGE_SURFACE = 0.31; //Mpix
//for MultipleIR
private double MULTIPLEIR_MAX_FILE_SIZE = 3500.0; //KBytes
private int MULTIPLEIR_MIN_DIMENSION = 100; //pix
private double MULTIPLEIR_MIN_IMAGE_SURFACE = 0.1; //Mpix
private double MULTIPLEIR_MAX_IMAGE_SURFACE = 5.1; //Mpix
/// <summary>
/// Constructor
/// </summary>
/// <param name="clientId">Your unique client ID. You can find it in the Account tab after logging in at recognize.im.</param>
/// <param name="apiKey">Your unique API key. You can find it in the Account tab after logging in at recognize.im.</param>
/// <param name="clapiKey">Your unique secret client key. You can find it in the Account tab after logging in at recognize.im.</param>
public recognizeProxy(string clientId, string apiKey, string clapiKey)
{
this.clientId = clientId;
this.apiKey = apiKey;
this.clapiKey = clapiKey;
string oRequest = "";
oRequest = oRequest + "<client_id xsi:type=\"xsd:string\">" + clientId + "</client_id>";
oRequest = oRequest + "<key_clapi xsi:type=\"xsd:string\">" + clapiKey + "</key_clapi>";
oRequest = oRequest + "<ip xsi:type=\"xsd:string\"></ip>";
this.cookieJar = new CookieContainer();
Dictionary<string, string> result = sendSoapRequest(oRequest, "auth");
}
/// <summary>
/// Converts response from xml to Dictionary<string, string>
/// </summary>
/// <param name="response">SOAP response</param>
/// <returns>SOAP response converted to Dictionary<string, string></returns>
private static Dictionary<string, string> convertResponse(Stream response)
{
XDocument doc = XDocument.Load(response);
var results = doc.Descendants("item");
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var result in results)
{
if (!result.Element("value").HasElements) dict[result.Element("key").Value] = result.Element("value").Value;
}
return dict;
}
/// <summary>
/// Generates MD5 from the glued: API key and the image.
/// </summary>
/// <param name="api_key">Unique API key.</param>
/// <param name="imageBytes">Image data in byte[] array</param>
/// <returns>Generated MD5 hash.</returns>
private static String getMD5(String api_key, byte[] imageBytes)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] api = System.Text.Encoding.ASCII.GetBytes(api_key);
byte[] glued = new byte[api.Length + imageBytes.Length];
System.Buffer.BlockCopy(api, 0, glued, 0, api.Length);
System.Buffer.BlockCopy(imageBytes, 0, glued, api.Length, imageBytes.Length);
byte[] retVal = md5.ComputeHash(glued);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
md5.Clear();
return sb.ToString();
}
/// <summary>
/// Send SOAP request to recognize.im
/// </summary>
/// <param name="body">Body of request</param>
/// <param name="operation">SOAP opperation</param>
/// <returns>Response converted to Dictionary<string, string></returns>
private Dictionary<string, string> sendSoapRequest(string body, string operation)
{
string oRequest = "";
oRequest = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://clapi.itraff.pl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns2=\"http://xml.apache.org/xml-soap\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
oRequest = oRequest + "<SOAP-ENV:Body>";
oRequest = oRequest + "<ns1:" + operation + ">";
oRequest = oRequest + body;
oRequest = oRequest + "</ns1:" + operation + ">";
oRequest = oRequest + "</SOAP-ENV:Body>";
oRequest = oRequest + "</SOAP-ENV:Envelope>";
//Builds the connection to the WebService.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://clapi.itraff.pl/");
req.Headers.Add("SOAPAction", "\"http://clapi.itraff.pl#" + operation + "\"");
req.ContentType = "text/xml; charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
req.CookieContainer = this.cookieJar;
//Passes the SoapRequest String to the WebService
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(oRequest);
}
}
//Gets the response
WebResponse response = req.GetResponse();
//Writes the Response
Stream responseStream = response.GetResponseStream();
return convertResponse(responseStream);
}
/// <summary>
/// Authentication
/// </summary>
/// <param name="clientId">Your unique client ID. You can find it in the Account tab after logging in at recognize.im.</param>
/// <param name="clapiKey">Your unique secret client key. You can find it in the Account tab after logging in at recognize.im.</param>
/// <returns>Server response</returns>
public Dictionary<string, string> auth(string clientId, string clapiKey)
{
string oRequest = "";
oRequest = oRequest + "<client_id xsi:type=\"xsd:string\">" + clientId + "</client_id>";
oRequest = oRequest + "<key_clapi xsi:type=\"xsd:string\">" + clapiKey + "</key_clapi>";
oRequest = oRequest + "<ip xsi:type=\"xsd:string\"></ip>";
return sendSoapRequest(oRequest, "auth");
}
/// <summary>
/// You need to call indexBuild method in order to apply all your recent (from the previous call of this method) changes,
/// including adding new images and deleting images.
/// </summary>
/// <returns>Server response</returns>
public Dictionary<string, string> indexBuild()
{
return sendSoapRequest("", "indexBuild");
}
/// <summary>
/// Add new picture to your pictures list
/// </summary>
/// <param name="imageId">A unique identifier of the inserted image.</param>
/// <param name="imageName">A label you want to assign to the inserted image.</param>
/// <param name="imagePath">Path to the image file.</param>
/// <returns>Server response</returns>
public Dictionary<string, string> imageInsert(string imageId, string imageName, string imagePath)
{
FileStream image = File.OpenRead(imagePath);
byte[] data = new byte[image.Length];
image.Read(data, 0, data.Length);
String imageData = System.Convert.ToBase64String(data);
string oRequest = "";
oRequest = oRequest + "<id xsi:type=\"xsd:string\">" + imageId + "</id>";
oRequest = oRequest + "<name xsi:type=\"xsd:string\">" + imageName + "</name>";
oRequest = oRequest + "<data xsi:type=\"xsd:string\">" + imageData + "</data>";
return sendSoapRequest(oRequest, "imageInsert");
}
/// <summary>
/// There are some situations when we might need to call one of your methods.
/// For example when we finish applying changes we may need to let you know that all your images are ready to be recognized.
/// </summary>
/// <param name="callbackUrl">The URL to the method you want us to call.</param>
/// <returns>Server response</returns>
public Dictionary<string, string> callback(string callbackUrl)
{
string oRequest = "";
oRequest = oRequest + "<callbackURL xsi:type=\"xsd:anyURI\">" + callbackUrl + "</callbackURL>";
return sendSoapRequest(oRequest, "callback");
}
/// <summary>
/// If you don't need an image to be recognizable anymore you have to remove this image from the database.
/// You can do this by calling imageDelete method passing the ID of the image you want to remove.
/// You can also remove all of your images with one call of this method. In order to achieve this you need to pass null value as a parameter.
/// </summary>
/// <param name="imageId">ID of the image you would like to remove (this is the same ID you pass a an argument to the imageInsert method).
/// Pass null value if you want to remove all of your images.</param>
/// <returns>Server response</returns>
public Dictionary<string, string> imageDelete(string imageId)
{
string oRequest = "";
oRequest = oRequest + "<ID xsi:type=\"xsd:string\">" + imageId + "</ID>";
return sendSoapRequest(oRequest, "imageDelete");
}
/// <summary>
/// There may be some situations when you would like to change the name or ID of an image stored in the database.
/// You can do this by calling the imageUpdate method.
/// </summary>
/// <param name="imageIdOld">ID of the image which data you would like to change (this is the same ID you pass a an argument to the imageInsert method).</param>
/// <param name="imageIdNew">New ID of an image.</param>
/// <param name="imageNameNew">New name of an image.</param>
/// <returns>Server response</returns>
public Dictionary<string, string> imageUpdate(string imageIdOld, string imageIdNew, string imageNameNew)
{
string oRequest = "";
oRequest = oRequest + "<ID xsi:type=\"xsd:string\">" + imageIdOld + "</ID>";
oRequest = oRequest + "<data xsi:type=\"ns2:Map\">";
oRequest = oRequest + "<item>";
oRequest = oRequest + "<key xsi:type=\"xsd:string\">id</key>";
oRequest = oRequest + "<value xsi:type=\"xsd:string\">" + imageIdNew + "</value>";
oRequest = oRequest + "</item>";
oRequest = oRequest + "<item>";
oRequest = oRequest + "<key xsi:type=\"xsd:string\">name</key>";
oRequest = oRequest + "<value xsi:type=\"xsd:string\">" + imageNameNew + "</value>";
oRequest = oRequest + "</item>";
oRequest = oRequest + "</data>";
return sendSoapRequest(oRequest, "imageUpdate");
}
/// <summary>
/// You may be curious what is the progress of applying your changes. In order to do this you need to call indexStatus method.
/// </summary>
/// <returns>Server response</returns>
public Dictionary<string, string> indexStatus()
{
return sendSoapRequest("", "indexStatus");
}
/// <summary>
/// When using our API you are limited with regards the number of images and number of scans (recognition operations).
/// The limits depend on the type of account you have.
/// In order to check how many more images you can add and how many scans you have left use the userLimits method.
/// </summary>
/// <returns>Server response</returns>
public Dictionary<string, string> userLimits()
{
return sendSoapRequest("", "userLimits");
}
/// <summary>
/// Gets recognition mode
/// </summary>
/// <returns>Server response</returns>
public Dictionary<string, string> modeGet()
{
return sendSoapRequest("", "modeGet");
}
/// <summary>
/// Changes recognition mode
/// </summary>
/// <returns>Server response</returns>
public Dictionary<string, string> modeChange(RecognitionMode mode)
{
string oRequest = "";
if (mode == RecognitionMode.multi)
{
oRequest = "<mode xsi:type=\"xsd:string\">Multi</mode>";
}
else
{
oRequest = "<mode xsi:type=\"xsd:string\">Single</mode>";
}
return sendSoapRequest(oRequest, "modeChange");
}
/// <summary>
/// Sends image recognition request.
/// </summary>
/// <param name="imagePath">Path to the image file.</param>
/// <param name="mode">Recognize mode</param>
/// <returns>Server response</returns>
public recognitionResponse recognize(string imagePath, RecognitionMode mode, bool all)
{
//open image
FileStream imageStream = File.OpenRead(imagePath);
byte[] data = new byte[imageStream.Length];
imageStream.Read(data, 0, data.Length);
if (!checkImageLimits(imageStream, mode))
{
throw new Exception("Image Limits exception");
}
imageStream.Close();
//create request
string url = "http://recognize.im/v2/recognize/";
if (mode == RecognitionMode.multi)
{
url += "multi/";
}
else
{
url += "single/";
}
if (all)
{
url += "all/";
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + this.clientId);
request.Method = "POST";
request.Headers["x-itraff-hash"] = getMD5(this.apiKey, data);
request.ContentType = "image/jpeg";
request.Accept = "application/json";
request.ContentLength = data.Length;
//send request
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
//get response
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
//read json response
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
String responseString = reader.ReadToEnd();
//deserialize json response into recognitionResponse
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<recognitionResponse>(responseString);
return dict;
}
}
}
/// <summary>
/// Sends image recognition request.
/// </summary>
/// <param name="imagePath">Path to the image file.</param>
/// <returns>Server response</returns>
public recognitionResponse recognize(string imagePath)
{
return recognize(imagePath, RecognitionMode.single, false);
}
/// <summary>
/// Checks the image limits for given recognize mode.
/// </summary>
/// <param name="imageStream">Image file stream.</param>
/// <param name="mode">Recognize mode</param>
/// <returns>Server response</returns>
public bool checkImageLimits(FileStream imageStream, RecognitionMode mode)
{
Image image = Image.FromStream(imageStream);
double imageSurface = (double)(image.Height * image.Width) / 1000000.0;
double fileSize = imageStream.Length / 1000.0;
if (mode == RecognitionMode.single)
{
if (fileSize > SINGLEIR_MAX_FILE_SIZE ||
image.Height < SINGLEIR_MIN_DIMENSION ||
image.Width < SINGLEIR_MIN_DIMENSION ||
imageSurface < SINGLEIR_MIN_IMAGE_SURFACE ||
imageSurface > SINGLEIR_MAX_IMAGE_SURFACE)
{
return false;
}
}
else if (mode == RecognitionMode.multi)
{
if (fileSize > MULTIPLEIR_MAX_FILE_SIZE ||
image.Height < MULTIPLEIR_MIN_DIMENSION ||
image.Width < MULTIPLEIR_MIN_DIMENSION ||
imageSurface < MULTIPLEIR_MIN_IMAGE_SURFACE ||
imageSurface > MULTIPLEIR_MAX_IMAGE_SURFACE)
{
return false;
}
}
return true;
}
}
}