Ejemplo n.º 1
0
        private void Recognize_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".png";
            dlg.Filter     = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png";


            // Display OpenFileDialog by calling ShowDialog method
            Nullable <bool> result = dlg.ShowDialog();

            string filename;

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                filename = dlg.FileName;

                Capture capture = new Capture(filename);

                var path = @"C:\opencv\build\etc\haarcascades\haarcascade_frontalface_alt2.xml";
                try
                {
                    CascadeClassifier _cascadeClassifier = new CascadeClassifier(path);
                    using (var imageFrame = capture.QueryFrame().ToImage <Bgr, Byte>())
                    {
                        if (imageFrame != null)
                        {
                            var grayframe = imageFrame.Convert <Gray, byte>();
                            var faces     = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, System.Drawing.Size.Empty);
                            foreach (var face in faces)
                            {
                                imageFrame.Draw(face, new Bgr(Color.Red), 3);
                            }
                        }

                        var personalFace = BitmapToImageSource(imageFrame.Bitmap);
                        image.Source = personalFace;

                        var faceToRecognize = new Image <Gray, byte>(imageFrame.Bitmap);
                        var recogResult     = dataStore.GetUsername(engine.Recognize(faceToRecognize));

                        if (recogResult == "")
                        {
                            recogResult = "No match.";
                        }

                        System.Windows.Forms.MessageBox.Show(recogResult, "Recognition Result", MessageBoxButtons.OK);
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }