Train() публичный Метод

Train the face recognizer with the specific images and labels
public Train ( IImage images, int labels ) : void
images IImage The images used in the training
labels int The labels of the images
Результат void
Пример #1
0
        public Form1()
        {
            InitializeComponent();
            recognizer = new LBPHFaceRecognizer(1, 8, 8, 9, 65);

            classifier = new CascadeClassifier(haarcascade);
            GPU_classifier = new GpuCascadeClassifier(haarcascade_cuda);

            font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5, 0.5);
            if (File.Exists(@"traningdata.xml"))
            {
                recognizer.Load(@"traningdata.xml");
            }
            else
            {

                foreach (var file in Directory.GetFiles(Application.StartupPath + @"\Traning Faces\"))
                {
                    try { temp = new Image<Gray, Byte>(file); }
                    catch { continue; }
                    temp._EqualizeHist();

                    var detectedFaces = classifier.DetectMultiScale(temp, 1.1, 15, new Size(24, 24), Size.Empty);
                    if (detectedFaces.Length == 0)
                    {
                        continue;
                    }

                    temp.ROI = detectedFaces[0];
                    temp = temp.Copy();
                    temp = temp.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                    imagesList.Add(temp);
                    imagesLabels.Add(Path.GetFileNameWithoutExtension(file));
                }
                for (int i = 0; i < imagesList.Count; i++)
                {
                    imagesLabels_indices.Add(i);
                }

                try { recognizer.Train(imagesList.ToArray(), imagesLabels_indices.ToArray()); }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Environment.Exit(0);
                }
            }
        }
        private bool LoadTrainingData()
        {
            mydb = new DBConn();
            allname = mydb.getLabelList();           
            trainingImages = mydb.getTrainedImageList();
            int[] temp = Enumerable.Range(0,(allname.Count)).ToArray();
            if (mydb.getImageCount() > 0)
            {

                if (trainingImages.Length != 0)
                {
                    //set round and ...
                    //termCrit = new MCvTermCriteria(mydb.getImageCount(), 0.001);
                    //Eigen face recognizer
                    recognizer = new FisherFaceRecognizer(0,3200);//4000
                    recognizer.Train(trainingImages, temp);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <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;
        }
Пример #4
0
        private bool loadTrainingData(string folderLocation)
        {
            if (File.Exists(folderLocation + "/TrainedLabels.xml"))
            {
                try
                {
                    // Purge all existing data
                    namesList.Clear();
                    trainingImages.Clear();

                    // Read the XML file into memory
                    FileStream fileStream = File.OpenRead(folderLocation + "/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())
                                        {
                                            // Add the name to the data set
                                            namesList.Add(xmlReader.Value.Trim());

                                            // Store an identifier. This is merely the position in namesList
                                            namesListId.Add((namesList.Count));

                                            // Increment the counter for entries in the data set
                                            numLabels += 1;
                                        }
                                        break;

                                    case "FILE":
                                        if (xmlReader.Read())
                                        {
                                            // Add the image to the data set
                                            trainingImages.Add(new Image<Gray, Byte>(System.AppDomain.CurrentDomain.BaseDirectory + "/TrainedFaces/" + xmlReader.Value.Trim()));
                                        }
                                        break;
                                }
                            }
                        }
                    }

                    if (trainingImages.ToArray().Length != 0)
                    {
                        recognizer = new LBPHFaceRecognizer(1, 8, 8, 9, 100);
                        recognizer.Train(trainingImages.ToArray(), namesListId.ToArray());

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