Beispiel #1
0
        private void SaveFace(object sender, RoutedEventArgs e)
        {
            if (SelectedFace == null)
            {
                MessageBox.Show("Выберите лицо для регистрации", "Ошибка регистрации", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            FaceRecognizer = new FisherFaceRecognizer(0, 3500);
            var imageList = new VectorOfMat();
            var labelList = new VectorOfInt();

            imageList.Push(SelectedFace.CVImage.Resize(100, 100, Inter.Cubic).Mat);

            var samples = Directory.GetFiles("Face Samples");

            foreach (var sample in samples)
            {
                imageList.Push(new Image <Gray, byte>(sample).Resize(100, 100, Inter.Cubic).Mat);
            }

            labelList.Push(Enumerable.Range(1, samples.Length + 1).ToArray());
            FaceRecognizer.Train(imageList, labelList);

            DialogResult = true;
            Close();
        }
        public bool TrainRecognizer()
        {
            var allFaces = _dataStoreAccess.CallFaces("ALL_USERS");

            if (allFaces != null)
            {
                var faceImages = new Image <Gray, byte> [allFaces.Count];
                var faceLabels = new int[allFaces.Count];
                for (int i = 0; i < allFaces.Count; i++)
                {
                    Stream stream = new MemoryStream();
                    stream.Write(allFaces[i].Image, 0, allFaces[i].Image.Length);
                    var faceImage = new Image <Gray, byte>(new Bitmap(stream));
                    faceImages[i] = faceImage.Resize(200, 200, Inter.Cubic);
                    faceLabels[i] = allFaces[i].UserId;
                }


                _faceRecognizer.Train(faceImages, faceLabels);
                _faceRecognizer.Save(_recognizerFilePath);
            }
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Loads the traing data given a (string) folder location
        /// </summary>
        /// <param name="Folder_location"></param>
        /// <returns></returns>
        private bool LoadTrainingData(string Folder_location)
        {
            if (File.Exists(Folder_location + "\\TrainedLabels.xml"))
            {
                try
                {
                    //message_bar.Text = "";
                    Names_List.Clear();
                    Names_List_ID.Clear();
                    trainingImages.Clear();
                    FileStream filestream = File.OpenRead(Folder_location + "\\TrainedLabels.xml");
                    long       filelength = filestream.Length;
                    byte[]     xmlBytes   = new byte[filelength];
                    filestream.Read(xmlBytes, 0, (int)filelength);
                    filestream.Close();

                    MemoryStream xmlStream = new MemoryStream(xmlBytes);

                    using (XmlReader xmlreader = XmlTextReader.Create(xmlStream))
                    {
                        while (xmlreader.Read())
                        {
                            if (xmlreader.IsStartElement())
                            {
                                switch (xmlreader.Name)
                                {
                                case "NAME":
                                    if (xmlreader.Read())
                                    {
                                        Names_List_ID.Add(Names_List.Count);     //0, 1, 2, 3....
                                        Names_List.Add(xmlreader.Value.Trim());
                                        NumLabels += 1;
                                    }
                                    break;

                                case "FILE":
                                    if (xmlreader.Read())
                                    {
                                        //PROBLEM HERE IF TRAININGG MOVED
                                        trainingImages.Add(new Image <Gray, byte>(Application.StartupPath + "\\TrainedFaces\\" + xmlreader.Value.Trim()));
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    ContTrain = NumLabels;

                    if (trainingImages.ToArray().Length != 0)
                    {
                        //Eigen face recognizer
                        //Parameters:
                        //      num_components – The number of components (read: Eigenfaces) kept for this Prinicpal
                        //          Component Analysis. As a hint: There’s no rule how many components (read: Eigenfaces)
                        //          should be kept for good reconstruction capabilities. It is based on your input data,
                        //          so experiment with the number. Keeping 80 components should almost always be sufficient.
                        //
                        //      threshold – The threshold applied in the prediciton. This still has issues as it work inversly to LBH and Fisher Methods.
                        //          if you use 0.0 recognizer.Predict will always return -1 or unknown if you use 5000 for example unknow won't be reconised.
                        //          As in previous versions I ignore the built in threhold methods and allow a match to be found i.e. double.PositiveInfinity
                        //          and then use the eigen distance threshold that is return to elliminate unknowns.
                        //
                        //NOTE: The following causes the confusion, sinc two rules are used.
                        //--------------------------------------------------------------------------------------------------------------------------------------
                        //Eigen Uses
                        //          0 - X = unknown
                        //          > X = Recognised
                        //
                        //Fisher and LBPH Use
                        //          0 - X = Recognised
                        //          > X = Unknown
                        //
                        // Where X = Threshold value


                        switch (Recognizer_Type)
                        {
                        case ("EMGU.CV.LBPHFaceRecognizer"):
                            recognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100);    //50
                            break;

                        case ("EMGU.CV.FisherFaceRecognizer"):
                            recognizer = new FisherFaceRecognizer(0, 3500);    //4000
                            break;

                        case ("EMGU.CV.EigenFaceRecognizer"):
                        default:
                            recognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
                            break;
                        }

                        recognizer.Train(trainingImages.ToArray(), Names_List_ID.ToArray());
                        // Recognizer_Type = recognizer.GetType();
                        // string v = recognizer.ToString(); //EMGU.CV.FisherFaceRecognizer || EMGU.CV.EigenFaceRecognizer || EMGU.CV.LBPHFaceRecognizer

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    Error = ex.ToString();
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }