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 575
/
DigitalAssetManagementPage.xaml.cs
463 lines (409 loc) · 17.8 KB
/
DigitalAssetManagementPage.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
//
// 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.CognitiveServices.Vision.CustomVision.Prediction;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models;
using ServiceHelpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Navigation;
namespace IntelligentKioskSample.Views.DigitalAssetManagement
{
[KioskExperience(Id = "DigitalAssetManagement",
DisplayName = "Digital Asset Management",
Description = "See how Computer Vision can add a layer of insights to image collections",
ImagePath = "ms-appx:/Assets/DemoGallery/Image Collection Insights.jpg",
ExperienceType = ExperienceType.Guided | ExperienceType.Business,
TechnologiesUsed = TechnologyType.Face | TechnologyType.Emotion | TechnologyType.Vision,
TechnologyArea = TechnologyAreaType.Vision,
DateAdded = "2019/10/30")]
public sealed partial class DigitalAssetManagementPage : Page, INotifyPropertyChanged
{
const int _maxImageCountPerProcessingCycle = 50;
ImageProcessor _imageProcessor = new ImageProcessor();
DigitalAssetData _currentData;
CustomVisionTrainingClient _customVisionTraining;
CustomVisionPredictionClient _customVisionPrediction;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<ProjectViewModel> CustomVisionProjects { get; set; } = new ObservableCollection<ProjectViewModel>();
public FilesViewModel FileManager { get; } = new FilesViewModel();
public ImageFiltersViewModel ImageFilters { get; } = new ImageFiltersViewModel();
public DigitalAssetManagementPage()
{
this.InitializeComponent();
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (string.IsNullOrEmpty(SettingsHelper.Instance.FaceApiKey) ||
string.IsNullOrEmpty(SettingsHelper.Instance.VisionApiKey))
{
await new MessageDialog("Missing Face or Vision API Key. Please enter a key in the Settings page.", "Missing API Key").ShowAsync();
}
FaceListManager.FaceListsUserDataFilter = SettingsHelper.Instance.WorkspaceKey + "_DigitalAssetManagement";
await FaceListManager.Initialize();
//load files
await FileManager.LoadFilesAsync();
//setup Custom Vision
await InitCustomVision();
base.OnNavigatedTo(e);
}
protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
await FaceListManager.ResetFaceLists();
base.OnNavigatingFrom(e);
}
async Task InitCustomVision()
{
//setup custom vision clients
if (!string.IsNullOrEmpty(SettingsHelper.Instance.CustomVisionTrainingApiKey) &&
!string.IsNullOrEmpty(SettingsHelper.Instance.CustomVisionPredictionApiKey) &&
!string.IsNullOrEmpty(SettingsHelper.Instance.CustomVisionPredictionResourceId))
{
_customVisionTraining = new CustomVisionTrainingClient(new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.ApiKeyServiceClientCredentials(SettingsHelper.Instance.CustomVisionTrainingApiKey))
{
Endpoint = SettingsHelper.Instance.CustomVisionTrainingApiKeyEndpoint
};
_customVisionPrediction = new CustomVisionPredictionClient(new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.ApiKeyServiceClientCredentials(SettingsHelper.Instance.CustomVisionPredictionApiKey))
{
Endpoint = SettingsHelper.Instance.CustomVisionPredictionApiKeyEndpoint
};
}
//get custom vision projects
CustomVisionProjects.Clear();
if (_customVisionTraining != null)
{
var projects = await _customVisionTraining.GetProjectsAsync();
CustomVisionProjects.AddRange(projects.OrderBy(i => i.Name).Select(i => new ProjectViewModel { Project = i }));
}
//enable UI
CustomVisionApi.IsEnabled = _customVisionTraining != null;
}
public DigitalAssetData CurrentData
{
get => _currentData;
set
{
_currentData = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentData)));
}
}
private void StartOverClicked(object sender, RoutedEventArgs e)
{
StartOver();
}
void StartOver()
{
//reset back to loading screen
this.landingMessage.Visibility = Visibility.Visible;
this.filterTab.Visibility = Visibility.Collapsed;
this.ActiveFilters.Visibility = Visibility.Collapsed;
this.ImagesContainer.Visibility = Visibility.Collapsed;
ImageFilters.Clear();
CurrentData = null;
progressRing.IsActive = false;
}
async void LoadTypeClicked(object sender, ItemClickEventArgs e)
{
var tag = (e.ClickedItem as FrameworkElement).Tag;
switch (tag)
{
case "file":
await LoadFromFile();
break;
case "storage":
await LoadFromStorage();
break;
}
}
async Task LoadFromFile()
{
try
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageApplicationPermissions.FutureAccessList.Add(folder);
await LoadImages(new FileSource(new Uri(folder.Path)), _maxImageCountPerProcessingCycle, null);
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Error loading from the target folder.");
}
}
async Task LoadFromStorage()
{
try
{
var dialog = new StorageDialog();
if ((await dialog.ShowAsync()) == ContentDialogResult.Primary)
{
await LoadImages(new StorageSource(new Uri(dialog.SasUri)), _maxImageCountPerProcessingCycle, null);
}
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Error loading from the target storage.");
}
}
async Task LoadImages(ImageProcessorSource source, int? fileLimit, int? startingFileIndex)
{
//prepare UI
this.progressRing.IsActive = true;
this.landingMessage.Visibility = Visibility.Collapsed;
this.filterTab.Visibility = Visibility.Visible;
this.ActiveFilters.Visibility = Visibility.Visible;
this.ImagesContainer.Visibility = Visibility.Visible;
ImageFilters.Clear();
await FaceListManager.ResetFaceLists();
try
{
//convert to view model
var serviceTypes = (FaceApi.IsChecked.GetValueOrDefault() ? ImageProcessorServiceType.Face : 0) |
(ComputerVisionApi.IsChecked.GetValueOrDefault() ? ImageProcessorServiceType.ComputerVision : 0) |
(CustomVisionApi.IsChecked.GetValueOrDefault() ? ImageProcessorServiceType.CustomVision : 0);
var customVisionProjects = CustomVisionProjects.Where(i => i.IsSelected).Select(i => i.Project.Id).ToArray();
var data = await _imageProcessor.ProcessImagesAsync(source, await GetServices(serviceTypes, customVisionProjects), fileLimit, startingFileIndex, async insight =>
{
await ImageFilters.AddImage(insight);
});
CurrentData = data;
ImageFilters.AddImagesCompleted();
//save data
await FileManager.SaveFileAsync(CurrentData);
}
catch
{
StartOver();
throw;
}
finally
{
//finished
this.progressRing.IsActive = false;
}
}
async Task LoadImages(DigitalAssetData data)
{
//prepare UI
this.progressRing.IsActive = true;
this.landingMessage.Visibility = Visibility.Collapsed;
this.filterTab.Visibility = Visibility.Visible;
this.ActiveFilters.Visibility = Visibility.Visible;
this.ImagesContainer.Visibility = Visibility.Visible;
ImageFilters.Clear();
CurrentData = null;
try
{
foreach (var insight in data.Insights)
{
await ImageFilters.AddImage(insight);
}
CurrentData = data;
ImageFilters.AddImagesCompleted();
}
catch (Exception ex)
{
StartOver();
await Util.GenericApiCallExceptionHandler(ex, "Error loading from history.");
}
finally
{
//finished
this.progressRing.IsActive = false;
}
}
async Task LoadMoreImages(int? fileLimit)
{
//validate
var data = CurrentData;
if (data == null)
{
return;
}
//prepare UI
this.progressRing.IsActive = true;
try
{
//create source
var source = data.Info.Source == "StorageSource" ? (ImageProcessorSource)new StorageSource(data.Info.Path) : new FileSource(data.Info.Path);
//convert to view model
var newData = await _imageProcessor.ProcessImagesAsync(source, await GetServices(data.Info.Services, data.Info.CustomVisionProjects), fileLimit, data.Info.LastFileIndex, async insight =>
{
await ImageFilters.AddImage(insight);
});
newData.Insights = data.Insights.Concat(newData.Insights).ToArray();
CurrentData = newData;
ImageFilters.AddImagesCompleted();
//save data
await FileManager.SaveFileAsync(CurrentData);
}
catch
{
StartOver();
throw;
}
finally
{
//finished
this.progressRing.IsActive = false;
}
}
async void History_ItemClick(object sender, ItemClickEventArgs e)
{
(sender as ListViewBase).SelectedItem = null;
//load the data
var data = await FileManager.GetFileData((e.ClickedItem as FileViewModel).File);
if (data == null)
{
await Util.GenericApiCallExceptionHandler(new Exception("failed to load json file"), "Error loading from history.");
}
HistoryFlyout.Hide();
//load the images
await LoadImages(data);
}
async void Reprocess_Click(object sender, RoutedEventArgs e)
{
await LoadMoreImages(_maxImageCountPerProcessingCycle);
}
async void Download_Click(object sender, RoutedEventArgs e)
{
await FileManager.DownloadFileAsync((sender as FrameworkElement).DataContext as FileViewModel);
}
async void Delete_Click(object sender, RoutedEventArgs e)
{
await FileManager.DeleteFileAsync((sender as FrameworkElement).DataContext as FileViewModel);
}
async Task<IImageProcessorService[]> GetServices(ImageProcessorServiceType serviceTypes, Guid[] customVisionProjects)
{
var result = new List<IImageProcessorService>();
if (serviceTypes.HasFlag(ImageProcessorServiceType.Face))
{
result.Add(new FaceProcessorService());
}
if (serviceTypes.HasFlag(ImageProcessorServiceType.ComputerVision))
{
result.Add(new ComputerVisionProcessorService());
}
if (serviceTypes.HasFlag(ImageProcessorServiceType.CustomVision))
{
result.Add(new CustomVisionProcessorService(_customVisionPrediction, await CustomVisionProcessorService.GetProjectIterations(_customVisionTraining, SettingsHelper.Instance.CustomVisionPredictionResourceId, customVisionProjects)));
}
return result.ToArray();
}
private void FilterChanged(object sender, RoutedEventArgs e)
{
ImageFilters.ApplyFilters();
}
private void WordSearch(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if (sender.Tag == null) //flag to ignore updating the autosuggest list
{
System.Diagnostics.Debug.WriteLine(args.CheckCurrent());
//find suggestion for last word
var lastWord = sender.Text.Trim().Split(' ').LastOrDefault() ?? string.Empty;
//pick top 5 words sorting words starting with on top
sender.ItemsSource = ImageFilters.WordFilters
.Where(i => ((string)i.Key).Contains(lastWord, StringComparison.OrdinalIgnoreCase)) //contains word
.OrderBy(i => ((string)i.Key).StartsWith(lastWord, StringComparison.OrdinalIgnoreCase) ? 1 : 2) //put terms starting with word on top
.Select(i => i.Key).Take(5); //top 5
}
else
{
sender.Tag = null; //reset the flag
}
}
}
private void WordSearchQuery(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
ImageFilters.ApplyWordsFilter(sender.Text);
}
private void WordSearchChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
//replace last word only
var words = sender.Text.Trim().Split(' ');
words[words.Length - 1] = args.SelectedItem as string;
sender.Text = string.Join(' ', words);
sender.Tag = true; //flag to ignore updating the autosuggest list
}
private void ShowAllToggle(object sender, RoutedEventArgs e)
{
var filter = (sender as FrameworkElement)?.DataContext as FilterCollection;
if (filter != null)
{
filter.IsShowingAll = !filter.IsShowingAll;
}
}
private void ImagesContainer_ItemClick(object sender, ItemClickEventArgs e)
{
DetailView.DataContext = e.ClickedItem;
FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
}
private void NavigateCustomVisionSetup(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
Frame.Navigate(typeof(CustomVisionSetup));
}
public class ProjectViewModel : INotifyPropertyChanged
{
bool _isSelected;
public event PropertyChangedEventHandler PropertyChanged;
public Project Project { get; set; }
public bool IsSelected
{
get => _isSelected;
set
{
_isSelected = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSelected)));
}
}
}
}
}