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 pathFormRecognizerScenarioSetup.xaml.cs
270 lines (231 loc) · 10 KB
/
FormRecognizerScenarioSetup.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
//
// 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 Microsoft.Azure.Storage.Blob;
using ServiceHelpers;
using ServiceHelpers.Models.FormRecognizer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace IntelligentKioskSample.Views.FormRecognizer
{
public sealed partial class FormRecognizerScenarioSetup : UserControl, INotifyPropertyChanged
{
public static readonly string StorageAccount = ""; // STORAGE ACCOUNT FOR CREATING CUSTOM FORM RECOGNIZER MODEL
public static readonly string StorageKey = ""; // STORAGE KEY FOR CREATING CUSTOM FORM RECOGNIZER MODEL
private FormRecognizerService formRecognizerService;
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public int MinFilesCount { get; } = 5;
public ObservableCollection<StorageFile> LocalFileCollection { get; private set; } = new ObservableCollection<StorageFile>();
private FormRecognizerScenarioSetupState scenarioSetupState = FormRecognizerScenarioSetupState.NotStarted;
public FormRecognizerScenarioSetupState ScenarioSetupState
{
get { return scenarioSetupState; }
set
{
scenarioSetupState = value;
NotifyPropertyChanged("ScenarioSetupState");
}
}
private string scenarioCreationErrorDetails;
public string ScenarioCreationErrorDetails
{
get { return scenarioCreationErrorDetails; }
set
{
scenarioCreationErrorDetails = value;
NotifyPropertyChanged(nameof(ScenarioCreationErrorDetails));
}
}
public event EventHandler<FormRecognizerViewModel> ModelCreated;
public FormRecognizerScenarioSetup()
{
this.InitializeComponent();
this.DataContext = this;
this.LocalFileCollection.CollectionChanged += LocalFileCollectionChanged;
}
public void OpenScenarioSetupForm(FormRecognizerService formRecognizerService)
{
this.hostGrid.Height = Window.Current.Bounds.Height;
this.hostGrid.Width = Window.Current.Bounds.Width;
this.formRecognizerService = formRecognizerService;
this.newFormScenario.IsOpen = true;
}
private void OnCloseScenarioFormButtonClicked(object sender, RoutedEventArgs e)
{
LocalFileCollection.Clear();
this.scenarioNameTextBox.Text = string.Empty;
ScenarioSetupState = FormRecognizerScenarioSetupState.NotStarted;
this.newFormScenario.IsOpen = false;
}
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
ValidateSetupForm();
}
private void FileListView_OnDragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
}
private async void FileListView_OnDrop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
LocalFileCollection.AddRange(storageItems.Select(x => (StorageFile)x).ToList());
}
}
public void LocalFileCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ValidateSetupForm();
}
private async void OnBrowseFilesButtonClicked(object sender, RoutedEventArgs e)
{
string[] inputFormExtensions = FormRecognizerExplorer.ImageExtensions.Concat(FormRecognizerExplorer.PdfExtensions).ToArray();
IReadOnlyList<StorageFile> selectedFiles = await Util.PickMultipleFilesAsync(inputFormExtensions);
if (selectedFiles != null && selectedFiles.Any())
{
LocalFileCollection.AddRange(selectedFiles);
}
}
private void OnDeleteFileButtonClicked(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
if (button?.DataContext is StorageFile curFile)
{
LocalFileCollection.Remove(curFile);
}
}
private async void OnCreateScenarioButtonClicked(object sender, RoutedEventArgs e)
{
Guid modelId = await CreateNewScenarioAsync();
if (modelId != Guid.Empty)
{
var suggestionFiles = new List<Tuple<string, Uri>>();
foreach (StorageFile file in LocalFileCollection.Take(MinFilesCount))
{
StorageFile copyFile = await FormRecognizerDataLoader.CopyFileToLocalModelFolderAsync(file, modelId);
suggestionFiles.Add(new Tuple<string, Uri>(copyFile.Name, new Uri(copyFile.Path)));
}
this.ModelCreated?.Invoke(this, new FormRecognizerViewModel()
{
Id = modelId,
Name = this.scenarioNameTextBox.Text,
IsPrebuiltModel = false,
SuggestionSamples = suggestionFiles
});
}
}
private async Task<Guid> CreateNewScenarioAsync()
{
CloudBlobContainer formRecognizerCloudContainer = null;
try
{
ScenarioSetupState = FormRecognizerScenarioSetupState.Running;
// create cloud storage container and upload local files
formRecognizerCloudContainer = AzureBlobHelper.GetCloudBlobContainer(StorageAccount, StorageKey, Guid.NewGuid().ToString());
await AzureBlobHelper.UploadStorageFilesToContainerAsync(LocalFileCollection, formRecognizerCloudContainer);
string containerFullSASUrl = AzureBlobHelper.GetContainerSasToken(formRecognizerCloudContainer, sharedAccessStartTimeInMinutes: 5, sharedAccessExpiryTimeInMinutes: 5);
// create and train the custom model
Guid modelId = await this.formRecognizerService.TrainCustomModelAsync(containerFullSASUrl);
ModelResultResponse model = await this.formRecognizerService.GetCustomModelAsync(modelId);
// check creation status
string status = (model?.ModelInfo?.Status ?? string.Empty).ToLower();
switch (status)
{
case "created":
case "ready":
ScenarioSetupState = FormRecognizerScenarioSetupState.Completed;
return modelId;
case "invalid":
default:
ScenarioSetupState = FormRecognizerScenarioSetupState.Error;
break;
}
}
catch (Exception ex)
{
ScenarioCreationErrorDetails = ex.Message;
ScenarioSetupState = FormRecognizerScenarioSetupState.Error;
}
finally
{
if (formRecognizerCloudContainer != null)
{
await formRecognizerCloudContainer.DeleteIfExistsAsync();
}
}
return Guid.Empty;
}
private void OnTryAgainButtonClicked(object sender, RoutedEventArgs e)
{
ScenarioSetupState = FormRecognizerScenarioSetupState.NotStarted;
ValidateSetupForm();
}
private void ValidateSetupForm()
{
bool isValidForm = LocalFileCollection?.Count >= MinFilesCount && !string.IsNullOrEmpty(this.scenarioNameTextBox.Text);
this.scenarioCreateButton.IsEnabled = isValidForm;
}
private void OnPopUpOpened(object sender, object e)
{
Window.Current.SizeChanged += WindowSizeChanged;
}
private void OnPopUpClosed(object sender, object e)
{
Window.Current.SizeChanged -= WindowSizeChanged;
}
private void WindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
this.hostGrid.Height = e.Size.Height;
this.hostGrid.Width = e.Size.Width;
}
}
public enum FormRecognizerScenarioSetupState
{
NotStarted,
Running,
Completed,
Error
}
}