ShowcaseDemo
ShowcaseDemo.xaml.cs
1 using System;
2 using System.Windows;
3 using SharpGL.SceneGraph;
4 using SharpGL;
5 using VisageCSWrapper;
6 using System.Drawing;
7 using System.Drawing.Imaging;
8 using System.ComponentModel;
9 using System.Runtime.InteropServices;
10 using System.Windows.Threading;
11 using Emgu.CV;
12 using Emgu.CV.UI;
13 using Emgu.CV.Structure;
14 using Emgu.CV.CvEnum;
15 using System.Windows.Forms;
16 using System.Collections.Generic;
17 using System.Linq;
18 using System.Threading;
19 using System.Windows.Controls;
20 using DirectShowLib;
21 
22 namespace ShowcaseDemo
23 {
24  /*! \mainpage visage|SDK ShowcaseDemo example project
25  *
26  * \if WIN64_DOXY
27  * \htmlonly
28  * <table border="0">
29  * <tr>
30  * <td width="32"><a href="../../../../../../bin64/DEMO_ShowcaseDemo.exe"><img src="../../../../../../doc/OpenGL/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run DEMO_ShowcaseDemo.exe from the visageSDK\bin64 folder.)"></a></td>
31  * <td width="32"><a href="../../../../../../bin64"><img src="../../../../../../doc/OpenGL/images/open_bin_folder.png" border=0 title="Open Binary Folder (visageSDK\bin64)"></a></td>
32  * <td width="32"><a href="../../../../data/Videos"><img src="../../../../../../doc/OpenGL/images/open_folder.png" border=0 title="Open Video Folder"></a></td>
33  * <td width="32"><a href="../../../../build/msvc142/ShowcaseDemo"><img src="../../../../../../doc/OpenGL/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
34  * <td width="32"><a href="../../../../source/ShowcaseDemo"><img src="../../../../../../doc/OpenGL/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
35  * </tr>
36  * </table>
37  * \endhtmlonly
38  * \else
39  * \htmlonly
40  * <table border="0">
41  * <tr>
42  * <td width="32"><a href="../../../../../../bin/DEMO_ShowcaseDemo.exe"><img src="../../../../../../doc/OpenGL/images/run_sample.png" border=0 title="Run Sample (this may not work in all browsers, in such case please run DEMO_ShowcaseDemo.exe from the visageSDK\bin folder.)"></a></td>
43  * <td width="32"><a href="../../../../../../bin"><img src="../../../../../../doc/OpenGL/images/open_bin_folder.png" border=0 title="Open Binary Folder (visageSDK\bin)"></a></td>
44  * <td width="32"><a href="../../../../data/Videos"><img src="../../../../../../doc/OpenGL/images/open_folder.png" border=0 title="Open Video Folder"></a></td>
45  * <td width="32"><a href="../../../../build/msvc142/ShowcaseDemo"><img src="../../../../../../doc/OpenGL/images/open_project_folder.png" border=0 title="Open Project Folder"></a></td>
46  * <td width="32"><a href="../../../../source/ShowcaseDemo"><img src="../../../../../../doc/OpenGL/images/open_source_folder.png" border=0 title="Open Source Code Folder"></a></td>
47  * </tr>
48  * </table>
49  * \endhtmlonly
50  * \endif
51  *
52  * The ShowcaseDemo example project demonstrates the following capabilities of visage|SDK C# API:
53  *
54  * - real-time head/facial features tracking, face recognition and age, gender and emotion estimation on multiple faces from video file or camera, based on the VisageCSWrapper.VisageTracker tracker, VisageCSWrapper.VisageFaceRecognition and VisageCSWrapper.VisageFaceAnalyser
55  * - detecting facial features, face recognition and age, gender and emotion estimation on multiple faces in still images, based on the VisageCSWrapper.VisageFeaturesDetector detector, VisageCSWrapper.VisageFaceRecognition and VisageCSWrapper.VisageFaceAnalyser
56  *
57  * Requirements:
58  * - Microsoft .NET Framework 4.0
59  * - Emgu CV: msvcp120.dll, msvcr120.dll
60  *
61  * Prebuilt version of the sample with integrated license key file can be found in
62  * \if WIN64_DOXY
63  * bin64
64  * \else
65  * bin
66  * \endif
67  * folder. In case you wish to rebuild the application, you will need a valid license key file.
68  * For more information please take a look at <a href="../../../../../../doc/OpenGL/licensing.html"><em>Licensing</em></a> section of the documentation.
69  *
70  * The camera tracking starts when you run the application.
71  *
72  * The specific classes and methods that demonstrate visage|SDK are:
73  * - ShowcaseDemo.MainWindow.runTrackFromVideo(string file): Performs real time tracking from a video file.
74  * - ShowcaseDemo.MainWindow.runTrackFromCam(): Performs real time tracking from a camera.
75  * - ShowcaseDemo.MainWindow.runTrackFromImage(string file, VISAGE_MODE activeMode): Performs real time tracking from an image.
76  *
77  * Drawing is implemented in ShowcaseDemo.VisageRendering which uses SharpGL, an external library which provides a C# wrapper of OpenGL.
78  *
79  * For frame capturing in video and camera tracking, ShowcaseDemo uses Emgu, an OpenCV wrapper for C#.
80  *
81  * Further classes and methods that are important for detailed understanding how this
82  * sample project is implemented and how it uses visage|SDK are:
83  * - VisageCSWrapper.FaceData class that is used for storing all the tracking data
84  *
85  */
86 
87  /// <summary>
88  /// Application can either be in TRACKER mode or in DETECTOR mode.
89  ///
90  /// Tracker mode features face tracking from image, video or camera source.
91  /// Detector mode features face detection from an image source.
92  /// </summary>
93  public enum VISAGE_MODE
94  {
95  TRACKER = 0,
96  DETECTOR
97  };
98 
99  /// <summary>
100  /// Type of input for tracker/detector.
101  /// </summary>
102  public enum INPUT_MODE
103  {
104  CAMERA = 0,
105  VIDEO,
106  IMAGE
107  };
108 
109  #region Textured model
110  struct TexturedModel
111  {
112  public List<Point3D> v; // geometric vertices
113  public List<Point> vt; // texture vertices
114  public List<Face> faces; // vertex indexes
115  }
116 
117  struct Face
118  {
119  public int[] indexPoints;
120  public int[] texIndexPoints;
121 
122  public Face(int[] indexPoints, int[] texIndexPoints)
123  {
124  this.indexPoints = indexPoints;
125  this.texIndexPoints = texIndexPoints;
126  }
127  }
128 
129  struct Point
130  {
131  public float x;
132  public float y;
133 
134  public Point(float x, float y)
135  {
136  this.x = x;
137  this.y = y;
138  }
139  };
140 
141  struct Point3D
142  {
143  public float x;
144  public float y;
145  public float z;
146 
147  public Point3D(float x, float y, float z)
148  {
149  this.x = x;
150  this.y = y;
151  this.z = z;
152  }
153  };
154  #endregion
155 
156 
157 
158  /// <summary>
159  /// VisageSDK ShowcaseDemo application MainWindow class.
160  ///
161  /// Demonstrates multi-threaded implementation and usage of visage|SDK C# wrapper.
162  /// </summary>
163  public partial class MainWindow : Window
164  {
165 
166  #region Display Constant Variables
167  int ACTIVE_DISPLAY = VisageRendering.DISPLAY_FRAME + VisageRendering.DISPLAY_FEATURE_POINTS + VisageRendering.DISPLAY_SPLINES + VisageRendering.DISPLAY_TRACKING_QUALITY + VisageRendering.DISPLAY_POINT_QUALITY;
168  bool ACTIVE_MIRROR = false;
169  #endregion
170 
171  #region Globals
172  VisageTracker gVisageTracker;
173  VisageFaceAnalyser gVisageFaceAnalyser;
174  VisageFeaturesDetector gVisageFeaturesDetector;
175  VisageFaceRecognition gVisageFaceRecognition;
176 
177 
178  VisageRendering gVisageRendering;
179  OpenGL gl;
180 
181  public bool isTracked = false;
182  public bool isDetected = false;
183  public bool inDrawDetection = false;
184 
185  VSImage frame;
186  VSImage frameRGB;
187 
188  //Used for pure tracking timer
189  public long elapsedTime;
190  public long startTime;
191  public long endTime;
192  System.Diagnostics.Stopwatch pureTrackStopwatch = new System.Diagnostics.Stopwatch();
193 
194  //Lock for draw thread and track thread synchronization
195  private Object lockFaceData = new Object();
196 
197  private Object lockClearRecognition = new Object();
198 
199  //BackgroundWorker object used in tracking (image, camera, video) and detection (image)
200  private BackgroundWorker worker;
201  //BackgroundWorker object used in face recognition
202  private BackgroundWorker fr_worker;
203  //BackgroundWorker object used in face analysis
204  private BackgroundWorker fa_worker;
205 
206  //VisageTracker
207  public FaceData[] gFaceData;
208  public int[] gTrackerStatus;
209  public INPUT_MODE activeInputMode = INPUT_MODE.CAMERA;
210  public VISAGE_MODE activeVisageMode = VISAGE_MODE.TRACKER;
211  string currentVideo = "";
212  string currentImage = "";
213  int currentCameraDevice = 0;
214  Dictionary<int, string> cameraDevices = new Dictionary<int, string>();
215  Capture cameraCapture;
216  const int maxFacesTracker = 4;
217 
218  //VisageFeaturesDetector
219  FaceData[] dataArray;
220  public bool detectorActive = false;
221  int numFaces = 0;
222  const int MAX_FACES_DETECTOR = 30;
223 
224  //VisageFaceAnalyser
225  int vfaInitialised = 0;
226  float[] gAgeArray = { -1.0f };
227  int[] gGenderArray = { -1 };
228  List<float[]> gEmotionList = new List<float[]>();
229  const int NUM_EMOTIONS = 7;
230  int[] gGenderSmoothed = { -1 };
231  float[] gAgeSmoothed = { -1.0f };
232  List<float[]> gEmotionsSmoothed = new List<float[]>();
233 
234  //VisageFaceRecognition
235  List<string> recognizedFaces = new List<string>();
236  int[] numInitFrames = new int[MAX_FACES_DETECTOR]; // current number of frames taken in face recognition initialization phase
237  const int numInitFramesThreshold = 10; //determines number of frames needed in face recognition initialization phase
238  int[] numFramesNewIdentity = new int[MAX_FACES_DETECTOR]; // current number of subsequent frames needed for new identity to be added
239  const int newIdentityFramesThreshold = 5; // determines number of subsequent frames needed for new identity to be added
240  //
241  int numPersons = 0; // number of new identities added so far
242  List<string> gUniqueNames = new List<string>();
243  string[] currentDisplayName = new string[MAX_FACES_DETECTOR];
244  Dictionary<int, string> indices_to_names = new Dictionary<int, string>();
245  float display_threshold = 0.65f;
246  List<string> max_faces = new List<string>();
247  Dictionary<int, List <short[]>> descBuffer = new Dictionary<int, List <short[]>>();
248 
249  //
250  string imageInitialDir = @"..\Samples\OpenGL\data\Images";
251  string videoInitialDir = @"..\Samples\OpenGL\data\Videos";
252 
253  //control flags accessed from face recognition/face analysis background thread as well as from the main UI thread
254  //set when the check-box in the UI thread is clicked
255  volatile bool FR_CHECKED = false;
256  volatile bool FA_AGE_CHECKED = false;
257  volatile bool FA_EMO_CHECKED = false;
258  volatile bool FA_GEN_CHECKED = false;
259 
260  bool freeze_gallery = false;
261 
262 
263  #endregion
264 
265  /// <summary>
266  /// Main entry point of the application.
267  ///
268  /// Creates instances of <see cref="VisageTracker"/>, <see cref="VisageFeaturesDetector"/> and <see cref="VisageFaceAnalyser"/> objects and demonstrates
269  /// license initialization. Also initializes OpenGL rendering.
270  ///
271  /// Starts tracking from camera, as a default mode.
272  /// </summary>
273  public MainWindow()
274  {
275  InitializeComponent();
276 
277  VisageLicensing.InitializeLicenseManager(licensePath);
278 
279  //Create VisageTracker object
280  gVisageTracker = new VisageTracker(@"..\Samples\data\Facial Features Tracker.cfg");
281 
282  //Create FaceData object used in tracking
283  gFaceData = new FaceData[maxFacesTracker];
284  for (int i = 0; i < maxFacesTracker; ++i)
285  {
286  gFaceData[i] = new FaceData();
287  }
288 
289  //Create and initialize VisageFaceAnalayser object
290  gVisageFaceAnalyser = new VisageFaceAnalyser();
291  vfaInitialised = gVisageFaceAnalyser.Init(@"..\Samples\data\vfa");
292 
293  //Initialize variables for storing and smoothing face analysis results
295 
296  //Initialize variables for storing face recognition results
298 
299  //Create VisageFeaturesDetector object
300  gVisageFeaturesDetector = new VisageFeaturesDetector(@"..\Samples\data\Face Detector.cfg");
301 
302  //Create VisageFeaturesRecognition object
303  gVisageFaceRecognition = new VisageFaceRecognition(@"..\Samples\data\vfr\fr.bin");
304 
305  //Create the renderer and openGL control objects
306  gl = openGLControl.OpenGL;
307  gVisageRendering = new VisageRendering(gl);
308 
309  //Load and set texture image
310  gVisageRendering.SetTextureImage(LoadImage(@"..\Samples\OpenGL\data\ShowcaseDemo\tiger_texture.png"));
311 
312  //Populate combox box with connected camera names
313  DsDevice[] _SystemCamereas = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
314  for (int i = 0; i < _SystemCamereas.Length; i++)
315  {
316  cameraDevices.Add(i, _SystemCamereas[i].Name);
317  ComboBoxItem cbi = new ComboBoxItem();
318  cbi.Content = _SystemCamereas[i].Name;
319  cbCameraChoice.Items.Add(cbi);
320  }
321  if (cameraDevices.Count > 0)
322  {
323  cbCameraChoice.SelectedIndex = currentCameraDevice; //Set the selected device the default
324  }
325  }
326 
327  #region FR and FA State Modifiers
328 
329  /// <summary>
330  /// Reset all the face recognition variables including gallery. Called when gallery is cleared.
331  /// </summary>
332  private void ClearFaceRecognition()
333  {
334  lock (lockClearRecognition)
335  {
336  for (int trackerIndex = 0; trackerIndex < maxFacesTracker; ++trackerIndex)
337  {
338  lbxNames.Items.Clear();
339  recognizedFaces = new List<string>();
340  descBuffer[trackerIndex].Clear();
341  numInitFrames[trackerIndex] = 0;
342  numFramesNewIdentity[trackerIndex] = 0;
343  numPersons = 0;
344  gUniqueNames = new List<string>();
345  currentDisplayName[trackerIndex] = "";
346  indices_to_names = new Dictionary<int, string>();
347  gVisageFaceRecognition.ResetGallery();
348  }
349  }
350  }
351 
352  /// <summary>
353  /// Initialize face recognition buffer variables. Called when the application starts.
354  /// </summary>
356  {
357  for (int i = 0; i < maxFacesTracker; ++i)
358  {
359  //For each face, initialize a list that will that face descriptors
360  descBuffer.Add(i, new List<short[]>());
361  //For each face reset the counters
362  numInitFrames[i] = 0;
363  numFramesNewIdentity[i] = 0;
364  //
365  currentDisplayName[i] = "";
366  }
367  }
368 
369 
370  /// <summary>
371  /// Reinitialize face recognition variables used during face recognition initialization phase. For example, when the face is lost in the middle of the initialization
372  /// process.
373  /// </summary>
374  /// <param name="index">Index of the face corresponding to the index of the array where face analysis data is stored.</param>
375  private void ResetFRInitialization(int index = -1)
376  {
377  lock (lockClearRecognition)
378  {
379  if (index == -1)
380  {
381  for (int i = 0; i < maxFacesTracker; ++i)
382  {
383  descBuffer[i].Clear();
384  numFramesNewIdentity[i] = 0;
385 
386  if (numInitFrames[i] != 0 && numInitFrames[i] < numInitFramesThreshold)
387  numInitFrames[i] = numInitFramesThreshold;
388  }
389  }
390  else
391  {
392  descBuffer[index].Clear();
393  numFramesNewIdentity[index] = 0;
394 
395  if (numInitFrames[index] != 0 && numInitFrames[index] < numInitFramesThreshold)
396  numInitFrames[index] = numInitFramesThreshold;
397  }
398  }
399  }
400 
401 
402  /// <summary>
403  /// Reinitialize face analysis variables for storing data.
404  /// </summary>
405  private void InitializeFaceAnalysis()
406  {
407  gAgeArray = new float[MAX_FACES_DETECTOR];
408  gGenderArray = new int[MAX_FACES_DETECTOR];
409  //
410  gAgeSmoothed = new float[maxFacesTracker];
411  gGenderSmoothed = new int[maxFacesTracker];
412  gEmotionsSmoothed.Clear();
413 
414  for (int i = 0; i < MAX_FACES_DETECTOR; ++i)
415  {
416  gAgeArray[i] = -1.0f;
417  gGenderArray[i] = -1;
418  gEmotionList.Add(default(float[]));
419  }
420  //
421  for (int i = 0; i < maxFacesTracker; ++i)
422  {
423  gAgeSmoothed[i] = -1.0f;
424  gGenderSmoothed[i] = -1;
425  gEmotionsSmoothed.Add(default(float[]));
426 
428  }
429  }
430 
431  /// <summary>
432  /// Clears face analysis data and smoothing data for the specific face.
433  /// </summary>
434  /// <param name="index">Index of the face corresponding to the index of the array where face analysis data is stored.</param>
435  private void ResetFaceAnalysis(int index)
436  {
437  gAgeSmoothed[index] = -1.0f;
438  gGenderSmoothed[index] = -1;
439  gEmotionsSmoothed[index] = default(float[]);
440 
441  gVisageFaceAnalyser.resetStreamAnalysis(index);
442  }
443 
444  #endregion
445 
446 
447  #region BackgroundWorker Tracking Functions
448 
449  private static byte[] BitmapToByteArray(Bitmap bitmap)
450  {
451  int stride = 0;
452  return BitmapToByteArray(bitmap, ref stride);
453  }
454 
455  /// <summary>
456  /// Helper function for converting image data from Bitmap class to a raw byte array.
457  /// </summary>
458  /// <param name="bitmap">Bitmap image.</param>
459  /// <param name="stride">Image stride.</param>
460  /// <returns></returns>
461  private static byte[] BitmapToByteArray(Bitmap bitmap, ref int stride)
462  {
463  System.Drawing.Imaging.PixelFormat pixelFormat = bitmap.PixelFormat;
464  BitmapData bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, pixelFormat);
465  int numbytes = bmpdata.Stride * bitmap.Height;
466  //((n + (k-1)) / k) * k;
467  //modify height to be the value of the next nearest number divisble by 4
468  int numbytes_padded = bmpdata.Stride * ((bitmap.Height + 3) / 4 * 4);
469  byte[] bytedata = new byte[numbytes_padded];
470  stride = bmpdata.Stride;
471  IntPtr ptr = bmpdata.Scan0;
472 
473  Marshal.Copy(ptr, bytedata, 0, numbytes);
474 
475  bitmap.UnlockBits(bmpdata);
476 
477  return bytedata;
478  }
479 
480  /// <summary>
481  /// Worker event for tracking/detection from an image source that runs in a separate thread. Used in runTrackFromImage() function.
482  ///
483  /// Expects two arguments sent via e parameter: path to the image and currently active VISAGE_MODE (TRACKER, DETECTOR). Based on the current mode initiates tracking or detection.
484  /// Depending on the tracker return status runs face analysis and face recognition in a separate thread.
485  ///
486  /// Locking mechanism is used to synchronize frame and tracking results rendering. Due to face analysis and face recognition running in separate thread
487  /// rendering of age, gender, emotion and identity is not synchronized with the rendering of the frame.
488  ///
489  /// Implemented using <see cref="BackgroundWorker"/>.
490  /// </summary>
491  /// <param name="sender"></param>
492  /// <param name="e"></param>
493  private void worker_DoWorkImage(object sender, DoWorkEventArgs e)
494  {
495  BackgroundWorker worker = sender as BackgroundWorker;
496  List<object> argumentList = e.Argument as List<object>;
497  string file = (string)argumentList[0];
498  VISAGE_MODE currentMode = (VISAGE_MODE)argumentList[1];
499 
500  //Load image data by filename
501  Bitmap bmp = new Bitmap(file);
502  int bitmapStride = 0;
503  byte[] bmp_byte = BitmapToByteArray(bmp, ref bitmapStride);
504  BitmapData bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
505 
506  //Determine image format
507  int format = (int)VISAGE_FORMAT.BGR;
508  int origin = (int)VISAGE_ORIGIN.TL;
509  int nChannels = 3;
510  switch (bmp.PixelFormat)
511  {
512  case PixelFormat.Format24bppRgb:
513  format = (int)VISAGE_FORMAT.BGR;
514  nChannels = 3;
515  break;
516  case PixelFormat.Format8bppIndexed:
517  format = (int)VISAGE_FORMAT.LUMINANCE;
518  nChannels = 1;
519  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
520  break;
521  case PixelFormat.Format32bppRgb:
522  format = (int)VISAGE_FORMAT.BGRA;
523  nChannels = 4;
524  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
525  break;
526  default:
527  format = -1;
528  break;
529  }
530 
531  //Clear FA variables
532  lock (lockFaceData)
533  {
535  }
536 
537  //Create a VSImage and fill it with data to pass it to the DetectFeatures function
538  frame = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
539  frame.imageData = bmp_byte;
540 
541  byte[] bytedataRGB = new byte[bmp_byte.Length];
542 
543  convertFromBGRToRGB(ref bytedataRGB, ref bmp_byte);
544 
545  frameRGB = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
546  frameRGB.imageData = bytedataRGB;
547 
548  while (true)
549  {
550  //Check if the worker thread was canceled
551  if (worker.CancellationPending || format == -1)
552  {
553  isTracked = false;
554  e.Cancel = true;
555  break;
556  }
557  else if (currentMode == VISAGE_MODE.TRACKER)
558  {
559  //
560  FaceData[] analysisData = new FaceData[maxFacesTracker];
561 
562  //Lock the shared variables so there can be no read/write concurrency between the tracking thread and the rendering thread
563  lock (lockFaceData)
564  {
565  pureTrackStopwatch.Start();
566  startTime = pureTrackStopwatch.ElapsedMilliseconds;
567  gTrackerStatus = gVisageTracker.Track(bmp.Width, bmp.Height, bmp_byte, gFaceData, format, origin, bmpdata.Stride, -1, maxFacesTracker);
568  pureTrackStopwatch.Stop();
569  endTime = pureTrackStopwatch.ElapsedMilliseconds;
570  elapsedTime = endTime - startTime;
571 
572  //While locked, copy face data from the tracker to face data array for face analysis and recognition
573  for (int i = 0; i < maxFacesTracker; ++i)
574  {
575  analysisData[i] = new FaceData(gFaceData[i]);
576  }
577 
578  //Signalize to the rendering thread that the tracking is complete
579  isTracked = true;
580  }
581 
582  //Run face analysis on each face in a separate thread
583  runFaceAnalysis(frameRGB, analysisData, gTrackerStatus);
584 
585  //Run face recognition on each face in a separate thread
586  runFaceRecognition(frameRGB, analysisData, gTrackerStatus);
587  }
588  else if (currentMode == VISAGE_MODE.DETECTOR)
589  {
590  isDetected = false;
591 
592  //Check that the rendering thread is not rendering, if it is rendering wait
593  while (inDrawDetection)
594  {
595  System.Threading.Thread.Sleep(100);
596  }
597 
598  bmp.UnlockBits(bmpdata);
599 
600  //Create new FaceData array and array objects
601  dataArray = new FaceData[MAX_FACES_DETECTOR];
602  for (int i = 0; i < MAX_FACES_DETECTOR; ++i)
603  {
604  dataArray[i] = new FaceData();
605  }
606 
607  //Check that the detector was initializes successfully, if not cancel worker and return
608  if (gVisageFeaturesDetector.Initialized != true)
609  {
610  worker.CancelAsync();
611  return;
612  }
613 
614  //Detect faces and facial features
615  numFaces = gVisageFeaturesDetector.DetectFacialFeatures(frame, dataArray);
616  if (numFaces > 0)
617  {
618  //Allocate arrays for age, gender and clear the emotion data list
619  gAgeArray = new float[numFaces];
620  gGenderArray = new int[numFaces];
621  gEmotionList.Clear();
622  for (int i = 0; i < numFaces; i++)
623  {
624  //Fill the arrays and the list with default values
625  gAgeArray[i] = -1.0f;
626  gGenderArray[i] = -1;
627  gEmotionList.Add(default(float[]));
628  //Run face analysis on this thread
629  AnalyzeFace(frameRGB, dataArray[i], i);
630  //Run face recognition on this thread
631  RecognizeFace(frameRGB, dataArray[i], i);
632  }
633 
634  }
635 
636  isDetected = true;
637  //Terminate the worker after detection
638  worker.CancelAsync();
639  }
640  }
641  //Stop the tracker
642  gVisageTracker.Stop();
643  }
644 
645  /// <summary>
646  /// Worker event for tracking from camera that runs in a separate thread. Used in runTrackFromCam() function.
647  ///
648  /// Demonstrates handling of the camera and camera frame data retrieval (using OpenCV). Tracks and analyses face in the current camera frame.
649  /// Depending on the tracker return status runs face analysis and face recognition in a separate thread.
650  ///
651  /// Locking mechanism is used to synchronize frame and tracking results rendering. Due to face analysis and face recognition running in separate thread
652  /// rendering of age, gender, emotion and identity is not synchronized with the rendering of the frame.
653  ///
654  /// Implemented using <see cref="BackgroundWorker"/>.
655  /// </summary>
656  /// <param name="sender"></param>
657  /// <param name="e"></param>
658  private void worker_DoWorkCam(object sender, DoWorkEventArgs e)
659  {
660  if (cameraCapture == null)
661  {
662  //Create new OpenCV camera capture
663  cameraCapture = new Capture(currentCameraDevice);
664  //Set desired resolution
665  cameraCapture.SetCaptureProperty(CapProp.FrameWidth, 800);
666  cameraCapture.SetCaptureProperty(CapProp.FrameHeight, 600);
667  }
668 
669  //Age estimation enabled by default
670  Dispatcher.BeginInvoke((Action)(() => { enableAgeEstimCheckbox(); }));
671 
672  //Clear FA variables
673  lock (lockFaceData)
674  {
676  }
677 
678  while (true)
679  {
680  //Check if the worker thread was canceled
681  if (worker.CancellationPending)
682  {
683  isTracked = false;
684  e.Cancel = true;
685  cameraCapture.Dispose();
686  cameraCapture = null;
687  break;
688  }
689  else
690  {
691  //Grab camera frame
692  Bitmap bmp = cameraCapture.QueryFrame().Bitmap;
693 
694  if (ACTIVE_MIRROR == true)
695  bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
696 
697  int nChannels = 3;
698  switch (bmp.PixelFormat)
699  {
700  case PixelFormat.Format24bppRgb:
701  nChannels = 3;
702  break;
703  case PixelFormat.Format8bppIndexed:
704  nChannels = 1;
705  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
706  break;
707  case PixelFormat.Format32bppRgb:
708  nChannels = 4;
709  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
710  break;
711  default:
712  break;
713  }
714 
715  //
716  FaceData[] analysisData = new FaceData[maxFacesTracker];
717 
718  lock (lockFaceData)
719  {
720  //Create a VSImage and fill it with data to pass it to the DetectFeatures function
721  frame = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
722  byte[] bmp_byte = BitmapToByteArray(bmp);
723  frame.imageData = bmp_byte;
724 
725  byte[] bytedataRGB = new byte[bmp_byte.Length];
726 
727  convertFromBGRToRGB(ref bytedataRGB, ref bmp_byte);
728 
729  frameRGB = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
730  frameRGB.imageData = bytedataRGB;
731 
732  pureTrackStopwatch.Start();
733  startTime = pureTrackStopwatch.ElapsedMilliseconds;
734  gTrackerStatus = gVisageTracker.Track(bmp.Width, bmp.Height, bmp_byte, gFaceData, (int)VISAGE_FORMAT.BGR, (int)VISAGE_ORIGIN.TL, 0, -1, maxFacesTracker);
735  pureTrackStopwatch.Stop();
736  endTime = pureTrackStopwatch.ElapsedMilliseconds;
737  elapsedTime = endTime - startTime;
738 
739  //While locked, copy face data from the tracker to face data array for face analysis and recognition //PROVJERITI KASNIJE: prije copya provjeriti da li je ukljucen FA/FR
740  for (int i = 0; i < maxFacesTracker; ++i)
741  {
742  analysisData[i] = new FaceData(gFaceData[i]);
743  }
744 
745  isTracked = true;
746  }
747 
748  //Run face analysis in a separate thread
749  runFaceAnalysis(frameRGB, analysisData, gTrackerStatus);
750 
751  //Run face recognition in a separate thread
752  runFaceRecognition(frameRGB, analysisData, gTrackerStatus);
753  }
754  }
755  //Stop the tracker
756  gVisageTracker.Stop();
757  }
758 
759 
760  /// <summary>
761  /// Converts BGR image obtained from camera to RGB image.
762  /// Image has to be converted to RGB format when VisageFaceAnalyser is used.
763  ///
764  /// </summary>
765  private void convertFromBGRToRGB(ref byte[] outputRGB, ref byte[] inputBGR)
766  {
767  int length = inputBGR.Length;
768  int nChannels = 3;
769  int j = 0;
770  for (int i = 0; i < length/nChannels; i++)
771  {
772  outputRGB[j++] = inputBGR[i * nChannels + 2];
773  outputRGB[j++] = inputBGR[i * nChannels + 1];
774  outputRGB[j++] = inputBGR[i * nChannels];
775  }
776  }
777 
778  /// <summary>
779  /// Worker event for tracking from video that runs in a separate thread. Used in runTrackFromVideo() function.
780  ///
781  /// Expects one argument sent via e parameter: path to the video file. Demonstrates handling of video files and video frame data retrieval (using OpenCV) and
782  /// implements synchronization to the video FPS. Tracks and analyses face in video frames. Depending on the tracker return status runs face analysis
783  /// and face recognition in a separate thread.
784  ///
785  /// Locking mechanism is used to synchronize frame and tracking results rendering. Due face analysis and face recognition are running in separate thread
786  /// rendering of age, gender, emotion and identity is not synchronized with the rendering of the frame.
787  ///
788  /// Implemented using <see cref="BackgroundWorker"/>.
789  /// </summary>
790  /// <param name="sender"></param>
791  /// <param name="e"></param>
792  private void worker_DoWorkVideo(object sender, DoWorkEventArgs e)
793  {
794  BackgroundWorker worker = sender as BackgroundWorker;
795  string file = (string)e.Argument;
796 
797  // Video capture and frame data initialization
798  Capture capture = new Capture(file);
799  Bitmap bmp = new Bitmap(capture.QueryFrame().Bitmap);
800  int bitmapStride = 0;
801 
802  //Age estimation enabled by default
803  Dispatcher.BeginInvoke((Action)(() => { enableAgeEstimCheckbox(); }));
804 
805  System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
806  stopwatch.Start();
807 
808  bool video_file_sync = true;
809  float frameTime = 1000 / (float)CvInvoke.cveVideoCaptureGet(capture, Emgu.CV.CvEnum.CapProp.Fps);
810  int frameCount = 0;
811 
812  //Clear FA variables
813  lock (lockFaceData)
814  {
816  }
817 
818  while (true)
819  {
820  if (worker.CancellationPending)
821  {
822  isTracked = false;
823  e.Cancel = true;
824  capture = null;
825  break;
826  }
827  else
828  {
829  long currentTime = stopwatch.ElapsedMilliseconds;
830 
831  while (video_file_sync && currentTime > frameTime * (frameCount + 1)) // skip frames if track() is too slow
832  {
833  frameCount++;
834  currentTime = stopwatch.ElapsedMilliseconds;
835  capture.Grab();
836  }
837 
838  while (video_file_sync && currentTime < frameTime * (frameCount - 5)) // wait if track() is too fast
839  {
840  System.Threading.Thread.Sleep(1);
841  currentTime = stopwatch.ElapsedMilliseconds;
842  }
843 
844  // Frame grabbing
845  try
846  {
847  bmp = capture.QueryFrame().Bitmap;
848 
849  if (ACTIVE_MIRROR == true)
850  bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
851  }
852  catch (NullReferenceException)
853  {
854  worker.CancelAsync();
855  }
856 
857  frameCount++;
858 
859  int format = (int)VISAGE_FORMAT.BGR;
860  int nChannels = 3;
861  switch (bmp.PixelFormat)
862  {
863  case PixelFormat.Format24bppRgb:
864  format = (int)VISAGE_FORMAT.BGR;
865  nChannels = 3;
866  break;
867  case PixelFormat.Format8bppIndexed:
868  format = (int)VISAGE_FORMAT.LUMINANCE;
869  nChannels = 1;
870  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
871  break;
872  case PixelFormat.Format32bppRgb:
873  format = (int)VISAGE_FORMAT.BGRA;
874  nChannels = 4;
875  Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
876  break;
877  default:
878  format = -1;
879  break;
880  }
881 
882  FaceData[] analysisData = new FaceData[maxFacesTracker];
883 
884  lock (lockFaceData)
885  {
886  //Create a VSImage and fill it with data to pass it to the track function
887  frame = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
888  byte[] bmp_byte = BitmapToByteArray(bmp, ref bitmapStride);
889  frame.imageData = bmp_byte;
890 
891  byte[] bytedataRGB = new byte[bmp_byte.Length];
892 
893  convertFromBGRToRGB(ref bytedataRGB, ref bmp_byte);
894 
895  frameRGB = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
896  frameRGB.imageData = bytedataRGB;
897 
898  pureTrackStopwatch.Start();
899  startTime = pureTrackStopwatch.ElapsedMilliseconds;
900  gTrackerStatus = gVisageTracker.Track(bmp.Width, bmp.Height, bmp_byte, gFaceData, format, (int)VISAGE_ORIGIN.TL, bitmapStride, (int)(frameTime*frameCount), maxFacesTracker);
901  pureTrackStopwatch.Stop();
902  endTime = pureTrackStopwatch.ElapsedMilliseconds;
903  elapsedTime = endTime - startTime;
904 
905  //While locked, copy face data from the tracker to face data array for face analysis and recognition
906  for (int i = 0; i < maxFacesTracker; ++i)
907  {
908  analysisData[i] = new FaceData(gFaceData[i]);
909  }
910 
911  isTracked = true;
912  }
913 
914  //Run face analysis in a separate thread
915  runFaceAnalysis(frameRGB, analysisData, gTrackerStatus);
916 
917  //Run face recognition in a separate thread
918  runFaceRecognition(frameRGB, analysisData, gTrackerStatus);
919  }
920  }
921  //Stop the tracker
922  gVisageTracker.Stop();
923  }
924 
925  /// <summary>
926  /// Worker event for face recognition that runs in a separate thread. Used in runFaceRecognition() function.
927  ///
928  /// Expects three arguments sent via e parameter: frame (VsImage), array of face data objects (FaceData[]) and array of tracking statuses (int[].
929  ///
930  /// Calls RecognizeFace function for each face in the frame and passes frame and corresponding FaceData object.
931  /// </summary>
932  /// <param name="sender"></param>
933  /// <param name="e"></param>
934  private void worker_DoWorkMatchFace(object sender, DoWorkEventArgs e)
935  {
936  List<object> argumentList = e.Argument as List<object>;
937  //read and cast the parameters
938  VSImage frame = (VSImage)argumentList[0];
939  FaceData[] faceDatas = (FaceData[])argumentList[1];
940  int[] trackerStatus = (int[])argumentList[2];
941 
942  //recognize face if the tracker is tracking, otherwise, if tracker state has changed, reset face recognition initialization buffers
943  for (int i = 0; i < maxFacesTracker; ++i)
944  {
945  if (trackerStatus[i] == (int)TRACK_STAT.OK && gVisageRendering.withinConstraints(ref faceDatas[i]))
946  {
947  RecognizeFace(frame, faceDatas[i], i);
948  }
949  else
950  {
952  }
953  }
954 
955  }
956 
957  /// <summary>
958  /// Worker event for face analysis that runs in a separate thread. Used in runFaceAnalysis() function.
959  ///
960  /// Expects three arguments sent via e parameter: frame (VsImage), face data array object (FaceData[]) and array of tracking statuses (int[]).
961  ///
962  /// Calls AnalyzeFace function for each face in the frame and passes frame and corresponding FaceData object.
963  /// </summary>
964  /// <param name="sender"></param>
965  /// <param name="e"></param>
966  private void worker_DoAnalyzeFace(object sender, DoWorkEventArgs e)
967  {
968  List<object> argumentList = e.Argument as List<object>;
969  //read and cast the parameters
970  VSImage frame = (VSImage)argumentList[0];
971  FaceData[] faceData = (FaceData[])argumentList[1];
972  int[] trackerStatus = (int[])argumentList[2];
973 
974  //analyze face if the tracker is tracking, otherwise, if tracker state has changed, reset buffers
975  for (int i = 0; i < maxFacesTracker; ++i)
976  {
977  if (trackerStatus[i] == (int)TRACK_STAT.OK)
978  {
979  if (gEmotionsSmoothed[i] == default(float[])) gEmotionsSmoothed[i] = new float[NUM_EMOTIONS];
980  AnalyzeFace(frame, faceData[i], i);
981  }
982  else
983  {
985  }
986  }
987  }
988 
989  /// <summary>
990  /// Performs face analysis on an image based on the input face data object. Used in worker_DoAnalyzeFace() function.
991  ///
992  /// Performs age, gender or emotion analysis depending on the check-boxes selected and which features of the VisageFaceAnalyser are available.
993  /// </summary>
994  /// <param name="frame">Input frame on which the face analysis is performed.</param>
995  /// <param name="faceData">Input FaceData object on which the face analysis is performed.</param>
996  /// <param name="index">The corresponding index of the FaceData object in the FaceData array matching the index of the age, gender array or emotion list.</param>
997  ///
998  private void AnalyzeFace(VSImage frame, FaceData faceData, int index = 0)
999  {
1000  VFAReturnCode status;
1001  AnalysisData analysisResults = new AnalysisData();
1002 
1003  if ((FA_AGE_CHECKED || FA_GEN_CHECKED || FA_EMO_CHECKED))
1004  {
1005  int options = 0;
1006 
1007  if (FA_AGE_CHECKED) options |= (int)VFAFlags.AGE;
1008  if (FA_GEN_CHECKED) options |= (int)VFAFlags.GENDER;
1009  if (FA_EMO_CHECKED) options |= (int)VFAFlags.EMOTION;
1010 
1011  if ((vfaInitialised & options) == options)
1012  {
1013  if (activeVisageMode == VISAGE_MODE.TRACKER)
1014  {
1015  status = gVisageFaceAnalyser.analyseStream(frame, faceData, (int)options, ref analysisResults, index);
1016 
1017  if (status == VFAReturnCode.NO_ERROR)
1018  {
1019  if (analysisResults.ageValid)
1020  {
1021  gAgeSmoothed[index] = analysisResults.age;
1022  }
1023  if (analysisResults.genderValid)
1024  {
1025  gGenderSmoothed[index] = analysisResults.gender;
1026  }
1027  if (analysisResults.emotionsValid)
1028  {
1029  gEmotionsSmoothed[index] = analysisResults.emotionProbabilities;
1030  }
1031  }
1032  }
1033  else if (activeVisageMode == VISAGE_MODE.DETECTOR)
1034  {
1035 
1036  status = gVisageFaceAnalyser.analyseImage(frame, faceData, (int)options, ref analysisResults);
1037 
1038  if (status == VFAReturnCode.NO_ERROR)
1039  {
1040  if (analysisResults.ageValid)
1041  {
1042  gAgeArray[index] = analysisResults.age;
1043  }
1044  if (analysisResults.genderValid)
1045  {
1046  gGenderArray[index] = analysisResults.gender;
1047  }
1048  if (analysisResults.emotionsValid)
1049  {
1050  gEmotionList[index] = new float[NUM_EMOTIONS];
1051  gEmotionList[index] = analysisResults.emotionProbabilities;
1052  }
1053  }
1054  }
1055  }
1056  }
1057  }
1058 
1059  /// <summary>
1060  /// Performs face recognition on an image based on the input face data array. Used in worker_DoWorkMatchFace() function when INPUT_MODE is set to
1061  /// CAMERA or IMAGE or can be called separately.
1062  ///
1063  /// Implements additional face recognition logic and descriptor clustering.
1064  /// </summary>
1065  /// <param name="frame">Input frame on which the face recognition is performed.</param>
1066  /// <param name="faceDatas">Input FaceData object on which the face recognition is performed.</param>
1067  /// <param name="index">The corresponding index of the FaceData object in the FaceData array.</param>
1068  ///
1069  private void RecognizeFace(VSImage frame, FaceData faceDatas, int index = 0)
1070  {
1071  //Activate lock - wait here until clear gallery event has finished
1072  lock (lockClearRecognition)
1073  {
1074  int DESCRIPTOR_SIZE;
1075 
1076  //Proceed with face recognition only if VisageFaceRecognition has initialized successfully
1077  //Save the descriptor size for later
1078  if (!gVisageFaceRecognition.IsInitialized)
1079  return;
1080  else
1081  DESCRIPTOR_SIZE = gVisageFaceRecognition.GetDescriptorSize();
1082 
1083  //If face recognition is enabled and adding descriptors to the gallery is enabled
1084  if (FR_CHECKED && !freeze_gallery)
1085  {
1086  //Fetch the current number of descriptors
1087  int count = gVisageFaceRecognition.GetDescriptorCount();
1088 
1089  short[] descriptor = new short[DESCRIPTOR_SIZE];
1090 
1091  //Extract a face descriptor from the face in the current frame
1092  int extract_descriptor_status = gVisageFaceRecognition.ExtractDescriptor(faceDatas, frame, descriptor);
1093 
1094  //Check if still in initialization phase for [index] face
1095  if (numInitFrames[index] < numInitFramesThreshold)
1096  {
1097  currentDisplayName[index] = "?";
1098 
1099  if (extract_descriptor_status == 1)
1100  {
1101  numInitFrames[index] += 1;
1102 
1103  //Add descriptor to the buffer for face index
1104  descBuffer[index].Add(descriptor);
1105  }
1106  else
1107  currentDisplayName[index] = "-";
1108 
1109  //Check if sufficient number of face descriptors has been collected if it has add them all to the gallery with the unique ID - Person #
1110  if (numInitFrames[index] >= numInitFramesThreshold)
1111  {
1112  for (int i = 0; i < numInitFramesThreshold; i++)
1113  {
1114  gVisageFaceRecognition.AddDescriptor(descBuffer[index][i], "Person" + numPersons);
1115  }
1116 
1117  //Clear buffer after descriptors have been added to the gallery
1118  descBuffer[index].Clear();
1119 
1120  gUniqueNames.Add("Person" + numPersons);
1121  indices_to_names[numPersons] = "Person" + numPersons;
1122  numPersons += 1;
1123  Dispatcher.BeginInvoke((Action)(() =>
1124  { FillNames(); }));
1125  }
1126 
1127  }
1128 
1129  //Check if the initialization phase is complete
1130  else
1131  {
1132  float[] sim = new float[count];
1133  string[] names = new string[count];
1134 
1135  //Compare the descriptor to all the descriptors in the gallery (count)
1136  int recognize_status = 0;
1137  if (extract_descriptor_status == 1)
1138  recognize_status = gVisageFaceRecognition.Recognize(descriptor, count, ref names, sim);
1139 
1140  //If face recognition was successful and the recognized face is recognized with high enough similarity, display the name
1141  if (recognize_status > 0 && extract_descriptor_status == 1 && sim[0] > display_threshold)
1142  {
1143  currentDisplayName[index] = names[0];
1144  numFramesNewIdentity[index] = 0;
1145  }
1146  //Case were extract descriptor fails, for example when on edge of the screen, keep the name and do not collect new descriptors
1147  else if (extract_descriptor_status == 0)
1148  {
1149  currentDisplayName[index] = "-";
1150  numFramesNewIdentity[index] = 0;
1151  }
1152  //Otherwise keep count on how many frames in a row similarity has not been high enough
1153  //If the number is above threshold we conclude that there is a new face in the frame and we go to the initialisation phase again
1154  else
1155  {
1156  if (numFramesNewIdentity[index] / (float)newIdentityFramesThreshold > 0)
1157  currentDisplayName[index] = "?";
1158  else if (recognize_status > 0 && sim[0] > display_threshold)
1159  currentDisplayName[index] = names[0];
1160  else
1161  currentDisplayName[index] = "?";
1162 
1163  numFramesNewIdentity[index] += 1;
1164 
1165  if (numFramesNewIdentity[index] > newIdentityFramesThreshold)
1166  {
1167  numInitFrames[index] = 0;
1168  numFramesNewIdentity[index] = 0;
1169  }
1170 
1171  }
1172  }
1173  }
1174  //If face recognition is enabled and adding descriptors to the gallery is disabled
1175  if (FR_CHECKED && freeze_gallery)
1176  {
1177  int count = gVisageFaceRecognition.GetDescriptorCount();
1178 
1179  //Delete descriptors for identities that are not fully processed
1180  if (!(numInitFrames[index] == 0 || numInitFrames[index] == numInitFramesThreshold))
1181  {
1182  descBuffer[index].Clear();
1183  numInitFrames[index] = 0;
1184  }
1185 
1186  count = gVisageFaceRecognition.GetDescriptorCount();
1187 
1188  //If we are not allowed to add to the gallery just extract descriptor and compare it to all the ones that are currently in the gallery
1189  if (count > 0)
1190  {
1191 
1192  short[] descriptor = new short[DESCRIPTOR_SIZE];
1193  float[] sim = new float[count];
1194  string[] names = new string[count];
1195 
1196  gVisageFaceRecognition.ExtractDescriptor(faceDatas, frame, descriptor);
1197 
1198  gVisageFaceRecognition.Recognize(descriptor, count, ref names, sim);
1199 
1200  if (names.Count() > 0 && sim[0] > display_threshold)
1201  currentDisplayName[index] = names[0];
1202  else
1203  currentDisplayName[index] = "?";
1204 
1205  }
1206  else
1207  currentDisplayName[index] = "?";
1208 
1209  }
1210  }
1211  }
1212 
1213  #endregion
1214 
1215 
1216 
1217  #region OpenGL Tracker And Detector Display
1218 
1219  /// <summary>
1220  /// Handles the OpenGLDraw event of the openGLControl control.
1221  ///
1222  /// Locking mechanism is used to synchronize frame and tracking results rendering (lockFaceData).
1223  /// </summary>
1224  /// <param name="sender">The source of the event.</param>
1225  /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
1226  ///
1227  private void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
1228  {
1229  lock (lockFaceData)
1230  {
1231  if (isTracked == true && gTrackerStatus[0] != 0)
1232  {
1233  isTracked = false;
1234 
1235  if (activeVisageMode == VISAGE_MODE.TRACKER && gFaceData != null)
1236  {
1237  trackerDisplay();
1238  }
1239  }
1240  }
1241 
1242  if (isDetected == true)
1243  {
1244  if (activeVisageMode == VISAGE_MODE.DETECTOR && dataArray != null)
1245  {
1246  inDrawDetection = true;
1247  detectorDisplay();
1248  inDrawDetection = false;
1249  }
1250  }
1251  }
1252 
1253  /// <summary>
1254  /// Calls the VisageRendering' DisplayResults function.
1255  ///
1256  /// Called in a separate thread controlled by openGLControl_OpenGLDraw() function when the active application mode is set to TRACKER mode.
1257  /// </summary>
1258  private void trackerDisplay()
1259  {
1260  cbFrame.IsEnabled = true;
1261 
1262  int winWidth = (int)openGLControl.ActualWidth;
1263  int winHeight = (int)openGLControl.ActualHeight;
1264 
1265  AdjustAspect(ref gFaceData[0], ref winWidth, ref winHeight);
1266 
1267  gl.Viewport(0, 0, winWidth, winHeight); // Reset The Current Viewport
1268 
1269  // clear the frame buffer before next drawing
1270  gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_COLOR_BUFFER_BIT);
1271 
1272  lFPS.Text = "Tracker FPS: " + gFaceData[0].FrameRate.ToString("n2");
1273  lPureTrackTime.Text = "Track time: " + elapsedTime.ToString() + " ms";
1274  //var b = gFaceData.FaceRotation.Select(x => { return Math.Round(x, 2); }).ToArray();
1275  lTranslation.Text = String.Format("Translation: {0}, {1}, {2}",
1276  Math.Round(gFaceData[0].FaceTranslation[0], 2), Math.Round(gFaceData[0].FaceTranslation[1], 2), Math.Round(gFaceData[0].FaceTranslation[2], 2));
1277  lRotation.Text = String.Format("Rotation: {0}, {1}, {2}",
1278  Math.Round(VisageRendering.VS_RAD2DEG(gFaceData[0].FaceRotation[0]), 2),
1279  Math.Round(VisageRendering.VS_RAD2DEG(gFaceData[0].FaceRotation[1]), 2),
1280  Math.Round(VisageRendering.VS_RAD2DEG(gFaceData[0].FaceRotation[2]), 2));
1281  lStatus.Text = "Status: " + GetTrackerStatusString();
1282 
1283  //Display frame separately from tracking results
1284  if ((bool)cbFrame.IsChecked)
1285  {
1286  gVisageRendering.DisplayFrame(frame, winWidth, winHeight);
1287  }
1288 
1289  //For every tracked face, display tracking and analysis results without displaying the frame (^)
1290  for (int i = 0; i < maxFacesTracker; ++i)
1291  {
1292  gVisageRendering.DisplayResults(frame, gFaceData[i], gTrackerStatus[i], winWidth, winHeight, ACTIVE_DISPLAY ^ VisageRendering.DISPLAY_FRAME, gEmotionsSmoothed[i], gGenderSmoothed[i], gAgeSmoothed[i], currentDisplayName[i]);
1293  }
1294  }
1295 
1296  /// <summary>
1297  /// Calls the Calls the VisageRendering' DisplayResults function for every detected face.
1298  ///
1299  /// Called in a separate thread controlled by openGLControl_OpenGLDraw() function when the active application mode is set to DETECTOR mode.
1300  /// </summary>
1301  private void detectorDisplay()
1302  {
1303  if (isDetected)
1304  {
1305  int winWidth = (int)openGLControl.ActualWidth;
1306  int winHeight = (int)openGLControl.ActualHeight;
1307 
1308  AdjustAspect(ref dataArray[0], ref winWidth, ref winHeight);
1309 
1310  //Reset the current viewport
1311  gl.Viewport(0, 0, winWidth, winHeight);
1312 
1313  //Clear the frame buffer before next drawing
1314  gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_COLOR_BUFFER_BIT);
1315 
1316  lFPS.Text = "";
1317  lPureTrackTime.Text = "";
1318  lTranslation.Text = "";
1319  lRotation.Text = "";
1320  lStatus.Text = "";
1321 
1322  //Display frame separately from detection results
1323  gVisageRendering.DisplayFrame(frame, winWidth, winHeight);
1324 
1325  //For every detected face, display detection and analysis results without displaying the frame (^)
1326  for (int i = 0; i < numFaces; i++)
1327  {
1328  gVisageRendering.DisplayResults(frame, dataArray[i], 1, winWidth, winHeight, ACTIVE_DISPLAY ^ VisageRendering.DISPLAY_FRAME, gEmotionList[i], gGenderArray[i], gAgeArray[i], currentDisplayName[i]);
1329  }
1330 
1331  if (numFaces == 0)
1332  {
1333  gl.DrawText(winWidth / 2, winHeight / 2, 1.0f, 0.0f, 0.0f, "Arial", 25, "Could not detect face");
1334  }
1335  }
1336  }
1337 
1338  private string GetTrackerStatusString()
1339  {
1340  string s = "";
1341 
1342  switch (gTrackerStatus[0])
1343  {
1344  case (int)TRACK_STAT.OFF:
1345  s = "OFF";
1346  break;
1347  case (int)TRACK_STAT.INIT:
1348  s = "INIT";
1349  break;
1350  case (int)TRACK_STAT.OK:
1351  s = "OK";
1352  break;
1353  case (int)TRACK_STAT.RECOVERING:
1354  s = "RECOVERING";
1355  break;
1356  }
1357 
1358  return s;
1359  }
1360 
1361  #endregion
1362 
1363 
1364  #region BackgroundWorker Control
1365 
1366  /// <summary>
1367  /// Demonstrates running a new tracking thread for tracking from video.
1368  ///
1369  /// The function is called by clicking on the Track from video button.
1370  /// </summary>
1371  /// <param name="file">Video on which the tracking will be performed.</param>
1372  private void runTrackFromVideo(string file)
1373  {
1374  //Disable rendering until worker starts
1375  isTracked = false;
1376 
1377  //Check if worker exists and if it is currently working, if yes cancel worker
1378  if (worker != null)
1379  {
1380  //
1381  if (worker.IsBusy)
1382  {
1383  worker.CancelAsync();
1384  }
1385 
1386  //Wait for the worker to exit before running a new one
1387  while (worker.IsBusy)
1388  {
1389  Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1390  new Action(delegate { }));
1391  }
1392  }
1393 
1394  //Change the input mode to video mode
1395  activeInputMode = INPUT_MODE.VIDEO;
1396 
1397  //Start tracking from video in another thread
1398  worker = new BackgroundWorker();
1399  worker.WorkerSupportsCancellation = true;
1400  worker.DoWork += worker_DoWorkVideo;
1401  worker.RunWorkerAsync(file);
1402  }
1403 
1404  /// <summary>
1405  /// Demonstrates running a new tracking thread for tracking from image.
1406  ///
1407  /// The function is called by clicking on the Track from image icon.
1408  /// </summary>
1409  /// <param name="file">Image on which the tracking will be performed.</param>
1410  /// <param name="activeMode">Current active mode. Can be DETECTOR or TRACKER.</param>
1411  private void runTrackFromImage(string file, VISAGE_MODE activeMode)
1412  {
1413  //Disable rendering until worker starts
1414  isTracked = false;
1415 
1416  //Check if worker exists and if it is currently working, if yes cancel worker
1417  if (worker != null)
1418  {
1419  if (worker.IsBusy)
1420  {
1421  worker.CancelAsync();
1422  }
1423 
1424  //Wait for the worker to exit before running a new one
1425  while (worker.IsBusy)
1426  {
1427  Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1428  new Action(delegate { }));
1429  }
1430  }
1431 
1432  //Change the input mode to image mode
1433  activeInputMode = INPUT_MODE.IMAGE;
1434 
1435  //Start tracking from image in another thread
1436  worker = new BackgroundWorker();
1437  worker.WorkerSupportsCancellation = true;
1438  List<object> arguments = new List<object>();
1439  arguments.Add(file);
1440  arguments.Add(activeMode);
1441  worker.DoWork += worker_DoWorkImage;
1442  worker.RunWorkerAsync(arguments);
1443  }
1444 
1445  /// <summary>
1446  /// Demonstrates running a new tracking thread for tracking from camera.
1447  ///
1448  /// The function is called by clicking on the Track from camera icon.
1449  /// </summary>
1450  private void runTrackFromCam()
1451  {
1452  //Disable rendering until worker starts
1453  isTracked = false;
1454 
1455  //Check if worker exists and if it is currently working, if yes cancel worker
1456  if (worker != null)
1457  {
1458  if (worker.IsBusy)
1459  {
1460  worker.CancelAsync();
1461  }
1462 
1463  //Wait for the worker to exit before running a new one
1464  while (worker.IsBusy)
1465  {
1466  Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1467  new Action(delegate { }));
1468  }
1469  }
1470 
1471  //Change the input mode to camera mode
1472  activeInputMode = INPUT_MODE.CAMERA;
1473 
1474  //Start tracking from camera in another thread
1475  worker = new BackgroundWorker();
1476  worker.WorkerSupportsCancellation = true;
1477  worker.DoWork += worker_DoWorkCam;
1478  worker.RunWorkerAsync();
1479  }
1480 
1481  /// <summary>
1482  /// Demonstrates running face recognition in a background thread. In case the worker is busy the function returns effectively skipping that frame.
1483  /// </summary>
1484  /// <param name="frame">Input frame on which the recognition is made</param>
1485  /// <param name="faceDatas">Input face data array</param>
1486  /// <param name="trackerStatus">tracker status array</param>
1487  private void runFaceRecognition(VSImage frame, FaceData[] faceDatas, int[] trackerStatus)
1488  {
1489  if (!(FR_CHECKED))
1490  return;
1491 
1492  //Check if worker exists and if it is currently working
1493  if (fr_worker != null)
1494  {
1495  if (fr_worker.IsBusy)
1496  {
1497  return;
1498  }
1499  }
1500  else
1501  {
1502  fr_worker = new BackgroundWorker();
1503  fr_worker.DoWork += worker_DoWorkMatchFace;
1504  fr_worker.WorkerSupportsCancellation = true;
1505  }
1506 
1507  //Start tracking from image in another thread
1508  List<object> arguments = new List<object>();
1509  arguments.Add(frame);
1510  arguments.Add(faceDatas);
1511  arguments.Add(trackerStatus);
1512  fr_worker.RunWorkerAsync(arguments);
1513  }
1514 
1515  /// <summary>
1516  /// Demonstrates running face analysis in a background thread. In case the worker is busy the function returns effectively skipping that frame.
1517  /// </summary>
1518  /// <param name="frame">Input frame on which the analysis is made</param>
1519  /// <param name="faceData">Input face data array</param>
1520  /// <param name="trackerStatus">tracker status array</param>
1521  private void runFaceAnalysis(VSImage frame, FaceData[] faceData, int[] trackerStatus)
1522  {
1523  if (!(FA_AGE_CHECKED || FA_GEN_CHECKED || FA_EMO_CHECKED))
1524  return;
1525 
1526  //Check if worker exists and if it is currently working
1527  if (fa_worker != null)
1528  {
1529  if (fa_worker.IsBusy)
1530  {
1531  return;
1532  }
1533  }
1534  else
1535  {
1536  fa_worker = new BackgroundWorker();
1537  fa_worker.DoWork += worker_DoAnalyzeFace;
1538  fa_worker.WorkerSupportsCancellation = true;
1539  }
1540 
1541  //Start tracking from image in another thread
1542  List<object> arguments = new List<object>();
1543  arguments.Add(frame);
1544  arguments.Add(faceData);
1545  arguments.Add(trackerStatus);
1546  fa_worker.RunWorkerAsync(arguments);
1547  }
1548  #endregion
1549 
1550 
1551  #region Style Control
1552 
1553  /// <summary>
1554  /// Change button style (camera, video or image buttons) depending on the chosen INPUT_MODE
1555  /// </summary>
1556  /// <param name="mode">chosen INPUT_MODE</param>
1557  private void changeButtonStyle(INPUT_MODE mode)
1558  {
1559  camButton.Content = FindResource("CameraPassive");
1560  videoButton.Content = FindResource("VideoPassive");
1561  imageButton.Content = FindResource("ImagePassive");
1562 
1563  lCamera.Foreground = (System.Windows.Media.Brush)FindResource("Passive");
1564  lVideo.Foreground = (System.Windows.Media.Brush)FindResource("Passive");
1565  lImage.Foreground = (System.Windows.Media.Brush)FindResource("Passive");
1566 
1567  if (mode == INPUT_MODE.CAMERA)
1568  {
1569  camButton.Content = FindResource("CameraActive");
1570  lCamera.Foreground = (System.Windows.Media.Brush)FindResource("Active");
1571  }
1572  else if (mode == INPUT_MODE.VIDEO)
1573  {
1574  videoButton.Content = FindResource("VideoActive");
1575  lVideo.Foreground = (System.Windows.Media.Brush)FindResource("Active");
1576  }
1577  else if (mode == INPUT_MODE.IMAGE)
1578  {
1579  imageButton.Content = FindResource("ImageActive");
1580  lImage.Foreground = (System.Windows.Media.Brush)FindResource("Active");
1581  }
1582  }
1583 
1584  /// <summary>
1585  /// Change radio button style (configuration choice) depending on the chose configuration
1586  /// </summary>
1587  /// <param name="sender">sender object from the click event</param>
1588  private void changeConfStyle(object sender)
1589  {
1590  rbFFTHigh.Foreground = (System.Windows.Media.Brush)FindResource("Passive");
1591  rbFFTLow.Foreground = (System.Windows.Media.Brush)FindResource("Passive");
1592 
1593  (sender as RadioButton).Foreground = (System.Windows.Media.Brush)FindResource((sender as RadioButton).Foreground == (System.Windows.Media.Brush)FindResource("Active") ? "Passive" : "Active");
1594  }
1595 
1596  #endregion
1597 
1598 
1599  #region Button Control
1600 
1601  private void camButton_Click(object sender, RoutedEventArgs e)
1602  {
1603  runTrackFromCam();
1604 
1605  //Set style
1606  changeButtonStyle(INPUT_MODE.CAMERA);
1607  }
1608 
1609  private void videoButton_Click(object sender, RoutedEventArgs e)
1610  {
1611  // File dialog
1612  Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
1613  dlg.InitialDirectory = System.IO.Path.GetFullPath(videoInitialDir);
1614 
1615  dlg.DefaultExt = ".mp4";
1616  dlg.Filter = "Video Files|*.mp4;*.avi";
1617 
1618  Nullable<bool> result = dlg.ShowDialog();
1619 
1620  if (result != true)
1621  {
1622  return;
1623  }
1624 
1625  videoInitialDir = System.IO.Path.GetDirectoryName(dlg.FileName);
1626  currentVideo = dlg.FileName;
1627  runTrackFromVideo(currentVideo);
1628 
1629  //Set style
1631  }
1632 
1633  private void imageButton_Click(object sender, RoutedEventArgs e)
1634  {
1635  // File dialog
1636  Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
1637 
1638  // Set filter for file extension and default file extension
1639  dlg.DefaultExt = ".jpg";
1640  dlg.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif| BMP Files (*.bmp)|*.bmp";
1641  dlg.InitialDirectory = System.IO.Path.GetFullPath(imageInitialDir);
1642 
1643  // Display OpenFileDialog by calling ShowDialog method
1644  Nullable<bool> result = dlg.ShowDialog();
1645 
1646  // Check if file is chosen
1647  if (result != true)
1648  {
1649  return;
1650  }
1651 
1653 
1654  imageInitialDir = System.IO.Path.GetDirectoryName(dlg.FileName);
1655  currentImage = dlg.FileName;
1656  runTrackFromImage(currentImage, activeVisageMode);
1657 
1658  //Set style
1660  }
1661 
1662  /// <summary>
1663  /// Toggles application mode from TRACKER to DETECTOR and vice versa.
1664  /// </summary>
1665  /// <param name="sender"></param>
1666  /// <param name="e"></param>
1667  private void trackerDetectorButton_Click(object sender, RoutedEventArgs e)
1668  {
1669  if (activeVisageMode == VISAGE_MODE.TRACKER)
1670  {
1671  //toggle detector mode
1672  activeVisageMode = VISAGE_MODE.DETECTOR;
1673  //toggle freeze gallery button
1675  freeze_gallery = true;
1676  cbFreeze.IsChecked = true;
1677  //
1678  manageControls(activeVisageMode);
1679  trackerDetectorButton.Content = "Switch to Tracker";
1680  // File dialog
1681  Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
1682 
1683  // Set filter for file extension and default file extension
1684  dlg.DefaultExt = ".jpg";
1685  dlg.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif| BMP Files (*.bmp)|*.bmp";
1686 
1687  // Display OpenFileDialog by calling ShowDialog method
1688  Nullable<bool> result = dlg.ShowDialog();
1689 
1690  // Check if file is chosen
1691  if (result != true)
1692  {
1693  return;
1694  }
1695  currentImage = dlg.FileName;
1696  runTrackFromImage(currentImage, activeVisageMode);
1697 
1698  }
1699 
1700  else if (activeVisageMode == VISAGE_MODE.DETECTOR)
1701  {
1702  //
1704  //toggle freeze gallery button
1705  freeze_gallery = false;
1706  cbFreeze.IsChecked = false;
1707  //toggle tracker mode
1708  activeVisageMode = VISAGE_MODE.TRACKER;
1709  manageControls(activeVisageMode);
1710  trackerDetectorButton.Content = "Switch to Detector";
1711  dataArray = null;
1712  numFaces = 0;
1713  runTrackFromCam();
1714  }
1715  }
1716 
1717  private void cameraSettingsButton_Click(object sender, RoutedEventArgs e)
1718  {
1719  if (cameraCapture != null)
1720  cameraCapture.SetCaptureProperty(CapProp.Settings, 0);
1721  }
1722 
1723  private void cbCameraChoice_SelectionChanged(object sender, SelectionChangedEventArgs e)
1724  {
1725  currentCameraDevice = (sender as ComboBox).SelectedIndex;
1726  runTrackFromCam();
1727  //Set style
1728  changeButtonStyle(INPUT_MODE.CAMERA);
1729  }
1730 
1731  /// <summary>
1732  /// Enable age estimation checkbox and change to appropriate color.
1733  /// </summary>
1734  private void enableAgeEstimCheckbox()
1735  {
1736  cbAge.IsEnabled = true;
1737  cbAge.ToolTip = null;
1738  if (cbAge.Foreground == ((System.Windows.Media.Brush)FindResource("Disabled")))
1739  {
1740  cbAge.Foreground = ((System.Windows.Media.Brush)FindResource("Passive"));
1741  }
1742  }
1743 
1744  /// <summary>
1745  /// Disable age estimation checkbox and change color for the use case when one channel or four channel is loaded
1746  /// (age estimation is supported for 3 channel images only) or when in Detector mode
1747  /// </summary>
1749  {
1750  cbAge.IsEnabled = false;
1751  cbAge.IsChecked = false;
1752  ToolTip t = new ToolTip { Content = "Age estimation disabled for images with one or four channels, or when in Detector mode." };
1753  cbAge.ToolTip = t;
1754  cbAge.Foreground = ((System.Windows.Media.Brush)FindResource("Disabled"));
1755  }
1756 
1757  /// <summary>
1758  /// Enable emotion estimation checkbox and change to appropriate color.
1759  /// </summary>
1761  {
1762  cbEmotion.IsEnabled = true;
1763  cbEmotion.ToolTip = null;
1764  if (cbEmotion.Foreground == ((System.Windows.Media.Brush)FindResource("Disabled")))
1765  {
1766  cbEmotion.Foreground = ((System.Windows.Media.Brush)FindResource("Passive"));
1767  }
1768  }
1769 
1770  /// <summary>
1771  /// Disable emotion estimation checkbox and change color for the use case when in Detector mode
1772  /// </summary>
1774  {
1775  cbEmotion.IsEnabled = false;
1776  cbEmotion.IsChecked = false;
1777  ToolTip t = new ToolTip { Content = "Emotion estimation disabled in Detector Mode." };
1778  cbEmotion.ToolTip = t;
1779  cbEmotion.Foreground = ((System.Windows.Media.Brush)FindResource("Disabled"));
1780  }
1781 
1782  /// <summary>
1783  /// Enable gender estimation checkbox and change to appropriate color.
1784  /// </summary>
1786  {
1787  cbGender.IsEnabled = true;
1788  cbGender.ToolTip = null;
1789  if (cbGender.Foreground == ((System.Windows.Media.Brush)FindResource("Disabled")))
1790  {
1791  cbGender.Foreground = ((System.Windows.Media.Brush)FindResource("Passive"));
1792  }
1793  }
1794 
1795  /// <summary>
1796  /// Disable gender estimation checkbox and change color for the use case when in Detector mode
1797  /// </summary>
1799  {
1800  cbGender.IsEnabled = false;
1801  cbGender.IsChecked = false;
1802  ToolTip t = new ToolTip { Content = "Gender estimation disabled in Detector Mode." };
1803  cbGender.ToolTip = t;
1804  cbGender.Foreground = ((System.Windows.Media.Brush)FindResource("Disabled"));
1805  }
1806 
1807  /// <summary>
1808  /// Enable Ears checkbox and change color for the use case when not in Detector mode
1809  /// </summary>
1810  private void enableEarsCheckbox()
1811  {
1812  cbEars.IsEnabled = true;
1813  cbEars.ToolTip = null;
1814  if (cbEars.Foreground == ((System.Windows.Media.Brush)FindResource("Disabled")))
1815  {
1816  cbEars.Foreground = ((System.Windows.Media.Brush)FindResource("Passive"));
1817  }
1818  }
1819 
1820  /// <summary>
1821  /// Disable Ears checkbox and change color for the use case when in Detector mode
1822  /// </summary>
1823  private void disableEarsCheckbox()
1824  {
1825  cbEars.IsEnabled = false;
1826  cbEars.IsChecked = false;
1827  ToolTip t = new ToolTip { Content = "Ears Tracking disabled in Detector Mode." };
1828  cbEars.ToolTip = t;
1829  cbEars.Foreground = ((System.Windows.Media.Brush)FindResource("Disabled"));
1830  }
1831  #endregion
1832 
1833 
1834  #region Recognition Controls
1835 
1836  /// <summary>
1837  /// Load face recognition gallery (.frg) from file.
1838  ///
1839  /// Opens a dialog to load the gallery (.frg) file.
1840  /// </summary>
1841  /// <param name="sender"></param>
1842  /// <param name="e"></param>
1843  private void loadGalleryButton_Click(object sender, RoutedEventArgs e)
1844  {
1845  Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
1846 
1847  // Set filter for file extension and default file extension
1848  dlg.DefaultExt = ".frg";
1849  dlg.Filter = "Face Recognition Gallery Files (*.frg)|*.frg";
1850  // Set the initial directory
1851  dlg.InitialDirectory = System.IO.Path.GetFullPath(@"\..\Samples\OpenGL\data\ShowcaseDemo");
1852 
1853  if (dlg.ShowDialog() == true)
1854  {
1855  string filename = dlg.FileName;
1856 
1857  lock (lockClearRecognition)
1858  {
1859  gVisageFaceRecognition.LoadGallery(filename);
1860 
1861  int count = gVisageFaceRecognition.GetDescriptorCount();
1862 
1863  for (int i = 0; i < count; i++)
1864  {
1865  // fetch all unique names from the FR gallery and store in global array for future use
1866  string name = gVisageFaceRecognition.GetDescriptorName(i);
1867  if (!gUniqueNames.Contains(name))
1868  {
1869  gUniqueNames.Add(name);
1870  indices_to_names[numPersons] = name;
1871  numPersons += 1;
1872  FillNames();
1873  }
1874  }
1875 
1876  // By setting numInitFrames to numInitFramesThreshold skip initialization phase
1877  for (int i = 0; i < MAX_FACES_DETECTOR; i++)
1878  numInitFrames[i] = numInitFramesThreshold;
1879  // Clear any descriptors stored in the buffer during initialization phase in case the init phase was interrupted
1880  for (int trackerIndex = 0; trackerIndex < maxFacesTracker; ++trackerIndex)
1881  {
1882  descBuffer[trackerIndex].Clear();
1883  }
1884  }
1885  }
1886  }
1887 
1888  /// <summary>
1889  /// Save face recognition gallery (.frg) to a file.
1890  /// </summary>
1891  /// <param name="sender"></param>
1892  /// <param name="e"></param>
1893  private void saveGalleryButton_Click(object sender, RoutedEventArgs e)
1894  {
1895  Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
1896 
1897  dlg.DefaultExt = ".frg";
1898  dlg.Filter = "Face Recognition Gallery Files (*.frg)|*.frg";
1899 
1900  if (dlg.ShowDialog() == true)
1901  {
1902  string filename = dlg.FileName;
1903  gVisageFaceRecognition.SaveGallery(filename);
1904  }
1905  }
1906 
1907  /// <summary>
1908  /// Clear face recognition GUI element representing the gallery.
1909  ///
1910  /// It will also clear all the buffers for every face.
1911  /// </summary>
1912  /// <param name="sender"></param>
1913  /// <param name="e"></param>
1914  private void clearGalleryButton_Click(object sender, RoutedEventArgs e)
1915  {
1917  }
1918 
1919  /// <summary>
1920  /// A helper function used for populating the GUI element representing face recognition gallery from a variable.
1921  /// </summary>
1922  private void FillNames()
1923  {
1924  lbxNames.Items.Clear();
1925 
1926  for (int i = 0; i < gUniqueNames.Count(); i++)
1927  {
1928  TextBox tbx = new TextBox();
1929 
1930  tbx.Text = gUniqueNames[i];
1931  tbx.Tag = i.ToString();
1932  tbx.BorderBrush = System.Windows.Media.Brushes.White;
1933 
1934  tbx.TextChanged += new TextChangedEventHandler(OnNameTextChanged);
1935 
1936  lbxNames.Items.Add(tbx);
1937  }
1938  }
1939 
1940  /// <summary>
1941  /// A helper function for renaming a descriptor once it is added to the GUI element representing face recognition gallery.
1942  /// </summary>
1943  /// <param name="sender"></param>
1944  /// <param name="e"></param>
1945  private void OnNameTextChanged(object sender, TextChangedEventArgs e)
1946  {
1947  TextBox tbx = sender as TextBox;
1948 
1949  int index = lbxNames.Items.IndexOf(tbx);
1950 
1951  string current_name = indices_to_names[index];
1952  for (int i = 0; i < gVisageFaceRecognition.GetDescriptorCount(); i++)
1953  if (gVisageFaceRecognition.GetDescriptorName(i) == current_name)
1954  gVisageFaceRecognition.ReplaceDescriptorName(tbx.Text, i);
1955 
1956  for (int i = 0; i < gUniqueNames.Count(); i++)
1957  if (gUniqueNames[i] == current_name)
1958  gUniqueNames[i] = tbx.Text;
1959  indices_to_names[index] = tbx.Text;
1960  }
1961 
1962  #endregion
1963 
1964 
1965  #region Configuration Control
1966 
1967 
1968  private void cbEars_Clicked(object sender, RoutedEventArgs e)
1969  {
1970  if (gVisageTracker != null)
1971  {
1972  toggleEarsTracking();
1973 
1974  (sender as CheckBox).Foreground = (System.Windows.Media.Brush)FindResource((sender as CheckBox).Foreground == ((System.Windows.Media.Brush)FindResource("Active")) ? "Passive" : "Active");
1975 
1976  if (activeInputMode == INPUT_MODE.CAMERA)
1977  {
1978  runTrackFromCam();
1979  }
1980 
1981  else if (activeInputMode == INPUT_MODE.VIDEO)
1982  {
1983  runTrackFromVideo(currentVideo);
1984  }
1985 
1986  else if (activeInputMode == INPUT_MODE.IMAGE)
1987  {
1988  runTrackFromImage(currentImage, activeVisageMode);
1989  }
1990  }
1991  }
1992 
1993  private void toggleEarsTracking()
1994  {
1995  VisageConfiguration cfg = gVisageTracker.GetTrackerConfiguration();
1996  string model_cfg;
1997  float[] smoothing = cfg.SmoothingFactors;
1998  int refineEars;
1999 
2000  if ((bool)cbEars.IsChecked)
2001  {
2002  model_cfg = @"vft\fm\jk_300_wEars.cfg";
2003  smoothing[7] = 1.5f;
2004  refineEars = 1;
2005  }
2006  else
2007  {
2008  model_cfg = @"vft\fm\jk_300.cfg";
2009  smoothing[7] = -1.0f;
2010  refineEars = 0;
2011  }
2012 
2013  cfg.MeshFittingModelConfiguration = model_cfg;
2014  cfg.PoseFittingModelConfiguration = model_cfg;
2015  cfg.AuFittingModelConfiguration = model_cfg;
2016  cfg.SmoothingFactors = smoothing;
2017  cfg.RefineEars = refineEars;
2018 
2019  gVisageTracker.SetTrackerConfiguration(cfg);
2020  }
2021 
2022  private void rbFFTHigh_Clicked(object sender, RoutedEventArgs e)
2023  {
2024  if (gVisageTracker != null)
2025  {
2026  gVisageTracker.SetTrackerConfiguration("../Samples/data/Facial Features Tracker.cfg");
2027  if ((bool)cbEars.IsChecked)
2028  {
2029  toggleEarsTracking();
2030  }
2031  if (activeInputMode == INPUT_MODE.CAMERA)
2032  {
2033  runTrackFromCam();
2034  }
2035 
2036  else if (activeInputMode == INPUT_MODE.VIDEO)
2037  {
2038  runTrackFromVideo(currentVideo);
2039  }
2040 
2041  else if (activeInputMode == INPUT_MODE.IMAGE)
2042  {
2043  runTrackFromImage(currentImage, activeVisageMode);
2044  }
2045 
2046  //set style
2047  changeConfStyle(sender);
2048  }
2049  }
2050 
2051  private void rbFFTLow_Clicked(object sender, RoutedEventArgs e)
2052  {
2053  if (gVisageTracker != null)
2054  {
2055  VisageConfiguration cfg = gVisageTracker.GetTrackerConfiguration();
2056  cfg.RefineLandmarks = 0;
2057  gVisageTracker.SetTrackerConfiguration(cfg);
2058  if ((bool)cbEars.IsChecked)
2059  {
2060  toggleEarsTracking();
2061  }
2062  if (activeInputMode == INPUT_MODE.CAMERA)
2063  {
2064  runTrackFromCam();
2065  }
2066  else if (activeInputMode == INPUT_MODE.VIDEO)
2067  {
2068  runTrackFromVideo(currentVideo);
2069  }
2070  else if (activeInputMode == INPUT_MODE.IMAGE)
2071  {
2072  runTrackFromImage(currentImage, activeVisageMode);
2073  }
2074 
2075  //set style
2076  changeConfStyle(sender);
2077  }
2078  }
2079 
2080  #endregion
2081 
2082 
2083  #region Render Options Control
2084 
2085  private void checkBox_onClick(object sender, RoutedEventArgs e)
2086  {
2087  //reset and update display options when checkbox is checked/unchecked
2088  updateDisplay();
2089 
2090  (sender as CheckBox).Foreground = (System.Windows.Media.Brush)FindResource((sender as CheckBox).Foreground == ((System.Windows.Media.Brush)FindResource("Active")) ? "Passive" : "Active");
2091  }
2092 
2093  private void updateDisplay()
2094  {
2095  ACTIVE_DISPLAY = 0;
2096 
2097  if ((bool)cbFrame.IsChecked)
2098  ACTIVE_DISPLAY += VisageRendering.DISPLAY_FRAME;
2099 
2100  if ((bool)cbFeaturePoints.IsChecked)
2101  ACTIVE_DISPLAY += VisageRendering.DISPLAY_FEATURE_POINTS;
2102 
2103  if ((bool)cbSplines.IsChecked)
2104  ACTIVE_DISPLAY += VisageRendering.DISPLAY_SPLINES;
2105 
2106  if ((bool)cbGaze.IsChecked)
2107  ACTIVE_DISPLAY += VisageRendering.DISPLAY_GAZE;
2108 
2109  if ((bool)cbIris.IsChecked)
2110  ACTIVE_DISPLAY += VisageRendering.DISPLAY_IRIS;
2111 
2112  if ((bool)cbAxes.IsChecked)
2113  ACTIVE_DISPLAY += VisageRendering.DISPLAY_AXES;
2114 
2115  if ((bool)cbWireFrame.IsChecked)
2116  ACTIVE_DISPLAY += VisageRendering.DISPLAY_WIRE_FRAME;
2117 
2118  if ((bool)cbAge.IsChecked)
2119  {
2120  ACTIVE_DISPLAY += VisageRendering.DISPLAY_AGE;
2121  FA_AGE_CHECKED = true;
2122  }
2123  else
2124  FA_AGE_CHECKED = false;
2125 
2126  if ((bool)cbGender.IsChecked)
2127  {
2128  ACTIVE_DISPLAY += VisageRendering.DISPLAY_GENDER;
2129  FA_GEN_CHECKED = true;
2130  }
2131  else
2132  FA_GEN_CHECKED = false;
2133 
2134  if ((bool)cbEmotion.IsChecked)
2135  {
2136  ACTIVE_DISPLAY += VisageRendering.DISPLAY_EMOTION;
2137  FA_EMO_CHECKED = true;
2138  }
2139  else
2140  FA_EMO_CHECKED = false;
2141 
2142 
2143  if ((bool)cbTexturedModel.IsChecked)
2144  ACTIVE_DISPLAY += VisageRendering.DISPLAY_TEXTURED_MODEL;
2145 
2146  if ((bool)cbTrackingQuality.IsChecked)
2147  ACTIVE_DISPLAY += VisageRendering.DISPLAY_TRACKING_QUALITY + VisageRendering.DISPLAY_POINT_QUALITY;
2148 
2149  if ((bool)cbRecognition.IsChecked)
2150  {
2151  ACTIVE_DISPLAY += VisageRendering.DISPLAY_NAME;
2152  FR_CHECKED = true;
2153  }
2154  else
2155  {
2156  FR_CHECKED = false;
2157  }
2158  ACTIVE_MIRROR = ((bool)cbMirror.IsChecked) ? true : false;
2159 
2160  if ((bool)cbFreeze.IsChecked)
2161  {
2162  freeze_gallery = true;
2163  }
2164  else
2165  {
2166  freeze_gallery = false;
2167  }
2168  }
2169 
2170  private void manageControls(VISAGE_MODE activeMode)
2171  {
2172  //enable/disable controls for the detector mode
2173  if (activeMode == VISAGE_MODE.DETECTOR)
2174  {
2175  camButton.IsEnabled = false;
2176  videoButton.IsEnabled = false;
2177  rbFFTHigh.IsEnabled = false;
2178  rbFFTLow.IsEnabled = false;
2179  if (cbFrame.IsChecked != true)
2180  {
2181  cbFrame.IsChecked = true;
2182  updateDisplay();
2183  }
2184  cbFrame.IsEnabled = false;
2185  lCamera.IsEnabled = false;
2186  lVideo.IsEnabled = false;
2187 
2188  // Disable Age, Gender and Emotion estimation features
2192 
2193  // Disable Ears tracking feature
2195 
2196  updateDisplay();
2197  //
2199  }
2200 
2201  //enable/disable controls for the tracker mode
2202  else if (activeMode == VISAGE_MODE.TRACKER)
2203  {
2204  camButton.IsEnabled = true;
2205  videoButton.IsEnabled = true;
2206  rbFFTHigh.IsEnabled = true;
2207  rbFFTLow.IsEnabled = true;
2208  cbFrame.IsEnabled = true;
2209  cbGender.IsEnabled = true;
2210  cbEmotion.IsEnabled = true;
2211  cbMirror.IsEnabled = true;
2212  lCamera.IsEnabled = true;
2213  lVideo.IsEnabled = true;
2214  // Enable Age, Gender and Emotion estimation features
2218 
2219  // Enable Ears tracking feature
2221 
2222  updateDisplay();
2223 
2224  //
2225  changeButtonStyle(INPUT_MODE.CAMERA);
2226  }
2227  }
2228 
2229  #endregion
2230 
2231 
2232  /// <summary>
2233  /// Adjust aspect of the rendered image on window resize.
2234  /// </summary>
2235  /// <param name="faceData">Input FaceData object which contains the frame necessary for adjusting aspect.</param>
2236  /// <param name="width">Width that will be adjusted.</param>
2237  /// <param name="height">Height that will be adjusted.</param>
2238  private void AdjustAspect(ref FaceData faceData, ref int width, ref int height)
2239  {
2240  int mWidth = frame.width;
2241  int mHeight = frame.height;
2242 
2243  float aspect = mWidth / (float)mHeight;
2244  float tmp;
2245 
2246  float winWidth = (float)openGLControl.ActualWidth;
2247  float winHeight = (float)openGLControl.ActualHeight;
2248 
2249  if (aspect < 1.0f)
2250  {
2251  tmp = winHeight;
2252  winHeight = winWidth / aspect;
2253  if (winHeight > tmp)
2254  {
2255  winWidth = winWidth * tmp / winHeight;
2256  winHeight = tmp;
2257  }
2258  }
2259  else
2260  {
2261  tmp = winWidth;
2262  winWidth = winHeight * aspect;
2263  if (winWidth > tmp)
2264  {
2265  winHeight = winHeight * tmp / winWidth;
2266  winWidth = tmp;
2267  }
2268  }
2269 
2270  width = (int)winWidth;
2271  height = (int)winHeight;
2272  }
2273 
2274  /// <summary>
2275  /// Helper functions for loading image from image path. Flips image.
2276  /// </summary>
2277  /// <param name="path">Image path</param>
2278  /// <returns></returns>
2279  private VSImage LoadImage(string path)
2280  {
2281  Bitmap bmp = new Bitmap(path);
2282  bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
2283  int bitmapStride = 0;
2284  byte[] bmp_byte = BitmapToByteArray(bmp, ref bitmapStride);
2285  BitmapData bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
2286  VSImage image = new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, 3); // nChannels ?
2287  image.imageData = bmp_byte;
2288  return image;
2289  }
2290 
2291  }
2292 }
VisageConfiguration VisageConfiguration GetTrackerConfiguration()
void trackerDisplay()
Calls the VisageRendering' DisplayResults function.
static byte[] BitmapToByteArray(Bitmap bitmap, ref int stride)
Helper function for converting image data from Bitmap class to a raw byte array.
VisageSDK ShowcaseDemo application MainWindow class.
void ResetFaceAnalysis(int index)
Clears face analysis data and smoothing data for the specific face.
void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
Handles the OpenGLDraw event of the openGLControl control.
int LoadGallery(System::String^file_name)
int ExtractDescriptor(FaceData^facedata, VSImage^image, short[] descriptor)
void worker_DoWorkVideo(object sender, DoWorkEventArgs e)
Worker event for tracking from video that runs in a separate thread.
void ResetFRInitialization(int index=-1)
Reinitialize face recognition variables used during face recognition initialization phase...
int Recognize(short[] descriptor, int n, ref string[] names, float[] similarities)
void runFaceAnalysis(VSImage frame, FaceData[] faceData, int[] trackerStatus)
Demonstrates running face analysis in a background thread.
void worker_DoWorkMatchFace(object sender, DoWorkEventArgs e)
Worker event for face recognition that runs in a separate thread.
void enableGenderEstimCheckbox()
Enable gender estimation checkbox and change to appropriate color.
void runTrackFromImage(string file, VISAGE_MODE activeMode)
Demonstrates running a new tracking thread for tracking from image.
INPUT_MODE
Type of input for tracker/detector.
int[] Track(int frameWidth, int frameHeight, Byte[] imageData, FaceData[] faceData, int format=VISAGE_FORMAT.RGB, int origin=VISAGE_ORIGIN.TL, int widthStep=0, long timeStamp=-1, int maxFaces=-1)
void disableEmotionEstimCheckbox()
Disable emotion estimation checkbox and change color for the use case when in Detector mode ...
VFAReturnCode analyseImage(VSImage image, FaceData facedata, int options, ref AnalysisData results)
int AddDescriptor(VSImage^image, FaceData^facedata, System::String^name)
void trackerDetectorButton_Click(object sender, RoutedEventArgs e)
Toggles application mode from TRACKER to DETECTOR and vice versa.
void ClearFaceRecognition()
Reset all the face recognition variables including gallery.
void InitializeFaceRecognition()
Initialize face recognition buffer variables.
void disableAgeEstimCheckbox()
Disable age estimation checkbox and change color for the use case when one channel or four channel is...
VSImage LoadImage(string path)
Helper functions for loading image from image path.
void runTrackFromVideo(string file)
Demonstrates running a new tracking thread for tracking from video.
int SaveGallery(System::String^file_name)
void changeConfStyle(object sender)
Change radio button style (configuration choice) depending on the chose configuration ...
void enableAgeEstimCheckbox()
Enable age estimation checkbox and change to appropriate color.
void saveGalleryButton_Click(object sender, RoutedEventArgs e)
Save face recognition gallery (.frg) to a file.
VFAReturnCode analyseStream(VSImage image, FaceData facedata, int options, ref AnalysisData results, int faceIndex)
void SetTrackerConfiguration(String trackerConfigFile, bool au_fitting_disabled=false, bool mesh_fitting_disabled=false)
void worker_DoAnalyzeFace(object sender, DoWorkEventArgs e)
Worker event for face analysis that runs in a separate thread.
MainWindow()
Main entry point of the application.
void changeButtonStyle(INPUT_MODE mode)
Change button style (camera, video or image buttons) depending on the chosen INPUT_MODE ...
void detectorDisplay()
Calls the Calls the VisageRendering' DisplayResults function for every detected face.
void worker_DoWorkCam(object sender, DoWorkEventArgs e)
Worker event for tracking from camera that runs in a separate thread.
void enableEarsCheckbox()
Enable Ears checkbox and change color for the use case when not in Detector mode
void runTrackFromCam()
Demonstrates running a new tracking thread for tracking from camera.
void enableEmotionEstimCheckbox()
Enable emotion estimation checkbox and change to appropriate color.
void loadGalleryButton_Click(object sender, RoutedEventArgs e)
Load face recognition gallery (.frg) from file.
void AnalyzeFace(VSImage frame, FaceData faceData, int index=0)
Performs face analysis on an image based on the input face data object.
void clearGalleryButton_Click(object sender, RoutedEventArgs e)
Clear face recognition GUI element representing the gallery.
void FillNames()
A helper function used for populating the GUI element representing face recognition gallery from a va...
void RecognizeFace(VSImage frame, FaceData faceDatas, int index=0)
Performs face recognition on an image based on the input face data array.
void disableEarsCheckbox()
Disable Ears checkbox and change color for the use case when in Detector mode
int DetectFacialFeatures(VSImage frame, FaceData[] output, float minFaceScale=0.1f, float maxFaceScale=1.0f, bool outputOnly2DFeatures=false)
int ReplaceDescriptorName(const char *name, int index)
void AdjustAspect(ref FaceData faceData, ref int width, ref int height)
Adjust aspect of the rendered image on window resize.
VISAGE_MODE
Application can either be in TRACKER mode or in DETECTOR mode.
void disableGenderEstimCheckbox()
Disable gender estimation checkbox and change color for the use case when in Detector mode ...
void convertFromBGRToRGB(ref byte[] outputRGB, ref byte[] inputBGR)
Converts BGR image obtained from camera to RGB image.
void InitializeFaceAnalysis()
Reinitialize face analysis variables for storing data.
void resetStreamAnalysis(int faceIndex)
void worker_DoWorkImage(object sender, DoWorkEventArgs e)
Worker event for tracking/detection from an image source that runs in a separate thread.
int GetDescriptorName(char *name, int index)
void runFaceRecognition(VSImage frame, FaceData[] faceDatas, int[] trackerStatus)
Demonstrates running face recognition in a background thread.
void OnNameTextChanged(object sender, TextChangedEventArgs e)
A helper function for renaming a descriptor once it is added to the GUI element representing face rec...