-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
609 lines (528 loc) · 17.3 KB
/
MainWindow.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.IO;
using System.Data;
using System.Drawing;
// Emgu.CV
using Emgu.CV;
using Emgu.CV.Face;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
//
using ThothTrainer.Models;
using ThothTrainer.Managers;
using ThothTrainer.Views;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Threading;
namespace ThothTrainer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<Face> _faces = new ObservableCollection<Face>();
private ThothRecognizer _recognizer;
private Capture _capture = null;
private bool _captureInProgress = false;
private User _user = new User();
private int cameraIndex = 0;
private int cameraCount = 1;
// COVER of Camera
private static string _cameraCoverFile = Environment.CurrentDirectory + ("/Resources/Obama.jpg");
public MainWindow()
{
InitializeComponent();
InitializeDataBind();
InitializeCamera();
StartTraining();
}
private void StartTraining()
{
SetStatus("Thoth training is in progress...");
Task task = new Task(() => {
_recognizer.InitializeTraining();
});
task.ContinueWith(OnTrainingCompleted);
task.Start();
}
private void OnTrainingCompleted(Task task)
{
_recognizer.OnTrainingCompleted();
string message = string.Empty;
if (task.IsCompleted)
{
message = ("Thoth training is completed");
}
else if (task.IsFaulted)
{
message = ("Thoth training is failed");
}
else if (task.IsCanceled)
{
message = ("Thoth training is canceled");
}
if (string.IsNullOrEmpty(message))
{
return;
}
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate () {
SetStatus(message);
});
}
private void InitializeDataBind()
{
_recognizer = new ThothRecognizer();
PictureList.ItemsSource = _faces;
}
private void InitializeCamera()
{
CameraImageBox.Image = new Mat(_cameraCoverFile, LoadImageType.Color);
}
/// <summary>
/// Update message statusbar
/// </summary>
/// <param name="message"></param>
private void SetStatus(string message)
{
StatusBar.Content = message;
}
private void OnProcessingFrame(object sender, EventArgs e)
{
Mat frame = new Mat();
try
{
bool isOK = _capture.Retrieve(frame);
if (isOK)
{
var image = _recognizer.DetectFace(frame, Face.W, Face.H);
CameraImageBox.Image = image;
}
if (_captureInProgress.Equals(false))
{
frame = new Mat(_cameraCoverFile, LoadImageType.Color);
var image = _recognizer.DetectFace(frame, Face.W, Face.H);
CameraImageBox.Image = image;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private void AddFace(Mat image)
{
var faceCount = 0;
long identity = 0;
long.TryParse(IdentityTextBox.Text, out identity);
string name = NameTextBox.Text;
var grayFaces = _recognizer.DetectFace(image, Face.W, Face.H, out faceCount);
foreach (var grayFace in grayFaces)
{
if (grayFace == null)
{
throw new NoFacesException("No faces detected");
}
AssignFormValues();
var data = grayFace.ToJpegData(100); // Store image in JPEG format, highest quality
_faces.Add(new Face(data, identity, name));
}
ScrollToBottom(PictureList);
SetStatus(faceCount + " face" + (faceCount > 1 ? "s" : "") + " detected");
}
private void AssessForm()
{
if (IsInvalid())
{
NameTextBox.Focus();
throw new InvalidFormException("The form is incomplete");
}
}
private void AssignFormValues()
{
long identity = 0;
long.TryParse(IdentityTextBox.Text, out identity);
_user.ID = identity;
_user.Email = EmailTextBox.Text;
_user.Name = NameTextBox.Text;
}
private bool IsInvalid()
{
if (string.IsNullOrWhiteSpace(NameTextBox.Text))
{
return true;
}
return false;
}
private void ClearFaces()
{
while (_faces.Count > 0)
{
_faces.RemoveAt(0);
}
SearchResult.ItemsSource = null;
}
private void TakePicture()
{
Mat frame = new Mat();
if (_captureInProgress)
{
frame = _capture.QueryFrame();
}
else
{
frame = new Mat(_cameraCoverFile, LoadImageType.Color);
CameraImageBox.Image = frame;
CameraImageBox.Refresh();
}
if (frame == null)
{
throw new NoImagesException("No images detected");
}
AddFace(frame);
}
private void ChoosePicture()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.DefaultExt = ".jpg";
dialog.Filter = "JPG image|*.jpg|PNG image|*.png";
bool? isSelected = dialog.ShowDialog();
if (isSelected.Equals(true))
{
var image = new Mat(dialog.FileName, LoadImageType.Color);
AddFace(image);
SetStatus("Face is detected succesfully");
}
else
{
SetStatus("Selection cancelled");
}
}
private void DropPictures(string[] files, List<int> results)
{
foreach (string file in files)
{
FileInfo fi = new FileInfo(file);
if (fi.Attributes.Equals(FileAttributes.Directory))
{
string[] f = Directory.GetFiles(file);
DropPictures(f, results);
continue;
}
var image = new Mat(file, LoadImageType.Color);
results[0]++;
try
{
AddFace(image);
}
catch (NoFacesException)
{
results[1]++;
}
}
}
private void SaveAndTrain()
{
AssessForm();
AssignFormValues();
var faceCount = _faces.Count();
string spString = (faceCount > 1 ? "s" : "");
var confirmMessage = string.Empty;
if (_user.ID.Equals(User.NEWID))
{
confirmMessage = string.Format("Are you sure to ADD a NEW user [{0}] with {1} face picture{2}?", _user.Name, faceCount, spString);
}
else
{
confirmMessage = string.Format("Are you sure to ADD [{0}] face picture{1} for [{2}]?", faceCount, spString, _user.Name);
}
var messageResult = MessageBox.Show(confirmMessage, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (messageResult == MessageBoxResult.Yes)
{
long identity = _user.ID;
int result = UserManager.AddUserWithFaces(_user, _faces);
string message = string.Empty;
if (_user.ID > identity)
{
message = "A new user has been created, ";
IdentityTextBox.Text = _user.ID.ToString();
}
message += result + " face image" + (result > 1 ? "s" : "") + " are imported into database";
SetStatus(message);
if (result > 0)
{
ClearFaces();
ClearForm();
}
StartTraining();
}
}
private void SearchUsers()
{
AssignFormValues();
DataSet dataSet = UserManager.SearchUser(_user);
SetStatus("Found " + dataSet.Tables[0].Rows.Count + " people");
SearchResult.SelectionChanged -= SearchResult_SelectionChanged;
SearchResult.ItemsSource = dataSet.Tables[0].DefaultView;
SearchResult.SelectionChanged += SearchResult_SelectionChanged;
}
private void DeleteUser()
{
int result = UserManager.DeleteUser(_user);
if (result > 0)
{
SetStatus("User [" + _user.Name + "] has been DELETED");
}
else
{
SetStatus("User [" + _user.Name + "] is NOT deleted");
}
ClearForm();
SearchUsers();
}
private void DeletePictures()
{
int result = UserManager.DeleteUser(_user, false);
if (result > 0)
{
SetStatus("Pictures of [" + _user.Name + "] has been DELETED");
}
else
{
SetStatus("Pictures of [" + _user.Name + "] is NOT deleted");
}
}
#region helpers
private void ScrollToBottom(ListBox listBox)
{
listBox.SelectedIndex = listBox.Items.Count - 1;
listBox.ScrollIntoView(listBox.SelectedItem);
listBox.SelectedIndex = -1;
}
private void ClearForm()
{
IdentityTextBox.Text = EmailTextBox.Text = NameTextBox.Text = string.Empty;
SearchResult.SelectedIndex = -1;
_user.ID = 0;
_user.Email = _user.Name = string.Empty;
ResetFacesID();
}
private void ResetFacesID()
{
for (int i = 0; i < _faces.Count; i++)
{
_faces[i].DisplayName = _user.Name;
}
PictureList.Items.Refresh();
}
#endregion helpers
#region events
/// <summary>
/// Take picture from camera
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TakePictures_Click(object sender, RoutedEventArgs e)
{
try
{
TakePicture();
}
catch (Exception ex)
{
SetStatus(ex.Message);
}
}
/// <summary>
/// Take picture from file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChoosePictures_Click(object sender, RoutedEventArgs e)
{
try
{
ChoosePicture();
}
catch (Exception ex)
{
SetStatus(ex.Message);
}
}
/// <summary>
/// Remove all the pictures from list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ResetPictures_Click(object sender, RoutedEventArgs e)
{
ClearForm();
ClearFaces();
SetStatus("");
NameTextBox.Focus();
}
/// <summary>
/// Placeholder for dragging
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_DragEnter(object sender, DragEventArgs e)
{
ThothTrainer.Opacity = 0.5;
}
/// <summary>
/// Read data from dropped files
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Drop(object sender, DragEventArgs e)
{
try
{
ThothTrainer.Opacity = 1;
string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
var results = new List<int>() { 0, 0 };
DropPictures(files, results);
SetStatus("" + results[0] + " file" + (results[0] > 1 ? "s" : "") + ", " + results[1] + " failure" + (results[1] > 1 ? "s" : ""));
}
catch (Exception ex)
{
SetStatus(ex.Message);
}
}
private void Window_DragLeave(object sender, DragEventArgs e)
{
ThothTrainer.Opacity = 1;
}
/// <summary>
/// Pause or resume camera
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CameraImageBox_Click(object sender, EventArgs e)
{
if(_capture != null)
{
_capture.ImageGrabbed -= OnProcessingFrame;
_capture.Stop();
_capture.Dispose();
}
if (cameraIndex > cameraCount - 1)
{
_captureInProgress = false;
CameraImageBox.Image = new Mat(_cameraCoverFile, LoadImageType.Color);
cameraIndex = 0;
}
else
{
_capture = new Capture(cameraIndex);
_capture.ImageGrabbed += OnProcessingFrame;
_capture.Start();
_captureInProgress = true;
cameraIndex++;
}
}
/// <summary>
/// Store data into database or file system
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SubmitAndTrain_Click(object sender, RoutedEventArgs e)
{
try
{
SaveAndTrain();
}
catch (Exception ex)
{
SetStatus(ex.Message);
}
}
private void NameTextBox_LostFocus(object sender, RoutedEventArgs e)
{
}
private void Search_Click(object sender, RoutedEventArgs e)
{
SearchUsers();
}
private void SearchResult_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataRowView item = (sender as ListBox).SelectedItem as DataRowView;
if(item == null)
{
return;
}
_user.Name = NameTextBox.Text = item["Name"].ToString();
_user.Email = EmailTextBox.Text = item["Email"].ToString();
_user.ID = 0;
long identity = 0;
long.TryParse(item["ID"].ToString(), out identity);
_user.ID = identity;
IdentityTextBox.Text = identity.ToString();
ResetFacesID();
}
private void DeleteSelectedUser_Click(object sender, RoutedEventArgs e)
{
if (_user.ID.Equals(0))
{
return;
}
if (MessageBox.Show("Are you sure to DELETE ["+_user.Name+"]?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
DeleteUser();
}
else
{
}
}
private void RemovePicture_Click(object sender, RoutedEventArgs e)
{
int index = PictureList.SelectedIndex;
if (index.Equals(-1))
{
return;
}
_faces.RemoveAt(index);
}
private void ResetFormOnly_Click(object sender, RoutedEventArgs e)
{
ClearForm();
SetStatus("");
NameTextBox.Focus();
}
private void NameTextBox_KeyUp(object sender, KeyEventArgs e)
{
_user.Name = NameTextBox.Text;
ResetFacesID();
}
private void DeletePicOnly_Click(object sender, RoutedEventArgs e)
{
if (_user.ID.Equals(0))
{
return;
}
if (MessageBox.Show("Are you sure to DELETE pictures of [" + _user.Name + "]?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
DeletePictures();
}
else
{
}
}
private void NameTextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//UserWindow userWindow = new UserWindow();
//userWindow.User = _user;
//userWindow.InitializeUI();
//userWindow.Show();
}
#endregion events
}
}