Face Recognizer
Inheritance: Emgu.Util.UnmanagedObject
 public static void InitializeFaceRecognizer()
 {
     _faceRecognizer?.Dispose();
     _faceRecognizer = new LBPHFaceRecognizer(Properties.Settings.Default.RecognitionRadius, Properties.Settings.Default.RecognitionNeighbours, 8, 8, Properties.Settings.Default.RecognitionThreshold);
     if (!File.Exists(Properties.Settings.Default.RecognitionTrainFile))
     {
         try
         {
             Directory.CreateDirectory(Path.GetDirectoryName(Properties.Settings.Default.RecognitionTrainFile));
             File.Create(Properties.Settings.Default.RecognitionTrainFile).Close();
         }
         catch (Exception ex)
         {
             Debug.WriteLine("Could not create recognition file: " + ex);
         }
     }
     else
     {
         try
         {
             _faceRecognizer.Load(Properties.Settings.Default.RecognitionTrainFile);
             _trained = true;
         }
         catch (Exception ex)
         {
             Debug.WriteLine("Could not load recognition file: " + ex);
         }
     }
 }
Exemplo n.º 2
0
 public TrainData()
 {
     InitializeComponent();
     fr1 = new EigenFaceRecognizer(80, double.PositiveInfinity);//The recognitoion object
     fr2 = new FisherFaceRecognizer(-1, 3100);//The recognitoion object
     fr3 = new LBPHFaceRecognizer(1, 8, 8, 8, 100);//50
     cascade = new CascadeClassifier("haarcascade_frontalface_default.xml"); //this file contains the training 
 }
Exemplo n.º 3
0
        public void dispose()
        {
            // Nullify all initialized variables
            recognizer = null;
            trainingImages = null;
            namesList = null;
            namesListId = null;
            error = null;

            // Garbage collect everything
            GC.Collect();
        }
Exemplo n.º 4
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);
                }
            }
        }
Exemplo n.º 5
0
        public void loadRecognizer(string fileName)
        {
            // Reinitialize the recognizer and load in the file
            recognizer = new LBPHFaceRecognizer(1, 8, 8, 9, 100);
            recognizer.Load(fileName);

            string directory = Path.GetDirectoryName(fileName);

            // Purge all existing name labels
            namesList.Clear();

            if (File.Exists(directory + "/TrainedLabels.xml"))
            {
                // Read the file into memory
                FileStream fileStream = File.OpenRead(directory + "/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())
                                    {
                                        namesList.Add(xmlReader.Value.Trim());
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            trainStatus = true;
        }
        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;
            }
        }
 public void Dispose()
 {
     recognizer = null;
     trainingImages = null;
     allname = null;
     Error = null;
     GC.Collect();
 }
        /// <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;
        }
 /// <summary>
 /// Dispose of Class call Garbage Collector
 /// </summary>
 public void Dispose()
 {
     recognizer = null;
     trainingImages = null;
     Names_List = null;
     Error = null;
     GC.Collect();
 }
        /// <summary>
        /// Loads the trained Eigen Recogniser from specified location
        /// </summary>
        /// <param name="filename"></param>
        public void Load_Eigen_Recogniser(string filename)
        {
            //Lets get the recogniser type from the file extension
            string ext = Path.GetExtension(filename);
            switch (ext)
            {
                case (".LBPH"):
                    Recognizer_Type = "EMGU.CV.LBPHFaceRecognizer";
                    recognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100);//50
                    break;
                case (".FFR"):
                    Recognizer_Type = "EMGU.CV.FisherFaceRecognizer";
                    recognizer = new FisherFaceRecognizer(0, 3500);//4000
                    break;
                case (".EFR"):
                    Recognizer_Type = "EMGU.CV.EigenFaceRecognizer";
                    recognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
                    break;
            }

            //introduce error checking
            recognizer.Load(filename);

            //Now load the labels
            string direct = Path.GetDirectoryName(filename);
            Names_List.Clear();
            if (File.Exists(direct + "/Labels.xml"))
            {
                FileStream filestream = File.OpenRead(direct + "/Labels.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.Add(xmlreader.Value.Trim());
                                    }
                                    break;
                            }
                        }
                    }
                }
                ContTrain = NumLabels;
            }
            _IsTrained = true;

        }
 private void LoadRecognizer()
 {
     switch (_faceRecognitionType)
     {
         case FaceRecognitionType.LBPHFaceRecognizer:
             _faceRecognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100);
             break;
         case FaceRecognitionType.FisherFaceRecognizer:
             _faceRecognizer = new FisherFaceRecognizer(0, 3500);
             break;
         case FaceRecognitionType.EigenFaceRecognizer:
             _faceRecognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
             break;
     }
 }
Exemplo n.º 12
0
 public FaceRecognizerService()
 {
     _faceRecognizer = new LBPHFaceRecognizer(2, 8, 6, 6, 50);
     _genderFaceRecognizer = new FisherFaceRecognizer();
     //_genderFaceRecognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100);
 }
Exemplo n.º 13
0
    private void InitEigenFaceRecognizer() {
      lock (this) {
        if (Recognizer != null) { return; }

        Threshold = ConfigManager.GetInstance().Find("face.Eigen_Threshold", 0);
        if (Threshold > 0) {
          Recognizer = new EigenFaceRecognizer(80, double.PositiveInfinity);
        }

        Threshold = ConfigManager.GetInstance().Find("face.Fisher_Threshold", 0);
        if (Threshold > 0) {
          Recognizer = new FisherFaceRecognizer(0, 3500);
        }

        Threshold = ConfigManager.GetInstance().Find("face.LBP_Threshold", 0);
        if (Threshold > 0) {
          Recognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 100);
        }

        if (trainedImages.Count <= 0) { return; }

        // Train Images and Labels
        Recognizer.Train(trainedImages.ToArray(), trainedLabelIds.ToArray());
        trained = true;
      }
    }
Exemplo n.º 14
0
 private void ResetEigenObjectRecognizer(){
   Recognizer = null;
   trained = false;
   InitEigenFaceRecognizer();
 }
Exemplo n.º 15
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;
            }
        }