This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathFormRecognizerExplorer.xaml.cs
576 lines (509 loc) · 22.3 KB
/
FormRecognizerExplorer.xaml.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
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using IntelligentKioskSample.Controls.Overlays;
using Newtonsoft.Json;
using ServiceHelpers;
using ServiceHelpers.Models.FormRecognizer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views.FormRecognizer
{
[KioskExperience(Id = "FormRecognizer",
DisplayName = "Form Recognizer",
Description = "Extract tables and key/value pairs from scanned forms",
ImagePath = "ms-appx:/Assets/DemoGallery/Forms Recognizer.jpg",
ExperienceType = ExperienceType.Guided | ExperienceType.Business | ExperienceType.Preview,
TechnologiesUsed = TechnologyType.FormRecognizer,
TechnologyArea = TechnologyAreaType.Vision,
DateAdded = "2019/07/22",
DateUpdated = "2020/06/03",
UpdatedDescription = "Updated to V2.0, including support for new fields in Receipt scenario")]
public sealed partial class FormRecognizerExplorer : Page, INotifyPropertyChanged
{
private FormRecognizerService formRecognizerService;
public static readonly string[] PdfExtensions = new string[] { ".pdf" };
public static readonly string[] ImageExtensions = new string[] { ".jpg", ".jpeg", ".png", ".bmp" };
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public ObservableCollection<FormRecognizerViewModel> Models { get; set; } = new ObservableCollection<FormRecognizerViewModel>();
public ObservableCollection<MultiPageResultViewModel> MultiPageResultCollection { get; set; } = new ObservableCollection<MultiPageResultViewModel>();
private FormRecognizerViewModel currentFormModel;
public FormRecognizerViewModel CurrentFormModel
{
get { return currentFormModel; }
set
{
if (currentFormModel != value)
{
currentFormModel = value;
NotifyPropertyChanged("CurrentFormModel");
CurrentFormModelChanged();
}
}
}
public bool isImageSource = true;
public bool IsImageSource
{
get { return isImageSource; }
set
{
if (isImageSource != value)
{
isImageSource = value;
NotifyPropertyChanged("IsImageSource");
}
}
}
public bool isReceipt = true;
public bool IsReceipt
{
get { return isReceipt; }
set
{
if (isReceipt != value)
{
isReceipt = value;
NotifyPropertyChanged(nameof(IsReceipt));
}
}
}
public FormRecognizerExplorer()
{
this.InitializeComponent();
this.DataContext = this;
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
bool enableFormRecognizerKeys = !string.IsNullOrEmpty(SettingsHelper.Instance.FormRecognizerApiKey) && !string.IsNullOrEmpty(SettingsHelper.Instance.FormRecognizerApiKeyEndpoint);
this.newScenarioButton.IsEnabled = enableFormRecognizerKeys;
if (enableFormRecognizerKeys)
{
this.formRecognizerService = new FormRecognizerService(SettingsHelper.Instance.FormRecognizerApiKey, SettingsHelper.Instance.FormRecognizerApiKeyEndpoint);
await LoadModelsAsync();
}
else
{
await new MessageDialog("Missing Form Recognizer Key. Please enter the key in the Settings page.", "Missing API Key").ShowAsync();
}
base.OnNavigatedTo(e);
}
private async Task LoadModelsAsync()
{
try
{
this.Models.Clear();
this.Models.AddRange(FormRecognizerDataLoader.GetBuiltInModels());
// get all model Ids by custom Form Recognizer API Key
IList<Guid> allModelIdsBySubKey = (await this.formRecognizerService.GetCustomModelsAsync())?.ModelList.Select(x => x.ModelId).ToList() ?? new List<Guid>();
// get stored models from local file by your custom Form Recognizer API Key
List<FormRecognizerViewModel> allCustomModels = await FormRecognizerDataLoader.GetCustomModelsAsync();
List<FormRecognizerViewModel> customModels = allCustomModels?.Where(x => allModelIdsBySubKey.Contains(x.Id)).ToList();
if (customModels != null && customModels.Any())
{
this.Models.AddRange(customModels);
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure loading models");
}
}
private void CurrentFormModelChanged()
{
if (CurrentFormModel != null)
{
this.initialViewControl.IsEnabled = false;
this.suggestionSamplesListView.SelectedIndex = 0;
}
}
private async void OnCameraImageCaptured(object sender, ImageAnalyzer img)
{
this.inputTypeFlyout.Hide();
if (CurrentFormModel.IsReceiptModel)
{
await AnalyzeReceiptAsync(img);
}
else
{
StorageFile imageFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FormImage.jpg", CreationCollisionOption.ReplaceExisting);
if (img?.GetImageStreamCallback != null)
{
await Util.SaveBitmapToStorageFileAsync(await img.GetImageStreamCallback(), imageFile);
}
await AnalyzeFormFromFileAsync(imageFile);
}
}
private async void OnImageSearchCompleted(object sender, IEnumerable<ImageAnalyzer> args)
{
this.inputTypeFlyout.Hide();
if (CurrentFormModel.IsReceiptModel)
{
await AnalyzeReceiptAsync(args.First());
}
}
private async Task AnalyzeLocalFileAsync()
{
if (CurrentFormModel.IsReceiptModel)
{
StorageFile localFile = await Util.PickSingleFileAsync(ImageExtensions);
if (localFile != null)
{
await AnalyzeReceiptAsync(new Uri(localFile.Path), localFile);
}
}
else
{
string[] inputFormExtensions = ImageExtensions.Concat(PdfExtensions).ToArray();
StorageFile localFile = await Util.PickSingleFileAsync(inputFormExtensions);
if (localFile != null)
{
await AnalyzeFormFromFileAsync(localFile);
}
}
}
private async Task AnalyzeReceiptAsync(ImageAnalyzer image)
{
await AnalyzeReceiptAsync(image, null, null);
}
private async Task AnalyzeReceiptAsync(Uri uri, StorageFile file = null)
{
await AnalyzeReceiptAsync(null, uri, file);
}
private async Task AnalyzeReceiptAsync(ImageAnalyzer image = null, Uri uri = null, StorageFile file = null)
{
try
{
IsReceipt = true;
this.progressControl.IsActive = false;
this.MultiPageResultCollection.Clear();
this.OverlayPresenter.TokenInfo = null;
this.notFoundGrid.Visibility = Visibility.Collapsed;
//analyze and show receipt
if (uri != null)
{
await receiptView.AnalyzeReceiptAsync(uri, file);
}
else if (image != null)
{
await receiptView.AnalyzeReceiptAsync(image);
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Receipt Recognizer error.");
}
}
private async Task AnalyzeFormFromFileAsync(StorageFile file)
{
if (file == null)
{
return;
}
try
{
IsReceipt = false;
this.progressControl.IsActive = true;
this.MultiPageResultCollection.Clear();
this.OverlayPresenter.TokenInfo = null;
this.notFoundGrid.Visibility = Visibility.Collapsed;
StorageFile imageFile = await file?.CopyAsync(ApplicationData.Current.TemporaryFolder, $"{Guid.NewGuid().ToString()}{file.FileType}");
if (imageFile != null)
{
IsImageSource = ImageExtensions.Contains(imageFile.FileType);
if (IsImageSource)
{
this.OverlayPresenter.Source = new BitmapImage(new Uri(imageFile.Path));
}
else
{
this.pdfViewerControl.Source = new Uri(imageFile.Path);
}
AnalyzeFormResult formResult = null;
string fileType = IsImageSource ? FormRecognizerService.ImageJpegContentType : FormRecognizerService.PdfContentType;
using (FileStream stream = new FileStream(imageFile.Path, FileMode.Open))
{
formResult = await this.formRecognizerService.AnalyzeImageFormWithCustomModelAsync(CurrentFormModel.Id, stream, fileType);
}
ProcessFormResult(formResult);
imageFile?.DeleteAsync();
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Form Recognizer error.");
}
finally
{
this.progressControl.IsActive = false;
}
}
private void ProcessFormResult(AnalyzeFormResult result)
{
IList<PageResult> pages = result?.PageResults ?? new List<PageResult>();
this.MultiPageResultCollection.Clear();
foreach (var (page, index) in pages.Select((p, i) => (p, i)))
{
var keyValuePairList = page.KeyValuePairs
.Where(x => !string.IsNullOrEmpty(x.Key?.Text) && x.Key?.BoundingBox != null) // showing pairs with non-empty keys
.Select(x => new Tuple<List<TokenOverlayInfo>, List<TokenOverlayInfo>>(
new List<TokenOverlayInfo>() { new TokenOverlayInfo(x.Key) { IsMuted = true } },
new List<TokenOverlayInfo>() { new TokenOverlayInfo(x.Value) { IsMuted = true } }));
var multiPage = new MultiPageResultViewModel()
{
Title = $"Page {index + 1}",
ExtractedKeyValuePairCollection = new ObservableCollection<Tuple<List<TokenOverlayInfo>, List<TokenOverlayInfo>>>(keyValuePairList),
DataTableSourceCollection = new ObservableCollection<DataTable>(ListDataTableResult(page.Tables))
};
this.MultiPageResultCollection.Add(multiPage);
}
string jsonResult = result != null ? JsonConvert.SerializeObject(result, Formatting.Indented) : string.Empty;
this.PreparePageResult(jsonResult);
}
private void PreparePageResult(string jsonResult)
{
if (this.MultiPageResultCollection.Any())
{
// highlight fields just in the image
var mPage = this.MultiPageResultCollection.FirstOrDefault();
if (IsImageSource && mPage != null)
{
this.OverlayPresenter.TokenInfo = mPage.ExtractedKeyValuePairCollection.SelectMany(x => x.Item1.Concat(x.Item2)).ToList();
}
//add json page
this.MultiPageResultCollection.Add(new MultiPageResultJson() { Title = "Json", Json = jsonResult });
}
else
{
this.notFoundGrid.Visibility = Visibility.Visible;
}
}
private async void SuggestionSampleListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.suggestionSamplesListView.SelectedValue is Tuple<string, Uri> selectedSuggestionSample)
{
Uri fileUri = selectedSuggestionSample.Item2;
bool isAppFile = fileUri.Scheme?.Equals("ms-appx", StringComparison.OrdinalIgnoreCase) ?? false;
if (CurrentFormModel.IsReceiptModel)
{
//analyze recipt
if (isAppFile)
{
StorageFile localFile = await StorageFile.GetFileFromApplicationUriAsync(fileUri);
await AnalyzeReceiptAsync(new Uri(localFile.Path), localFile);
}
else
{
await AnalyzeReceiptAsync(fileUri);
}
}
else
{
//analyze form
await AnalyzeFormFromFileAsync(isAppFile ? await StorageFile.GetFileFromApplicationUriAsync(fileUri) : await StorageFile.GetFileFromPathAsync(fileUri.LocalPath));
}
}
}
private async void OnNewFormScenarioOpenButtonClicked(object sender, RoutedEventArgs e)
{
bool enableStorageKeys = !string.IsNullOrEmpty(FormRecognizerScenarioSetup.StorageAccount) && !string.IsNullOrEmpty(FormRecognizerScenarioSetup.StorageKey);
if (enableStorageKeys)
{
this.formRecognizerScenarioSetupControl.OpenScenarioSetupForm(this.formRecognizerService);
}
else
{
await new MessageDialog("Please enter Storage Keys in the code behind of this demo.", "Missing Storage Keys").ShowAsync();
}
}
private async void OnNewModelCreated(object sender, FormRecognizerViewModel e)
{
try
{
this.Models.Add(e);
CurrentFormModel = this.Models.FirstOrDefault(m => m.Id == e.Id);
// update local file with custom models
await FormRecognizerDataLoader.SaveCustomModelsToFileAsync(this.Models.Where(x => !x.IsPrebuiltModel));
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure creating model");
}
}
private async void OnDeleteModelClicked(object sender, RoutedEventArgs e)
{
await Util.ConfirmActionAndExecute("Delete current scenario?", async () => { await DeleteModelAsync(); });
}
private async Task DeleteModelAsync()
{
try
{
if (CurrentFormModel?.Id != null && !CurrentFormModel.IsPrebuiltModel)
{
Guid modelId = CurrentFormModel.Id;
// delete Form Recognizer model
await this.formRecognizerService.DeleteCustomModelAsync(modelId);
// update local file with custom models
this.Models.Remove(CurrentFormModel);
await FormRecognizerDataLoader.SaveCustomModelsToFileAsync(this.Models.Where(x => !x.IsPrebuiltModel));
await FormRecognizerDataLoader.DeleteModelStorageFolderAsync(modelId);
// re-select the first item from collection
CurrentFormModel = this.Models.FirstOrDefault();
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Failure deleting model");
}
}
private List<DataTable> ListDataTableResult(IList<TableResult> tables)
{
var tableList = new List<DataTable>();
foreach (TableResult tableItem in tables)
{
var dataTable = new DataTable();
var columnData = new List<string[]>();
var columns = tableItem.Cells.GroupBy(c => c.ColumnIndex).ToDictionary(c => c.Key, c => c.ToList());
// prepare columns
foreach (var col in columns)
{
var headerText = col.Value.FirstOrDefault(c => c.IsHeader)?.Text ?? string.Empty;
if (dataTable.Columns.Contains(headerText))
{
// we already have a column with this same header, so let's add an unique suffix
int countOfColumnsWithMatchingHeaderPrefix = dataTable.Columns.OfType<DataColumn>().Count(c => c.ColumnName.StartsWith(headerText + "("));
headerText = $"{headerText}({countOfColumnsWithMatchingHeaderPrefix + 1})";
}
dataTable.Columns.Add(headerText, typeof(string));
var columndArr = col.Value.Where(x => !x.IsHeader).Select(x => x.Text).ToArray();
columnData.Add(columndArr);
}
// prepare rows
this.PrepareTableRows(dataTable, columnData);
tableList.Add(dataTable);
}
return tableList;
}
private void PrepareTableRows(DataTable dataTable, List<string[]> columnData)
{
int columnCount = columnData.FirstOrDefault()?.Length ?? 0;
for (int i = 0; i < columnCount; i++)
{
var rowItems = new List<string>();
foreach (string[] col in columnData)
{
rowItems.Add(col[i]);
}
dataTable.Rows.Add(rowItems.ToArray());
}
}
private void OnInputTypeFlyoutOpened(object sender, object e)
{
this.flyoutScrollViewer.Width = this.inputTypeSelector.ActualWidth;
}
private async void OnInputTypeListViewItemTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
string option = ((ListViewItem)sender)?.Tag.ToString() ?? string.Empty;
switch (option.ToLower())
{
case "file":
await AnalyzeLocalFileAsync();
break;
case "camera":
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
break;
case "search":
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
break;
}
this.suggestionSamplesListView.SelectedItem = null;
}
private async void OnCameraFlyoutOpened(object sender, object e)
{
await this.cameraControl.StartStreamAsync();
}
private async void OnCameraFlyoutClosed(object sender, object e)
{
await this.cameraControl.StopStreamAsync();
}
}
public class MultiPageResultViewModel
{
public string Title { get; set; }
public ObservableCollection<DataTable> DataTableSourceCollection { get; set; } = new ObservableCollection<DataTable>();
public ObservableCollection<Tuple<List<TokenOverlayInfo>, List<TokenOverlayInfo>>> ExtractedKeyValuePairCollection { get; set; } = new ObservableCollection<Tuple<List<TokenOverlayInfo>, List<TokenOverlayInfo>>>();
}
public class MultiPageResultJson : MultiPageResultViewModel
{
public string Json { get; set; }
}
public class FileUriToFontGlyphConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
Uri fileUri = (Uri)value;
if (fileUri != null)
{
if (FormRecognizerExplorer.PdfExtensions.Any(x => fileUri.LocalPath.EndsWith(x, StringComparison.OrdinalIgnoreCase)))
{
return "\uEA90";
}
else if (FormRecognizerExplorer.ImageExtensions.Any(x => fileUri.LocalPath.EndsWith(x, StringComparison.OrdinalIgnoreCase)))
{
return "\uEB9F";
}
}
return "\uE8A5";
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}
}