forked from microsoft/Cognitive-Samples-IntelligentKiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualAlertBuilderWizardControl.xaml.cs
269 lines (234 loc) · 11.3 KB
/
VisualAlertBuilderWizardControl.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
//
// 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 ServiceHelpers;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
namespace IntelligentKioskSample.Views.VisualAlert
{
public sealed partial class VisualAlertBuilderWizardControl : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int MinPositiveImageCount { get; } = 5;
public int MinNegativeImageCount { get; } = 5;
public ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>> PositiveSubjectImageCollection { get; set; } = new ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>>();
public ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>> SelectedPositiveSubjectImageCollection { get; set; } = new ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>>();
public ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>> NegativeSubjectImageCollection { get; set; } = new ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>>();
public ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>> SelectedNegativeSubjectImageCollection { get; set; } = new ObservableCollection<Tuple<BitmapImage, ImageAnalyzer>>();
private string subjectName = string.Empty;
public string SubjectName
{
get { return subjectName; }
set
{
subjectName = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SubjectName"));
}
}
private VisualAlertBuilderStepType builderStepType;
public VisualAlertBuilderStepType BuilderStepType
{
get { return builderStepType; }
set
{
builderStepType = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("BuilderStepType"));
UpdateWizardView();
}
}
public event EventHandler<VisualAlertModelData> WizardCompleted;
public event EventHandler<Tuple<VisualAlertBuilderStepType, VisualAlertModelData>> WizardStepChanged;
public VisualAlertBuilderWizardControl()
{
this.InitializeComponent();
DataContext = this;
}
public void StartWizard()
{
SubjectName = string.Empty;
PositiveSubjectImageCollection.Clear();
NegativeSubjectImageCollection.Clear();
SelectedPositiveSubjectImageCollection.Clear();
SelectedNegativeSubjectImageCollection.Clear();
BuilderStepType = VisualAlertBuilderStepType.NewSubject;
}
public async void AddNewImage(ImageAnalyzer img)
{
try
{
BitmapImage bitmap = new BitmapImage();
await bitmap.SetSourceAsync((await img.GetImageStreamCallback()).AsRandomAccessStream());
var imageItem = new Tuple<BitmapImage, ImageAnalyzer>(bitmap, img);
switch (BuilderStepType)
{
case VisualAlertBuilderStepType.AddPositiveImages:
PositiveSubjectImageCollection.Add(imageItem);
if (this.positiveImagesGrid.SelectionMode == ListViewSelectionMode.Multiple)
{
this.positiveImagesGrid.SelectedItems.Add(imageItem);
}
break;
case VisualAlertBuilderStepType.AddNegativeImages:
NegativeSubjectImageCollection.Add(imageItem);
if (this.negativeImagesGrid.SelectionMode == ListViewSelectionMode.Multiple)
{
this.negativeImagesGrid.SelectedItems.Add(imageItem);
}
break;
}
}
catch (Exception ex)
{
if (SettingsHelper.Instance.ShowDebugInfo)
{
await Util.GenericApiCallExceptionHandler(ex, "Error loading captured image.");
}
}
}
private void UpdateWizardView()
{
this.nextStepButton.Content = "Next";
switch (BuilderStepType)
{
case VisualAlertBuilderStepType.NewSubject:
this.stepNumber.Text = "1";
this.nextStepButton.IsEnabled = !string.IsNullOrEmpty(SubjectName);
break;
case VisualAlertBuilderStepType.AddPositiveImages:
this.nextStepButton.IsEnabled = PositiveSubjectImageCollection.Any();
this.imageCount.Text = $"{PositiveSubjectImageCollection.Count}";
this.imageCountTextBlock.Visibility = Visibility.Visible;
break;
case VisualAlertBuilderStepType.AddNegativeImages:
this.stepNumber.Text = "2";
this.nextStepButton.IsEnabled = NegativeSubjectImageCollection.Any();
this.imageCount.Text = $"{NegativeSubjectImageCollection.Count}";
this.imageCountTextBlock.Visibility = Visibility.Visible;
if (this.positiveImagesGrid.SelectedItems != null)
{
SelectedPositiveSubjectImageCollection.Clear();
var selectedPosImages = this.positiveImagesGrid.SelectedItems.Cast<Tuple<BitmapImage, ImageAnalyzer>>().ToArray();
SelectedPositiveSubjectImageCollection.AddRange(selectedPosImages);
}
break;
case VisualAlertBuilderStepType.TrainModel:
this.stepNumber.Text = "3";
this.nextStepButton.Content = "Train model";
this.imageCountTextBlock.Visibility = Visibility.Collapsed;
if (this.negativeImagesGrid.SelectedItems != null)
{
SelectedNegativeSubjectImageCollection.Clear();
var selectedNegImages = this.negativeImagesGrid.SelectedItems.Cast<Tuple<BitmapImage, ImageAnalyzer>>().ToArray();
SelectedNegativeSubjectImageCollection.AddRange(selectedNegImages);
}
break;
}
this.WizardStepChanged?.Invoke(this, new Tuple<VisualAlertBuilderStepType, VisualAlertModelData>(BuilderStepType,
new VisualAlertModelData()
{
Name = SubjectName,
PositiveImages = SelectedPositiveSubjectImageCollection.Select(x => x.Item2).ToList(),
NegativeImages = SelectedNegativeSubjectImageCollection.Select(x => x.Item2).ToList()
}));
}
private void OnSubjectNameTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox)sender;
if (textBox != null && BuilderStepType == VisualAlertBuilderStepType.NewSubject)
{
this.nextStepButton.IsEnabled = !string.IsNullOrEmpty(textBox.Text);
}
}
private void OnNextStepButtonClicked(object sender, RoutedEventArgs e)
{
switch (BuilderStepType)
{
case VisualAlertBuilderStepType.NewSubject:
BuilderStepType = VisualAlertBuilderStepType.AddPositiveImages;
break;
case VisualAlertBuilderStepType.AddPositiveImages:
BuilderStepType = VisualAlertBuilderStepType.AddNegativeImages;
break;
case VisualAlertBuilderStepType.AddNegativeImages:
BuilderStepType = VisualAlertBuilderStepType.TrainModel;
break;
case VisualAlertBuilderStepType.TrainModel:
this.WizardCompleted?.Invoke(this, new VisualAlertModelData()
{
Name = SubjectName,
PositiveImages = SelectedPositiveSubjectImageCollection.Select(x => x.Item2).ToList(),
NegativeImages = SelectedNegativeSubjectImageCollection.Select(x => x.Item2).ToList()
});
break;
}
}
private void OnBackStepButtonClicked(object sender, RoutedEventArgs e)
{
switch (BuilderStepType)
{
case VisualAlertBuilderStepType.AddPositiveImages:
BuilderStepType = VisualAlertBuilderStepType.NewSubject;
break;
case VisualAlertBuilderStepType.AddNegativeImages:
BuilderStepType = VisualAlertBuilderStepType.AddPositiveImages;
break;
case VisualAlertBuilderStepType.TrainModel:
BuilderStepType = VisualAlertBuilderStepType.AddNegativeImages;
break;
}
}
private void OnPositiveImagesGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.positiveImagesGrid.SelectedItems != null)
{
int selectedCount = this.positiveImagesGrid.SelectedItems.Count;
this.imageCount.Text = selectedCount.ToString();
this.nextStepButton.IsEnabled = selectedCount >= MinPositiveImageCount;
}
}
private void OnNegativeImagesGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.negativeImagesGrid.SelectedItems != null)
{
int selectedCount = this.negativeImagesGrid.SelectedItems.Count;
this.imageCount.Text = selectedCount.ToString();
this.nextStepButton.IsEnabled = selectedCount >= MinNegativeImageCount;
}
}
}
}