-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.txt
1243 lines (1241 loc) · 52.7 KB
/
Events.txt
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using ABBYY.FlexiCapture;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
namespace GlobalWorkflow
{
/// <summary>
/// Main class of the connector that is being used for recognition in Tegaki
/// </summary>
public class Stage
{
/// <summary>
/// To store FlexiCapture document that is being recognized at the moment
/// </summary>
public IDocument Document { get; private set; }
/// <summary>
/// List of constructed fields of the document that needs to be recognized in Tegaki
/// </summary>
public List<FieldForRecognition> Fields { get; private set; }
/// <summary>
/// Contains field names and their edited versions that are passed to Tegaki to be able to understand these names
/// </summary>
private Dictionary<string, string> aliases = new Dictionary<string, string>();
/// <summary>
/// Logger instance
/// </summary>
private Logger logger;
/// <summary>
/// Main constructor
/// </summary>
/// <param name="document">FC document</param>
/// <param name="fieldsForIcr">List of fields to recognize</param>
public Stage(IDocument document, List<FieldForRecognition> fieldsForIcr)
{
Document = document;
Fields = fieldsForIcr;
logger = new Logger();
}
/// <summary>
/// Main method for sending the fields to Tegaki, writing results to fields then saving the fields settings to document properties
/// </summary>
public void Execute()
{
try
{
// Check if any doc def is matched
if (Document.DefinitionName == string.Empty)
throw new FieldAccessException("文書定義が適合しません。");
logger.Write("FlexiCapture has started communication with Tegaki.");
logger.Write("Preparing fields for Tegaki processing...");
RemoveNonExistentOrEmptyFields();
if (Fields.Count == 0)
throw new FieldAccessException("ICRのためのフィールドは存在しません。");
logger.Write(Fields.Count + " fields to recognize by Tegaki in total.");
//Map the field names
AssignAliases();
//===================
int count = 0;
int step = 100;
while (true)
{
var fields = Fields.Skip(count).Take(step).ToList();
logger.Write("Processing " + fields.Count() + " fields. " + (Fields.Count - fields.Count() - count) + " fields remain...");
var response = GetResponseFromProcessingOfFields(fields, aliases);
logger.Write("Setting received values to the document fields...");
FillDocumentFields(fields, response);
count += step;
if (count >= Fields.Count)
break;
}
logger.Write("Compacting received fields recognition settings...");
RemoveDuplicateSettings();
logger.Write("Saving settings...");
//saving the recognition settings to document properties
//to be used in the recognition scripts on verification stages
//when user changes the region of the field
Document.Properties.Set(Constants.IcrSettingsProperty,
JsonConvert.SerializeObject(Fields,
new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }));
logger.Write("Final document rules validataion...");
//if there will be some validation rules we need to execute them
// rules are not checked by default
Document.CheckRules();
logger.Write("Tegaki API processing ended successfully.");
}
catch (FieldAccessException e)
{
FCTools.ShowMessage(e.Message);
}
catch (Exception e)
{
FCTools.ShowMessage("ICR エラー: " + e.Message, true);
}
}
/// <summary>
/// Assigns aliases for fields that are passed for recognition in Tegaki
/// </summary>
private void AssignAliases()
{
for (int n = 0; n < Fields.Count; n++)
aliases.Add(Fields[n].FullName, "Field" + (n + 1));
}
/// <summary>
/// Removes duplicate settings for fields that have same recognition settings so the same ones are not stored in document properties
/// </summary>
private void RemoveDuplicateSettings()
{
string fullName = null;
string correctedFullName = null;
List<string> metFullNames = new List<string>();
for (int n = 0; n < Fields.Count; n++)
{
fullName = Fields[n].FullName;
while (fullName.Contains('[') && fullName.Contains(']'))
fullName = fullName.Remove(fullName.IndexOf('['), fullName.IndexOf(']') - fullName.IndexOf('[') + 1);
correctedFullName = fullName;
if (metFullNames.Contains(correctedFullName))
{
Fields.Remove(Fields[n]);
n--;
}
else
{
metFullNames.Add(correctedFullName);
Fields[n].FullName = correctedFullName;
}
}
}
/// <summary>
/// Fills in fields of processed document with content of Tegaki response
/// </summary>
/// <param name="fields">List of fields that are to be filled with results</param>
/// <param name="response">Response received from Tegaki</param>
private void FillDocumentFields(List<FieldForRecognition> fields, GetResponse response)
{
foreach (var field in fields)
{
var recognitionResult = GetCorrespondingResult(response, field);
var characters = new List<GetResponseCharacter>();
var value = string.Empty;
switch (field.Type)
{
case FieldType.Text:
if (recognitionResult.singleLine != null)
{
value = recognitionResult.singleLine.text;
characters = recognitionResult.singleLine.characters;
}
break;
case FieldType.Boxed:
if (recognitionResult.boxedCharacters != null)
{
value = recognitionResult.boxedCharacters.text;
characters = recognitionResult.boxedCharacters.characters;
}
break;
case FieldType.Multiline:
if (recognitionResult.multiLine != null)
{
value = string.Join("\n", recognitionResult.multiLine.lines.Select(item => item.text)) ?? string.Empty;
characters = recognitionResult.multiLine.lines.SelectMany(item => item.characters).ToList();
}
break;
default:
throw new Exception("フィールドタイプはサポートされていません。");
}
Document.Field(field.FullName).Text = value;
SetCharacterConfidence(field, characters);
}
}
/// <summary>
/// Highlights the characters red in case the confidence is less than threshold where threshold for fields set in FlexiCapture stage script
/// </summary>
/// <param name="field">Field to highlight the characters in</param>
/// <param name="characters">List of characters for the field returned from Tegaki</param>
private void SetCharacterConfidence(FieldForRecognition field, List<GetResponseCharacter> characters)
{
if (characters == null)
return;
Document.Field(field.FullName).NeedVerification = false;
int delta = 0;
for (int i = 0; i < characters.Count; i++)
{
if (Document.Field(field.FullName).Symbols[i + delta].Symbol == "\n")
delta++;
if (characters[i].confidence < field.Confidence)
Document.Field(field.FullName).Symbols[i + delta].NeedVerification = true;
}
}
/// <summary>
/// Gets the recognition result for one field from the response from Tegaki
/// </summary>
/// <param name="response">Tegaki response</param>
/// <param name="field">Constructed field</param>
/// <returns>Recognition result for a field</returns>
private GetResponseResult GetCorrespondingResult(GetResponse response, FieldForRecognition field)
{
var result = response.results.SingleOrDefault(x => x.name == aliases[field.FullName]);
if (result == null)
throw new Exception("内部エラー: 不正な認識結果。");
return result;
}
/// <summary>
/// Recognizes fields in Tegaki and returns the recognition results
/// </summary>
/// <param name="fields">List of fields to be recognized in Tegaki</param>
/// <param name="aliases">Dictionary with field names and their aliases</param>
/// <returns>Tegaki response</returns>
private GetResponse GetResponseFromProcessingOfFields(List<FieldForRecognition> fields, Dictionary<string, string> aliases)
{
var provider = new TegakiFcProvider();
logger.Write("Creating request for " + fields.Count + " fields recognition...");
var request = new PostRequest();
try
{
provider.ProcessFields(fields, ref request, aliases);
}
catch (Exception e)
{
FCTools.ShowMessage("ICR プロセシングエラー: " + e.Message);
}
logger.Write("FlexiCapture is calling Tegaki API...");
logger.Write("Sending request with " + fields.Count + " fields recognition...");
// Send request to Tegaki and fill all fields
var response = provider.GetRecognitionResults(request.ToString());
return response;
}
/// <summary>
/// Removes non-existent or not found fields and logs warnings in the processing log if that happens
/// </summary>
private void RemoveNonExistentOrEmptyFields()
{
for (int index = 0; index < Fields.Count; index++)
{
if (!Document.HasField(Fields[index].FullName))
{
FCTools.ShowMessage(Fields[index].FullName + "という名称のフィールドは見つかりませんでした。 設定をご確認ください。");
Fields.Remove(Fields[index]);
index--;
continue;
}
if (Document.Field(Fields[index].FullName).Regions.Count == 0)
{
FCTools.ShowMessage("フィールド '" + Fields[index].FullName + "' はregionが設定されていません。");
Fields.Remove(Fields[index]);
index--;
continue;
}
}
}
}
/// <summary>
/// Logger that serves for extended logging of recognition process
/// </summary>
class Logger
{
/// <summary>
/// Key that indicates is extended logging enabled
/// </summary>
private string logEnabled;
/// <summary>
/// Main constructor
/// </summary>
public Logger()
{
if (FCTools.ScriptContext.Project.EnvironmentVariables.Has("ExtendedLogWriting"))
logEnabled = FCTools.ScriptContext.Project.EnvironmentVariables.Get("ExtendedLogWriting");
}
/// <summary>
/// Writes a message to processing log if enabled
/// </summary>
/// <param name="message">Message to log</param>
public void Write(string message)
{
if (logEnabled == "Enable")
FCTools.ShowMessage(message);
}
}
/// <summary>
/// Field types according to Tegaki API documentation
/// </summary>
public enum FieldType
{
Text,
Boxed,
Multiline
};
/// <summary>
/// Constructed field with data required for processing
/// </summary>
public class FieldForRecognition
{
/// <summary>
/// Full name of the field in document definition
/// </summary>
[JsonProperty("Name")]
public string FullName { get; set; }
/// <summary>
/// Short field name DEPRECATED
/// </summary>
[JsonIgnore]
public string Name { get; private set; }
/// <summary>
/// Characters with confidence below this value will be highlighlighted on document data form in FlexiCapture
/// </summary>
public double Confidence { get; set; }
/// <summary>
/// Field type according to Tegaki API documentation
/// </summary>
public FieldType Type { get; private set; }
/// <summary>
/// Constructed field recognition settings
/// </summary>
public RecognitionSettings Settings { get; internal set; }
/// <summary>
/// Jpg or png image in base64 format
/// </summary>
[JsonIgnore]
public string ImageData { get; set; }
/// <summary>
/// Serializer for sending to tegaki
/// </summary>
/// <param name="name">Short field name that does not contain '\'</param>
/// <param name="type">Field type</param>
/// <param name="settings">Recognition settings</param>
/// <param name="confidence">Confidence level specified in range from 0 to 1 in percent</param>
/// <param name="imageData">Image in base64 string</param>
[JsonConstructor]
internal FieldForRecognition(string name, FieldType type, RecognitionSettings settings, double confidence,
string imageData)
{
FullName = name;
Name = name;
Type = type;
Settings = settings;
Confidence = confidence;
ImageData = imageData;
}
/// <summary>
/// Constructor that is used in FC on ICRRecognition stage in case of not using FormOut
/// </summary>
/// <param name="field">FC field</param>
/// <param name="type">Field type</param>
/// <param name="settings">Recognition settings</param>
/// <param name="confidence">Confidence level specified in range from 0 to 1 in percent</param>
public FieldForRecognition(IField field, FieldType type, RecognitionSettings settings, double confidence)
{
FullName = field.FullName;
Name = field.Name;
Type = type;
Settings = settings;
Confidence = confidence;
if (field.Regions.Count != 0)
ImageData = GetFieldRegionImage(field.Regions[0].Picture.CreateEditableCopy());
}
/// <summary>
/// Converts an image of field region to a string
/// </summary>
/// <param name="pic">IEditablePictureObject instance from FC</param>
/// <returns>base64 string of the image</returns>
private string GetFieldRegionImage(IEditablePictureObject pic)
{
var bitmap = Image.FromHbitmap(new IntPtr(pic.Handle));
return GetFieldRegionImage(bitmap);
}
/// <summary>
/// Converts a bitmap to base64 string
/// </summary>
/// <param name="bitmap">Picture</param>
/// <returns>base64 string of the bitmap</returns>
public static string GetFieldRegionImage(Bitmap bitmap)
{
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return Convert.ToBase64String(memoryStream.ToArray());
}
}
/// <summary>
/// Character types of symbols in the constructed field
/// </summary>
public enum CharacterType
{
Latin,
Japan,
All,
DigitsAndLatin,
DigitsAndJapan,
Digits
};
/// <summary>
/// Constructed field recognition settings
/// </summary>
public class RecognitionSettings
{
/// <summary>
/// Use hiragana
/// </summary>
public bool UseHiragana { get; protected set; }
/// <summary>
/// Use katakana
/// </summary>
public bool UseKatakana { get; protected set; }
/// <summary>
/// Use kanji
/// </summary>
public bool UseKanji { get; protected set; }
/// <summary>
/// Use digits
/// </summary>
public bool UseDigits { get; protected set; }
/// <summary>
/// Use upper case latin
/// </summary>
public bool UseUpperCaseLatin { get; protected set; }
/// <summary>
/// Use lower case latin
/// </summary>
public bool UseLowerCaseLatin { get; protected set; }
/// <summary>
/// Use punctuation
/// </summary>
public bool UsePunctuation { get; protected set; }
/// <summary>
/// Number of boxes if boxed field
/// Note that after the subtraction of the form the boxes disappear
/// that is why the setter is public
/// </summary>
public int BoxesNumber { get; set; }
/// <summary>
/// Margin
/// </summary>
public int Margin { get; protected set; }
/// <summary>
/// Serialization result is sent to Tegaki
/// </summary>
/// <param name="Hiragana">Use Hiragana</param>
/// <param name="Katakana">Use Katakana</param>
/// <param name="Kanji">Use Kanji</param>
/// <param name="Digits">UseDigits</param>
/// <param name="UpperCaseLatin">Use UpperCaseLatin</param>
/// <param name="LowerCaseLatin">Use LowerCaseLatin</param>
/// <param name="Punctuation">Use Punctuation</param>
/// <param name="BoxesNumber">Number of boxes</param>
/// <param name="Margin">Margin</param>
[JsonConstructor]
public RecognitionSettings(bool Hiragana = false, bool Katakana = false, bool Kanji = false,
bool Digits = false, bool UpperCaseLatin = false, bool LowerCaseLatin = false,
bool Punctuation = false, int BoxesNumber = 0, int Margin = 0)
{
bool useAll = !Hiragana
&& !Katakana
&& !Kanji
&& !Digits
&& !UpperCaseLatin
&& !LowerCaseLatin
&& !Punctuation;
UseHiragana = useAll || Hiragana;
UseKatakana = useAll || Katakana;
UseKanji = useAll || Kanji;
UseDigits = useAll || Digits;
UseUpperCaseLatin = useAll || UpperCaseLatin;
UseLowerCaseLatin = useAll || LowerCaseLatin;
UsePunctuation = useAll || Punctuation;
this.BoxesNumber = BoxesNumber;
this.Margin = Margin;
}
/// <summary>
/// Constructor that is used in FlexiCapture scripts
/// </summary>
/// <param name="type">Character type</param>
/// <param name="BoxesNumber">Number of boxes</param>
/// <param name="Margin">Margin</param>
public RecognitionSettings(CharacterType type, int BoxesNumber = 0, int Margin = 0)
{
switch (type)
{
case CharacterType.Digits:
UseDigits = true;
UsePunctuation = true;
break;
case CharacterType.Latin:
UseUpperCaseLatin = true;
UseLowerCaseLatin = true;
break;
case CharacterType.Japan:
UseHiragana = true;
UseKatakana = true;
UseKanji = true;
break;
case CharacterType.DigitsAndJapan:
UseHiragana = true;
UseKatakana = true;
UseKanji = true;
UseDigits = true;
break;
case CharacterType.DigitsAndLatin:
UseUpperCaseLatin = true;
UseLowerCaseLatin = true;
UseDigits = true;
break;
case CharacterType.All:
UsePunctuation = true;
UseDigits = true;
UseUpperCaseLatin = true;
UseLowerCaseLatin = true;
UseHiragana = true;
UseKatakana = true;
UseKanji = true;
break;
default:
System.Diagnostics.Trace.Assert(false);
break;
}
this.BoxesNumber = BoxesNumber;
this.Margin = Margin;
}
}
/// <summary>
/// Constants for environment variable names and other for convenient use in scripts
/// </summary>
public class Constants
{
/// <summary>
/// Name of the environmental variable that contains Tegaki API key
/// </summary>
public const string ApiKey = "TegakiApiKey";
/// <summary>
/// Name of the environmental variable that contains Tegaki API URI
/// </summary>
public const string ApiUri = "TegakiBaseApiUri";
/// <summary>
/// Name of the environmental variable that enables or disables performing of deletion of recognition data from Tegaki server
/// </summary>
public const string ApiDeleteRecognitionData = "TegakiDeleteRecognitionDataFromServer";
/// <summary>
/// Name of the registration parameter of a document where the recognition settings saved
/// </summary>
public const string IcrSettingsProperty = "TegakiSettings";
}
/// <summary>
/// Provider class between FlexiCapture project and Tegaki API
/// This class provides tools for recognizing regions of fields. You must specify the following values in environment variables:
/// "TegakiApiKey", "TegakiBaseApiUri" and "TegakiDeleteRecognitionDataFromServer".</summary>
public class TegakiFcProvider
{
/// <summary>
/// Tegaki connector instance
/// </summary>
public TegakiConnector TegakiProvider { get; private set; }
/// <summary>
/// Stores marker that indicates should recognition data be deleted from Tegaki server
/// </summary>
private string deleteRecognitionData = string.Empty;
/// <summary>
/// Logger instance
/// </summary>
private Logger logger;
/// <summary>
/// Main constructor
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public TegakiFcProvider()
{
if (!FCTools.ScriptContext.Project.EnvironmentVariables.Has(Constants.ApiKey)
|| string.IsNullOrEmpty(FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiKey)))
throw new Exception(Constants.ApiKey + "のための環境を設定する必要があります。");
if (!FCTools.ScriptContext.Project.EnvironmentVariables.Has(Constants.ApiUri)
|| string.IsNullOrEmpty(FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiUri)))
throw new Exception(Constants.ApiUri + " のための環境を設定する必要があります。");
if (!FCTools.ScriptContext.Project.EnvironmentVariables.Has(Constants.ApiDeleteRecognitionData)
|| string.IsNullOrEmpty(FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiDeleteRecognitionData))
|| (FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiDeleteRecognitionData) != "Enable"
&& FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiDeleteRecognitionData) != "Disable"))
throw new Exception(Constants.ApiDeleteRecognitionData + " のための環境を設定する必要があります。");
else
deleteRecognitionData = FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiDeleteRecognitionData);
TegakiProvider = new TegakiConnector(
FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiKey),
FCTools.ScriptContext.Project.EnvironmentVariables.Get(Constants.ApiUri));
logger = new Logger();
}
/// <summary>
/// Main method that creates the request to tegaki from the list of fields
/// </summary>
/// <param name="fieldsForIcr">List of fields for recognition</param>
/// <param name="request">Post request formed according to Tegaki specification</param>
/// <param name="aliases">Dictionary that contains field names mapping</param>
/// <returns>True if at least one field was added</returns>
public bool ProcessFields(List<FieldForRecognition> fieldsForIcr, ref PostRequest request, Dictionary<string, string> aliases)
{
var fieldAdded = false;
foreach (var fieldForIcr in fieldsForIcr)
{
try
{
var field = new PostRequestField()
{
name = aliases[fieldForIcr.FullName]
};
switch (fieldForIcr.Type)
{
case FieldType.Text:
field.singleLine = CreateSingleLine(fieldForIcr);
break;
case FieldType.Multiline:
field.multiLine = CreateMultiLine(fieldForIcr);
break;
case FieldType.Boxed:
field.boxedCharacters = CreateBoxedCharacters(fieldForIcr);
break;
default:
throw new Exception("フィールドタイプはサポートされていません。");
}
request.fields.Add(field);
fieldAdded = true;
}
catch
{
//in case of model inaccuracies
continue;
}
}
return fieldAdded;
}
/// <summary>
/// Sends a request to Tegaki, returns the response and delete recognition data from Tegaki server
/// </summary>
/// <param name="request">Request string</param>
/// <returns>Response as GetResponse</returns>
public GetResponse GetRecognitionResults(string request)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var requestId = TegakiProvider.SendPostRequest(request);
// Trying to get response while it will not be received
GetResponse response;
logger.Write("Waiting for response...");
int counter = 0;
do
{
if (counter++ > 50)
throw new TimeoutException("Tegaki APIからのレスポンスを得ることができません。");
Thread.Sleep(1000);
} while (!TegakiProvider.GetRecognitionResult(requestId, out response));
logger.Write("Tegaki returned " + response.results.Count + " results.");
//Delete recognition data from server
if (deleteRecognitionData == "Enable")
{
logger.Write("Deleting request data from Tegaki server...");
TegakiProvider.DeleteRecognitionResults(requestId);
}
return response;
}
/// <summary>
/// Creates single line object for post request for constructed field
/// </summary>
/// <param name="fieldForIcr">Constructed field to create PostRequestSingleLine with</param>
/// <returns>PostRequestSingleLine instance</returns>
private PostRequestSingleLine CreateSingleLine(FieldForRecognition fieldForIcr)
{
var newCharacterType = new PostRequestCharacterType()
{
digits = fieldForIcr.Settings.UseDigits,
hiragana = fieldForIcr.Settings.UseHiragana,
kanji = fieldForIcr.Settings.UseKanji,
katakana = fieldForIcr.Settings.UseKatakana,
lowerCaseLatin = fieldForIcr.Settings.UseLowerCaseLatin,
upperCaseLatin = fieldForIcr.Settings.UseUpperCaseLatin,
punctuation = fieldForIcr.Settings.UsePunctuation,
};
return new PostRequestSingleLine()
{
characterType = newCharacterType,
configuration = new PostRequestConfiguration() { languageModel = DoesLanguageModelExists(newCharacterType) },
imageData = fieldForIcr.ImageData
};
}
/// <summary>
/// Creates multiline object for post request for constructed field
/// </summary>
/// <param name="fieldForIcr">Constructed field to create PostRequestMultiLine with</param>
/// <returns>PostRequestMultiLine instance</returns>
private PostRequestMultiLine CreateMultiLine(FieldForRecognition fieldForIcr)
{
var newCharacterType = new PostRequestCharacterType()
{
digits = fieldForIcr.Settings.UseDigits,
hiragana = fieldForIcr.Settings.UseHiragana,
kanji = fieldForIcr.Settings.UseKanji,
katakana = fieldForIcr.Settings.UseKatakana,
lowerCaseLatin = fieldForIcr.Settings.UseLowerCaseLatin,
upperCaseLatin = fieldForIcr.Settings.UseUpperCaseLatin,
punctuation = fieldForIcr.Settings.UsePunctuation,
};
return new PostRequestMultiLine()
{
characterType = newCharacterType,
configuration = new PostRequestConfiguration() { languageModel = DoesLanguageModelExists(newCharacterType) },
imageData = fieldForIcr.ImageData
};
}
/// <summary>
/// Creates boxed characters object for post request for constructed field
/// </summary>
/// <param name="fieldForIcr">Constructed field to create PostRequestBoxedCharacters with</param>
/// <returns>PostRequestBoxedCharacters instance</returns>
private PostRequestBoxedCharacters CreateBoxedCharacters(FieldForRecognition fieldForIcr)
{
return new PostRequestBoxedCharacters()
{
characterType = new PostRequestCharacterType()
{
digits = fieldForIcr.Settings.UseDigits,
hiragana = fieldForIcr.Settings.UseHiragana,
kanji = fieldForIcr.Settings.UseKanji,
katakana = fieldForIcr.Settings.UseKatakana,
lowerCaseLatin = fieldForIcr.Settings.UseLowerCaseLatin,
upperCaseLatin = fieldForIcr.Settings.UseUpperCaseLatin,
punctuation = fieldForIcr.Settings.UsePunctuation,
},
marginBetweenBoxes = fieldForIcr.Settings.Margin,
numberOfBoxes = fieldForIcr.Settings.BoxesNumber,
imageData = fieldForIcr.ImageData
};
}
/// <summary>
/// According to Tegaki documentation the language model exists only for these options of character types
/// </summary>
/// <param name="characterType">Character type (see model)</param>
/// <returns>True if language model exists</returns>
private bool DoesLanguageModelExists(PostRequestCharacterType characterType)
{
return characterType.hiragana
|| characterType.kanji
|| characterType.lowerCaseLatin
|| characterType.upperCaseLatin;
}
}
/// <summary>
/// Tegaki connector itself (communication with Tegaki)
/// </summary>
public class TegakiConnector
{
/// <summary>
/// Recieved from Cogent Labs
/// </summary>
private string _apiKey;
/// <summary>
/// By default this is https://api.tegaki.ai/hwr/v2/
/// </summary>
private string _apiUri;
/// <summary>
/// Main constructor
/// </summary>
/// <param name="apiKey">Can be recieved from Cogent Labs, guid-like string</param>
/// <param name="apiUri">by default https://api.tegaki.ai/hwr/v2/ </param>
public TegakiConnector(string apiKey, string apiUri)
{
_apiKey = apiKey;
_apiUri = apiUri;
}
/// <summary>
/// Returns requestId that used for getting recognition results
/// </summary>
/// <param name="content">Request content as string</param>
/// <returns>Response as string</returns>
public string SendPostRequest(string content)
{
var request = (HttpWebRequest)WebRequest.Create(_apiUri + "field/");
#region Request settings
request.Method = "POST";
request.Headers["Authorization"] = "apikey " + _apiKey;
request.ContentType = "application/json";
var byteArray = Encoding.UTF8.GetBytes(content);
request.ContentLength = byteArray.Length;
var newStream = request.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
#endregion Request settings
using (var response = (HttpWebResponse)request.GetResponse())
{
var encoding = Encoding.GetEncoding(response.CharacterSet);
using (var responseStream = response.GetResponseStream())
using (var reader = new StreamReader(responseStream, encoding))
return JsonConvert.DeserializeObject<PostResponse>(reader.ReadToEnd()).requestId;
}
}
/// <summary>
/// Gets recognition results for specified requestId if completed
/// </summary>
/// <param name="requestId">requestId string returned by SendPostRequest</param>
/// <param name="responce">Result of the recognition</param>
/// <returns>True if recognition was successful</returns>
public bool GetRecognitionResult(string requestId, out GetResponse responce)
{
responce = null;
var request = (HttpWebRequest)WebRequest.Create(_apiUri + "request/" + requestId);
#region Request settings
request.Method = "GET";
request.Headers["Authorization"] = "apikey " + _apiKey;
#endregion Request settings
using (var response = (HttpWebResponse)request.GetResponse())
{
var encoding = Encoding.GetEncoding(response.CharacterSet);
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream, encoding))
{
var getResponse = JsonConvert.DeserializeObject<GetResponse>(reader.ReadToEnd());
if (getResponse.state != "COMPLETED")
return false;
responce = getResponse;
return true;
}
}
}
}
/// <summary>
/// Deletes recognition results from Tegaki server
/// </summary>
/// <param name="requestId">requestId string returned by SendPostRequest</param>
public void DeleteRecognitionResults(string requestId)
{
var request = (HttpWebRequest)WebRequest.Create(_apiUri + "request/" + requestId);
#region Request settings
request.Method = "DELETE";
request.Headers["Authorization"] = "apikey " + _apiKey;
#endregion Request settings
using (var response = (HttpWebResponse)request.GetResponse())
;
}
}
/// <summary>
/// To construct a boxed field
/// </summary>
public class BoxedField : FieldForRecognition
{
/// <summary>
/// Is used for serializing sending data to Tegaki
/// </summary>
/// <param name="name">Short field name that does not contain '\'</param>
/// <param name="settings">Tegaki settings, see model</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
/// <param name="imageData">Image in base64 string</param>
[JsonConstructor]
public BoxedField(string name, RecognitionSettings settings, double confidence, string imageData) :
base(name, FieldType.Boxed, settings, confidence, imageData)
{
}
/// <summary>
/// Constructor that is used in FlexiCapture on ICRRegognition stage script
/// </summary>
/// <param name="field">FC field</param>
/// <param name="settings">Tegaki recognition settings</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
public BoxedField(IField field, RecognitionSettings settings, double confidence) : base(field, FieldType.Boxed, settings, confidence)
{
if (settings.BoxesNumber == 0)
throw new Exception("FieldForRecognitionとその認識設定のタイプは、 " + field.FullName + "とマッチしません。");
}
}
/// <summary>
/// To construct a single line field
/// </summary>
public class TextField : FieldForRecognition
{
/// <summary>
/// Serialization used for sending to Tegaki. See Tegaki AI specification
/// </summary>
/// <param name="fullName">Field full name</param>
/// <param name="settings">Tegaki settings, see model</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
/// <param name="imageData">Image in base64 string</param>
[JsonConstructor]
public TextField(string fullName, RecognitionSettings settings, double confidence, string imageData) :
base(fullName, FieldType.Text, settings, confidence, imageData)
{
}
/// <summary>
/// Constructor that is ised in flexiCapture on ICR Recognition stage script
/// </summary>
/// <param name="field">FC field</param>
/// <param name="settings">Tegaki settings, see model</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
public TextField(IField field, RecognitionSettings settings, double confidence) : base(field, FieldType.Text, settings, confidence)
{
if (settings.BoxesNumber != 0)
throw new Exception("The type for FieldForRecognition and the recognition settings don't match " + field.FullName);
}
}
/// <summary>
/// To construct a multiline field
/// </summary>
public class MultiLineField : FieldForRecognition
{
/// <summary>
/// Serialization used for sending to Tegaki (see Tegaki AI specification)
/// </summary>
/// <param name="fullName">Full field name</param>
/// <param name="settings">Tegaki settings, see model</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
/// <param name="imageData">Image in base64 string</param>
[JsonConstructor]
public MultiLineField(string fullName, RecognitionSettings settings, double confidence, string imageData) :
base(fullName, FieldType.Multiline, settings, confidence, imageData)
{
}
/// <summary>
/// Constructor that is used in FlexiCapture on ICRRecognition stage script
/// </summary>
/// <param name="field">FC field</param>
/// <param name="settings">Tegaki settings, see model</param>
/// <param name="confidence">From 0 to 1 in percentages</param>
public MultiLineField(IField field, RecognitionSettings settings, double confidence) : base(field, FieldType.Multiline, settings, confidence)
{
if (settings.BoxesNumber != 0)
{
throw new Exception("The type for FieldForRecognition and the recognition settings don't match " + field.FullName);
}
}
}
#region Serialization model
/// <summary>
/// Main model class to deserialize content of GET request response
/// </summary>
public class GetResponse
{
/// <summary>
/// Unique requestId generated by the API service
/// </summary>
public string requestId { get; set; }
/// <summary>
/// Unique identifier for a user
/// </summary>
public string userId { get; set; }
/// <summary>
/// API version
/// </summary>
public string version { get; set; }
/// <summary>
/// Enum: "RUNNING", "COMPLETED" or "FAILED" string indicating the current state of a accepted request
/// </summary>
public string state { get; set; }
/// <summary>
/// Timestamp the request was accepted and created
/// </summary>
public DateTime created { get; set; }
/// <summary>
/// Timestamp last time the request got updated
/// </summary>
public DateTime modified { get; set; }
/// <summary>
/// List of fields with results returned from Tegaki
/// </summary>
public List<GetResponseResult> results { get; set; }
}
/// <summary>
/// Returned field content is deserialized into this
/// </summary>
public class GetResponseResult
{
/// <summary>
/// Unique identifier for a single field generated by the API service
/// </summary>
public string fieldId { get; set; }
/// <summary>
/// Field name
/// </summary>
public string name { get; set; }
/// <summary>
/// If a field is of single line type a returned field is deserialized into this property
/// </summary>
public GetResponseSingleLine singleLine { get; set; }
/// <summary>
/// If a field is of multiline type a returned field is deserialized into this property
/// </summary>
public GetResponseMultiLine multiLine { get; set; }
/// <summary>
/// If a field is of boxed type a returned field is deserialized into this property
/// </summary>
public GetResponseBoxedCharacters boxedCharacters { get; set; }
}
/// <summary>
/// If a field is of single line type returned field content is deserialized into this
/// </summary>
public class GetResponseSingleLine
{
/// <summary>
/// A number that represents a probability
/// </summary>
public double confidence { get; set; }
/// <summary>