7 using System.Drawing.Imaging;
8 using System.ComponentModel;
9 using System.Runtime.InteropServices;
10 using System.Windows.Threading;
13 using Emgu.CV.Structure;
15 using System.Windows.Forms;
16 using System.Collections.Generic;
19 using System.Windows.Controls;
109 #region Textured model
112 public List<Point3D> v;
113 public List<Point> vt;
114 public List<Face> faces;
119 public int[] indexPoints;
120 public int[] texIndexPoints;
122 public Face(
int[] indexPoints,
int[] texIndexPoints)
124 this.indexPoints = indexPoints;
125 this.texIndexPoints = texIndexPoints;
134 public Point(
float x,
float y)
147 public Point3D(
float x,
float y,
float z)
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;
178 VisageRendering gVisageRendering;
181 public bool isTracked =
false;
182 public bool isDetected =
false;
183 public bool inDrawDetection =
false;
189 public long elapsedTime;
190 public long startTime;
192 System.Diagnostics.Stopwatch pureTrackStopwatch =
new System.Diagnostics.Stopwatch();
195 private Object lockFaceData =
new Object();
197 private Object lockClearRecognition =
new Object();
200 private BackgroundWorker worker;
202 private BackgroundWorker fr_worker;
204 private BackgroundWorker fa_worker;
208 public int[] gTrackerStatus;
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;
220 public bool detectorActive =
false;
222 const int MAX_FACES_DETECTOR = 30;
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[]>();
235 List<string> recognizedFaces =
new List<string>();
236 int[] numInitFrames =
new int[MAX_FACES_DETECTOR];
237 const int numInitFramesThreshold = 10;
238 int[] numFramesNewIdentity =
new int[MAX_FACES_DETECTOR];
239 const int newIdentityFramesThreshold = 5;
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[]>>();
250 string imageInitialDir =
@"..\Samples\OpenGL\data\Images";
251 string videoInitialDir =
@"..\Samples\OpenGL\data\Videos";
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;
260 bool freeze_gallery =
false;
275 InitializeComponent();
277 VisageLicensing.InitializeLicenseManager(licensePath);
280 gVisageTracker =
new VisageTracker(
@"..\Samples\data\Facial Features Tracker.cfg");
283 gFaceData =
new FaceData[maxFacesTracker];
284 for (
int i = 0; i < maxFacesTracker; ++i)
291 vfaInitialised = gVisageFaceAnalyser.
Init(
@"..\Samples\data\vfa");
306 gl = openGLControl.OpenGL;
307 gVisageRendering =
new VisageRendering(gl);
310 gVisageRendering.SetTextureImage(
LoadImage(
@"..\Samples\OpenGL\data\ShowcaseDemo\tiger_texture.png"));
313 DsDevice[] _SystemCamereas = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
314 for (
int i = 0; i < _SystemCamereas.Length; i++)
316 cameraDevices.Add(i, _SystemCamereas[i].Name);
317 ComboBoxItem cbi =
new ComboBoxItem();
318 cbi.Content = _SystemCamereas[i].Name;
319 cbCameraChoice.Items.Add(cbi);
321 if (cameraDevices.Count > 0)
323 cbCameraChoice.SelectedIndex = currentCameraDevice;
327 #region FR and FA State Modifiers
334 lock (lockClearRecognition)
336 for (
int trackerIndex = 0; trackerIndex < maxFacesTracker; ++trackerIndex)
338 lbxNames.Items.Clear();
339 recognizedFaces =
new List<string>();
340 descBuffer[trackerIndex].Clear();
341 numInitFrames[trackerIndex] = 0;
342 numFramesNewIdentity[trackerIndex] = 0;
344 gUniqueNames =
new List<string>();
345 currentDisplayName[trackerIndex] =
"";
346 indices_to_names =
new Dictionary<int, string>();
357 for (
int i = 0; i < maxFacesTracker; ++i)
360 descBuffer.Add(i,
new List<
short[]>());
362 numInitFrames[i] = 0;
363 numFramesNewIdentity[i] = 0;
365 currentDisplayName[i] =
"";
377 lock (lockClearRecognition)
381 for (
int i = 0; i < maxFacesTracker; ++i)
383 descBuffer[i].Clear();
384 numFramesNewIdentity[i] = 0;
386 if (numInitFrames[i] != 0 && numInitFrames[i] < numInitFramesThreshold)
387 numInitFrames[i] = numInitFramesThreshold;
392 descBuffer[index].Clear();
393 numFramesNewIdentity[index] = 0;
395 if (numInitFrames[index] != 0 && numInitFrames[index] < numInitFramesThreshold)
396 numInitFrames[index] = numInitFramesThreshold;
407 gAgeArray =
new float[MAX_FACES_DETECTOR];
408 gGenderArray =
new int[MAX_FACES_DETECTOR];
410 gAgeSmoothed =
new float[maxFacesTracker];
411 gGenderSmoothed =
new int[maxFacesTracker];
412 gEmotionsSmoothed.Clear();
414 for (
int i = 0; i < MAX_FACES_DETECTOR; ++i)
416 gAgeArray[i] = -1.0f;
417 gGenderArray[i] = -1;
418 gEmotionList.Add(
default(
float[]));
421 for (
int i = 0; i < maxFacesTracker; ++i)
423 gAgeSmoothed[i] = -1.0f;
424 gGenderSmoothed[i] = -1;
425 gEmotionsSmoothed.Add(
default(
float[]));
437 gAgeSmoothed[index] = -1.0f;
438 gGenderSmoothed[index] = -1;
439 gEmotionsSmoothed[index] =
default(
float[]);
447 #region BackgroundWorker Tracking Functions
449 private static byte[] BitmapToByteArray(Bitmap bitmap)
452 return BitmapToByteArray(bitmap, ref stride);
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;
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;
473 Marshal.Copy(ptr, bytedata, 0, numbytes);
475 bitmap.UnlockBits(bmpdata);
495 BackgroundWorker worker = sender as BackgroundWorker;
496 List<object> argumentList = e.Argument as List<object>;
497 string file = (string)argumentList[0];
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);
510 switch (bmp.PixelFormat)
512 case PixelFormat.Format24bppRgb:
516 case PixelFormat.Format8bppIndexed:
521 case PixelFormat.Format32bppRgb:
524 Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
538 frame =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
541 byte[] bytedataRGB =
new byte[bmp_byte.Length];
545 frameRGB =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
551 if (worker.CancellationPending || format == -1)
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;
573 for (
int i = 0; i < maxFacesTracker; ++i)
575 analysisData[i] =
new FaceData(gFaceData[i]);
593 while (inDrawDetection)
595 System.Threading.Thread.Sleep(100);
598 bmp.UnlockBits(bmpdata);
601 dataArray =
new FaceData[MAX_FACES_DETECTOR];
602 for (
int i = 0; i < MAX_FACES_DETECTOR; ++i)
608 if (gVisageFeaturesDetector.Initialized !=
true)
610 worker.CancelAsync();
619 gAgeArray =
new float[numFaces];
620 gGenderArray =
new int[numFaces];
621 gEmotionList.Clear();
622 for (
int i = 0; i < numFaces; i++)
625 gAgeArray[i] = -1.0f;
626 gGenderArray[i] = -1;
627 gEmotionList.Add(
default(
float[]));
638 worker.CancelAsync();
642 gVisageTracker.
Stop();
660 if (cameraCapture == null)
663 cameraCapture =
new Capture(currentCameraDevice);
665 cameraCapture.SetCaptureProperty(CapProp.FrameWidth, 800);
666 cameraCapture.SetCaptureProperty(CapProp.FrameHeight, 600);
681 if (worker.CancellationPending)
685 cameraCapture.Dispose();
686 cameraCapture = null;
692 Bitmap bmp = cameraCapture.QueryFrame().Bitmap;
694 if (ACTIVE_MIRROR ==
true)
695 bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
698 switch (bmp.PixelFormat)
700 case PixelFormat.Format24bppRgb:
703 case PixelFormat.Format8bppIndexed:
707 case PixelFormat.Format32bppRgb:
721 frame =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
722 byte[] bmp_byte = BitmapToByteArray(bmp);
725 byte[] bytedataRGB =
new byte[bmp_byte.Length];
729 frameRGB =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
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;
740 for (
int i = 0; i < maxFacesTracker; ++i)
742 analysisData[i] =
new FaceData(gFaceData[i]);
756 gVisageTracker.
Stop();
767 int length = inputBGR.Length;
770 for (
int i = 0; i < length/nChannels; i++)
772 outputRGB[j++] = inputBGR[i * nChannels + 2];
773 outputRGB[j++] = inputBGR[i * nChannels + 1];
774 outputRGB[j++] = inputBGR[i * nChannels];
794 BackgroundWorker worker = sender as BackgroundWorker;
795 string file = (string)e.Argument;
798 Capture capture =
new Capture(file);
799 Bitmap bmp =
new Bitmap(capture.QueryFrame().Bitmap);
800 int bitmapStride = 0;
805 System.Diagnostics.Stopwatch stopwatch =
new System.Diagnostics.Stopwatch();
808 bool video_file_sync =
true;
809 float frameTime = 1000 / (float)CvInvoke.cveVideoCaptureGet(capture, Emgu.CV.CvEnum.CapProp.Fps);
820 if (worker.CancellationPending)
829 long currentTime = stopwatch.ElapsedMilliseconds;
831 while (video_file_sync && currentTime > frameTime * (frameCount + 1))
834 currentTime = stopwatch.ElapsedMilliseconds;
838 while (video_file_sync && currentTime < frameTime * (frameCount - 5))
840 System.Threading.Thread.Sleep(1);
841 currentTime = stopwatch.ElapsedMilliseconds;
847 bmp = capture.QueryFrame().Bitmap;
849 if (ACTIVE_MIRROR ==
true)
850 bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
852 catch (NullReferenceException)
854 worker.CancelAsync();
861 switch (bmp.PixelFormat)
863 case PixelFormat.Format24bppRgb:
867 case PixelFormat.Format8bppIndexed:
872 case PixelFormat.Format32bppRgb:
875 Dispatcher.BeginInvoke((Action)(() => { disableAgeEstimCheckbox(); }));
887 frame =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
888 byte[] bmp_byte = BitmapToByteArray(bmp, ref bitmapStride);
891 byte[] bytedataRGB =
new byte[bmp_byte.Length];
895 frameRGB =
new VSImage(bmp.Width, bmp.Height, VS_DEPTH._8U, nChannels);
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;
906 for (
int i = 0; i < maxFacesTracker; ++i)
908 analysisData[i] =
new FaceData(gFaceData[i]);
922 gVisageTracker.
Stop();
936 List<object> argumentList = e.Argument as List<object>;
940 int[] trackerStatus = (
int[])argumentList[2];
943 for (
int i = 0; i < maxFacesTracker; ++i)
945 if (trackerStatus[i] == (
int)
TRACK_STAT.OK && gVisageRendering.withinConstraints(ref faceDatas[i]))
968 List<object> argumentList = e.Argument as List<object>;
972 int[] trackerStatus = (
int[])argumentList[2];
975 for (
int i = 0; i < maxFacesTracker; ++i)
979 if (gEmotionsSmoothed[i] ==
default(
float[])) gEmotionsSmoothed[i] =
new float[NUM_EMOTIONS];
1003 if ((FA_AGE_CHECKED || FA_GEN_CHECKED || FA_EMO_CHECKED))
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;
1011 if ((vfaInitialised & options) == options)
1015 status = gVisageFaceAnalyser.
analyseStream(frame, faceData, (
int)options, ref analysisResults, index);
1019 if (analysisResults.ageValid)
1021 gAgeSmoothed[index] = analysisResults.age;
1023 if (analysisResults.genderValid)
1025 gGenderSmoothed[index] = analysisResults.gender;
1027 if (analysisResults.emotionsValid)
1029 gEmotionsSmoothed[index] = analysisResults.emotionProbabilities;
1033 else if (activeVisageMode ==
VISAGE_MODE.DETECTOR)
1036 status = gVisageFaceAnalyser.
analyseImage(frame, faceData, (
int)options, ref analysisResults);
1040 if (analysisResults.ageValid)
1042 gAgeArray[index] = analysisResults.age;
1044 if (analysisResults.genderValid)
1046 gGenderArray[index] = analysisResults.gender;
1048 if (analysisResults.emotionsValid)
1050 gEmotionList[index] =
new float[NUM_EMOTIONS];
1051 gEmotionList[index] = analysisResults.emotionProbabilities;
1072 lock (lockClearRecognition)
1074 int DESCRIPTOR_SIZE;
1084 if (FR_CHECKED && !freeze_gallery)
1089 short[] descriptor =
new short[DESCRIPTOR_SIZE];
1092 int extract_descriptor_status = gVisageFaceRecognition.
ExtractDescriptor(faceDatas, frame, descriptor);
1095 if (numInitFrames[index] < numInitFramesThreshold)
1097 currentDisplayName[index] =
"?";
1099 if (extract_descriptor_status == 1)
1101 numInitFrames[index] += 1;
1104 descBuffer[index].Add(descriptor);
1107 currentDisplayName[index] =
"-";
1110 if (numInitFrames[index] >= numInitFramesThreshold)
1112 for (
int i = 0; i < numInitFramesThreshold; i++)
1114 gVisageFaceRecognition.
AddDescriptor(descBuffer[index][i],
"Person" + numPersons);
1118 descBuffer[index].Clear();
1120 gUniqueNames.Add(
"Person" + numPersons);
1121 indices_to_names[numPersons] =
"Person" + numPersons;
1123 Dispatcher.BeginInvoke((Action)(() =>
1132 float[] sim =
new float[count];
1133 string[] names =
new string[count];
1136 int recognize_status = 0;
1137 if (extract_descriptor_status == 1)
1138 recognize_status = gVisageFaceRecognition.
Recognize(descriptor, count, ref names, sim);
1141 if (recognize_status > 0 && extract_descriptor_status == 1 && sim[0] > display_threshold)
1143 currentDisplayName[index] = names[0];
1144 numFramesNewIdentity[index] = 0;
1147 else if (extract_descriptor_status == 0)
1149 currentDisplayName[index] =
"-";
1150 numFramesNewIdentity[index] = 0;
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];
1161 currentDisplayName[index] =
"?";
1163 numFramesNewIdentity[index] += 1;
1165 if (numFramesNewIdentity[index] > newIdentityFramesThreshold)
1167 numInitFrames[index] = 0;
1168 numFramesNewIdentity[index] = 0;
1175 if (FR_CHECKED && freeze_gallery)
1180 if (!(numInitFrames[index] == 0 || numInitFrames[index] == numInitFramesThreshold))
1182 descBuffer[index].Clear();
1183 numInitFrames[index] = 0;
1192 short[] descriptor =
new short[DESCRIPTOR_SIZE];
1193 float[] sim =
new float[count];
1194 string[] names =
new string[count];
1198 gVisageFaceRecognition.
Recognize(descriptor, count, ref names, sim);
1200 if (names.Count() > 0 && sim[0] > display_threshold)
1201 currentDisplayName[index] = names[0];
1203 currentDisplayName[index] =
"?";
1207 currentDisplayName[index] =
"?";
1217 #region OpenGL Tracker And Detector Display
1231 if (isTracked ==
true && gTrackerStatus[0] != 0)
1235 if (activeVisageMode ==
VISAGE_MODE.TRACKER && gFaceData != null)
1242 if (isDetected ==
true)
1244 if (activeVisageMode ==
VISAGE_MODE.DETECTOR && dataArray != null)
1246 inDrawDetection =
true;
1248 inDrawDetection =
false;
1260 cbFrame.IsEnabled =
true;
1262 int winWidth = (int)openGLControl.ActualWidth;
1263 int winHeight = (
int)openGLControl.ActualHeight;
1265 AdjustAspect(ref gFaceData[0], ref winWidth, ref winHeight);
1267 gl.Viewport(0, 0, winWidth, winHeight);
1270 gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_COLOR_BUFFER_BIT);
1272 lFPS.Text =
"Tracker FPS: " + gFaceData[0].FrameRate.ToString(
"n2");
1273 lPureTrackTime.Text =
"Track time: " + elapsedTime.ToString() +
" ms";
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();
1284 if ((
bool)cbFrame.IsChecked)
1286 gVisageRendering.DisplayFrame(frame, winWidth, winHeight);
1290 for (
int i = 0; i < maxFacesTracker; ++i)
1292 gVisageRendering.DisplayResults(frame, gFaceData[i], gTrackerStatus[i], winWidth, winHeight, ACTIVE_DISPLAY ^ VisageRendering.DISPLAY_FRAME, gEmotionsSmoothed[i], gGenderSmoothed[i], gAgeSmoothed[i], currentDisplayName[i]);
1305 int winWidth = (int)openGLControl.ActualWidth;
1306 int winHeight = (
int)openGLControl.ActualHeight;
1308 AdjustAspect(ref dataArray[0], ref winWidth, ref winHeight);
1311 gl.Viewport(0, 0, winWidth, winHeight);
1314 gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_COLOR_BUFFER_BIT);
1317 lPureTrackTime.Text =
"";
1318 lTranslation.Text =
"";
1319 lRotation.Text =
"";
1323 gVisageRendering.DisplayFrame(frame, winWidth, winHeight);
1326 for (
int i = 0; i < numFaces; i++)
1328 gVisageRendering.DisplayResults(frame, dataArray[i], 1, winWidth, winHeight, ACTIVE_DISPLAY ^ VisageRendering.DISPLAY_FRAME, gEmotionList[i], gGenderArray[i], gAgeArray[i], currentDisplayName[i]);
1333 gl.DrawText(winWidth / 2, winHeight / 2, 1.0f, 0.0f, 0.0f,
"Arial", 25,
"Could not detect face");
1338 private string GetTrackerStatusString()
1342 switch (gTrackerStatus[0])
1364 #region BackgroundWorker Control
1383 worker.CancelAsync();
1387 while (worker.IsBusy)
1389 Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1390 new Action(delegate { }));
1398 worker =
new BackgroundWorker();
1399 worker.WorkerSupportsCancellation =
true;
1401 worker.RunWorkerAsync(file);
1421 worker.CancelAsync();
1425 while (worker.IsBusy)
1427 Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1428 new Action(delegate { }));
1436 worker =
new BackgroundWorker();
1437 worker.WorkerSupportsCancellation =
true;
1438 List<object> arguments =
new List<object>();
1439 arguments.Add(file);
1440 arguments.Add(activeMode);
1442 worker.RunWorkerAsync(arguments);
1460 worker.CancelAsync();
1464 while (worker.IsBusy)
1466 Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
1467 new Action(delegate { }));
1475 worker =
new BackgroundWorker();
1476 worker.WorkerSupportsCancellation =
true;
1478 worker.RunWorkerAsync();
1493 if (fr_worker != null)
1495 if (fr_worker.IsBusy)
1502 fr_worker =
new BackgroundWorker();
1504 fr_worker.WorkerSupportsCancellation =
true;
1508 List<object> arguments =
new List<object>();
1509 arguments.Add(frame);
1510 arguments.Add(faceDatas);
1511 arguments.Add(trackerStatus);
1512 fr_worker.RunWorkerAsync(arguments);
1523 if (!(FA_AGE_CHECKED || FA_GEN_CHECKED || FA_EMO_CHECKED))
1527 if (fa_worker != null)
1529 if (fa_worker.IsBusy)
1536 fa_worker =
new BackgroundWorker();
1538 fa_worker.WorkerSupportsCancellation =
true;
1542 List<object> arguments =
new List<object>();
1543 arguments.Add(frame);
1544 arguments.Add(faceData);
1545 arguments.Add(trackerStatus);
1546 fa_worker.RunWorkerAsync(arguments);
1551 #region Style Control
1559 camButton.Content = FindResource(
"CameraPassive");
1560 videoButton.Content = FindResource(
"VideoPassive");
1561 imageButton.Content = FindResource(
"ImagePassive");
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");
1569 camButton.Content = FindResource(
"CameraActive");
1570 lCamera.Foreground = (
System.Windows.Media.Brush)FindResource(
"Active");
1574 videoButton.Content = FindResource(
"VideoActive");
1575 lVideo.Foreground = (
System.Windows.Media.Brush)FindResource(
"Active");
1579 imageButton.Content = FindResource(
"ImageActive");
1580 lImage.Foreground = (
System.Windows.Media.Brush)FindResource(
"Active");
1590 rbFFTHigh.Foreground = (
System.Windows.Media.Brush)FindResource(
"Passive");
1591 rbFFTLow.Foreground = (
System.Windows.Media.Brush)FindResource(
"Passive");
1593 (sender as RadioButton).Foreground = (
System.Windows.Media.Brush)FindResource((sender as RadioButton).Foreground == (
System.Windows.Media.Brush)FindResource(
"Active") ?
"Passive" :
"Active");
1599 #region Button Control
1601 private void camButton_Click(
object sender, RoutedEventArgs e)
1609 private void videoButton_Click(
object sender, RoutedEventArgs e)
1612 Microsoft.Win32.OpenFileDialog dlg =
new Microsoft.Win32.OpenFileDialog();
1613 dlg.InitialDirectory =
System.IO.Path.GetFullPath(videoInitialDir);
1615 dlg.DefaultExt =
".mp4";
1616 dlg.Filter =
"Video Files|*.mp4;*.avi";
1618 Nullable<bool> result = dlg.ShowDialog();
1625 videoInitialDir =
System.IO.Path.GetDirectoryName(dlg.FileName);
1626 currentVideo = dlg.FileName;
1633 private void imageButton_Click(
object sender, RoutedEventArgs e)
1636 Microsoft.Win32.OpenFileDialog dlg =
new Microsoft.Win32.OpenFileDialog();
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);
1644 Nullable<bool> result = dlg.ShowDialog();
1654 imageInitialDir =
System.IO.Path.GetDirectoryName(dlg.FileName);
1655 currentImage = dlg.FileName;
1675 freeze_gallery =
true;
1676 cbFreeze.IsChecked =
true;
1678 manageControls(activeVisageMode);
1679 trackerDetectorButton.Content =
"Switch to Tracker";
1681 Microsoft.Win32.OpenFileDialog dlg =
new Microsoft.Win32.OpenFileDialog();
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";
1688 Nullable<bool> result = dlg.ShowDialog();
1695 currentImage = dlg.FileName;
1700 else if (activeVisageMode ==
VISAGE_MODE.DETECTOR)
1705 freeze_gallery =
false;
1706 cbFreeze.IsChecked =
false;
1709 manageControls(activeVisageMode);
1710 trackerDetectorButton.Content =
"Switch to Detector";
1717 private void cameraSettingsButton_Click(
object sender, RoutedEventArgs e)
1719 if (cameraCapture != null)
1720 cameraCapture.SetCaptureProperty(CapProp.Settings, 0);
1723 private void cbCameraChoice_SelectionChanged(
object sender, SelectionChangedEventArgs e)
1725 currentCameraDevice = (sender as ComboBox).SelectedIndex;
1736 cbAge.IsEnabled =
true;
1737 cbAge.ToolTip = null;
1738 if (cbAge.Foreground == ((
System.Windows.Media.Brush)FindResource(
"Disabled")))
1740 cbAge.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Passive"));
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." };
1754 cbAge.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Disabled"));
1762 cbEmotion.IsEnabled =
true;
1763 cbEmotion.ToolTip = null;
1764 if (cbEmotion.Foreground == ((
System.Windows.Media.Brush)FindResource(
"Disabled")))
1766 cbEmotion.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Passive"));
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"));
1787 cbGender.IsEnabled =
true;
1788 cbGender.ToolTip = null;
1789 if (cbGender.Foreground == ((
System.Windows.Media.Brush)FindResource(
"Disabled")))
1791 cbGender.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Passive"));
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"));
1812 cbEars.IsEnabled =
true;
1813 cbEars.ToolTip = null;
1814 if (cbEars.Foreground == ((
System.Windows.Media.Brush)FindResource(
"Disabled")))
1816 cbEars.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Passive"));
1825 cbEars.IsEnabled =
false;
1826 cbEars.IsChecked =
false;
1827 ToolTip t =
new ToolTip { Content =
"Ears Tracking disabled in Detector Mode." };
1829 cbEars.Foreground = ((
System.Windows.Media.Brush)FindResource(
"Disabled"));
1834 #region Recognition Controls
1845 Microsoft.Win32.OpenFileDialog dlg =
new Microsoft.Win32.OpenFileDialog();
1848 dlg.DefaultExt =
".frg";
1849 dlg.Filter =
"Face Recognition Gallery Files (*.frg)|*.frg";
1851 dlg.InitialDirectory =
System.IO.Path.GetFullPath(
@"\..\Samples\OpenGL\data\ShowcaseDemo");
1853 if (dlg.ShowDialog() ==
true)
1855 string filename = dlg.FileName;
1857 lock (lockClearRecognition)
1863 for (
int i = 0; i < count; i++)
1867 if (!gUniqueNames.Contains(name))
1869 gUniqueNames.Add(name);
1870 indices_to_names[numPersons] = name;
1877 for (
int i = 0; i < MAX_FACES_DETECTOR; i++)
1878 numInitFrames[i] = numInitFramesThreshold;
1880 for (
int trackerIndex = 0; trackerIndex < maxFacesTracker; ++trackerIndex)
1882 descBuffer[trackerIndex].Clear();
1895 Microsoft.Win32.SaveFileDialog dlg =
new Microsoft.Win32.SaveFileDialog();
1897 dlg.DefaultExt =
".frg";
1898 dlg.Filter =
"Face Recognition Gallery Files (*.frg)|*.frg";
1900 if (dlg.ShowDialog() ==
true)
1902 string filename = dlg.FileName;
1924 lbxNames.Items.Clear();
1926 for (
int i = 0; i < gUniqueNames.Count(); i++)
1928 TextBox tbx =
new TextBox();
1930 tbx.Text = gUniqueNames[i];
1931 tbx.Tag = i.ToString();
1932 tbx.BorderBrush =
System.Windows.Media.Brushes.White;
1936 lbxNames.Items.Add(tbx);
1947 TextBox tbx = sender as TextBox;
1949 int index = lbxNames.Items.IndexOf(tbx);
1951 string current_name = indices_to_names[index];
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;
1965 #region Configuration Control
1968 private void cbEars_Clicked(
object sender, RoutedEventArgs e)
1970 if (gVisageTracker != null)
1972 toggleEarsTracking();
1974 (sender as CheckBox).Foreground = (
System.Windows.Media.Brush)FindResource((sender as CheckBox).Foreground == ((
System.Windows.Media.Brush)FindResource(
"Active")) ?
"Passive" :
"Active");
1981 else if (activeInputMode ==
INPUT_MODE.VIDEO)
1986 else if (activeInputMode ==
INPUT_MODE.IMAGE)
1993 private void toggleEarsTracking()
2000 if ((
bool)cbEars.IsChecked)
2002 model_cfg =
@"vft\fm\jk_300_wEars.cfg";
2003 smoothing[7] = 1.5f;
2008 model_cfg =
@"vft\fm\jk_300.cfg";
2009 smoothing[7] = -1.0f;
2022 private void rbFFTHigh_Clicked(
object sender, RoutedEventArgs e)
2024 if (gVisageTracker != null)
2027 if ((
bool)cbEars.IsChecked)
2029 toggleEarsTracking();
2036 else if (activeInputMode ==
INPUT_MODE.VIDEO)
2041 else if (activeInputMode ==
INPUT_MODE.IMAGE)
2051 private void rbFFTLow_Clicked(
object sender, RoutedEventArgs e)
2053 if (gVisageTracker != null)
2056 cfg.RefineLandmarks = 0;
2058 if ((
bool)cbEars.IsChecked)
2060 toggleEarsTracking();
2066 else if (activeInputMode ==
INPUT_MODE.VIDEO)
2070 else if (activeInputMode ==
INPUT_MODE.IMAGE)
2083 #region Render Options Control
2085 private void checkBox_onClick(
object sender, RoutedEventArgs e)
2090 (sender as CheckBox).Foreground = (
System.Windows.Media.Brush)FindResource((sender as CheckBox).Foreground == ((
System.Windows.Media.Brush)FindResource(
"Active")) ?
"Passive" :
"Active");
2093 private void updateDisplay()
2097 if ((
bool)cbFrame.IsChecked)
2098 ACTIVE_DISPLAY += VisageRendering.DISPLAY_FRAME;
2100 if ((
bool)cbFeaturePoints.IsChecked)
2101 ACTIVE_DISPLAY += VisageRendering.DISPLAY_FEATURE_POINTS;
2103 if ((
bool)cbSplines.IsChecked)
2104 ACTIVE_DISPLAY += VisageRendering.DISPLAY_SPLINES;
2106 if ((
bool)cbGaze.IsChecked)
2107 ACTIVE_DISPLAY += VisageRendering.DISPLAY_GAZE;
2109 if ((
bool)cbIris.IsChecked)
2110 ACTIVE_DISPLAY += VisageRendering.DISPLAY_IRIS;
2112 if ((
bool)cbAxes.IsChecked)
2113 ACTIVE_DISPLAY += VisageRendering.DISPLAY_AXES;
2115 if ((
bool)cbWireFrame.IsChecked)
2116 ACTIVE_DISPLAY += VisageRendering.DISPLAY_WIRE_FRAME;
2118 if ((
bool)cbAge.IsChecked)
2120 ACTIVE_DISPLAY += VisageRendering.DISPLAY_AGE;
2121 FA_AGE_CHECKED =
true;
2124 FA_AGE_CHECKED =
false;
2126 if ((
bool)cbGender.IsChecked)
2128 ACTIVE_DISPLAY += VisageRendering.DISPLAY_GENDER;
2129 FA_GEN_CHECKED =
true;
2132 FA_GEN_CHECKED =
false;
2134 if ((
bool)cbEmotion.IsChecked)
2136 ACTIVE_DISPLAY += VisageRendering.DISPLAY_EMOTION;
2137 FA_EMO_CHECKED =
true;
2140 FA_EMO_CHECKED =
false;
2143 if ((
bool)cbTexturedModel.IsChecked)
2144 ACTIVE_DISPLAY += VisageRendering.DISPLAY_TEXTURED_MODEL;
2146 if ((
bool)cbTrackingQuality.IsChecked)
2147 ACTIVE_DISPLAY += VisageRendering.DISPLAY_TRACKING_QUALITY + VisageRendering.DISPLAY_POINT_QUALITY;
2149 if ((
bool)cbRecognition.IsChecked)
2151 ACTIVE_DISPLAY += VisageRendering.DISPLAY_NAME;
2158 ACTIVE_MIRROR = ((bool)cbMirror.IsChecked) ?
true :
false;
2160 if ((
bool)cbFreeze.IsChecked)
2162 freeze_gallery =
true;
2166 freeze_gallery =
false;
2170 private void manageControls(
VISAGE_MODE activeMode)
2175 camButton.IsEnabled =
false;
2176 videoButton.IsEnabled =
false;
2177 rbFFTHigh.IsEnabled =
false;
2178 rbFFTLow.IsEnabled =
false;
2179 if (cbFrame.IsChecked !=
true)
2181 cbFrame.IsChecked =
true;
2184 cbFrame.IsEnabled =
false;
2185 lCamera.IsEnabled =
false;
2186 lVideo.IsEnabled =
false;
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;
2240 int mWidth = frame.
width;
2241 int mHeight = frame.
height;
2243 float aspect = mWidth / (float)mHeight;
2246 float winWidth = (float)openGLControl.ActualWidth;
2247 float winHeight = (
float)openGLControl.ActualHeight;
2252 winHeight = winWidth / aspect;
2253 if (winHeight > tmp)
2255 winWidth = winWidth * tmp / winHeight;
2262 winWidth = winHeight * aspect;
2265 winHeight = winHeight * tmp / winWidth;
2270 width = (int)winWidth;
2271 height = (int)winHeight;
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);
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.
property bool IsInitialized
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 ...
String MeshFittingModelConfiguration
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.
String PoseFittingModelConfiguration
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.
String AuFittingModelConfiguration
void disableGenderEstimCheckbox()
Disable gender estimation checkbox and change color for the use case when in Detector mode ...
int Init(String dataPath)
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...