Ejemplo n.º 1
0
        private void FisherFaceRecognition(object sender, EventArgs e)
        {
            Frame = _capture.QueryFrame().ToImage <Bgr, byte>();
            var frame = Frame.Resize(frameW, frameH, Inter.Cubic);

            grayFrame = frame.Convert <Gray, Byte>();
            var faces = cascadeClassifier.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty);

            foreach (var f in faces)
            {
                fisherFaceRecognizer = new FisherFaceRecognizer(Count, double.PositiveInfinity);
                fisherFaceRecognizer.Train(trainingImages.ToArray(), indexLabels.ToArray());

                var result = fisherFaceRecognizer.Predict(frame.Copy(f).Convert <Gray, Byte>().Resize(100, 100, Inter.Cubic));
                if (result.Label == -1)
                {
                    frame.Draw(f, new Bgr(Color.Red), 2);
                    frame.Draw("Unknown", new Point(f.X, f.Y - 10), font, 0.8, new Bgr(Color.Blue), 2, new LineType(), false);
                }
                else
                {
                    frame.Draw(f, new Bgr(Color.Green), 2);
                    frame.Draw(nameLabels[result.Label], new Point(f.X, f.Y - 10), font, 0.8, new Bgr(Color.Blue), 2, new LineType(), false);
                }
                alertMessage.Text = (alert + "เริ่มการ Face Recognition ด้วยวิธีการ " + RecognitionType.Text + " แล้ว \r\n" + "Distance " + result.Distance + "\r\n Faces " + faces.Length.ToString());
            }


            imgFrame.Image = frame.Resize(imgBoxW, imgBoxH, Inter.Cubic);
        }
        // Start training from the collected faces.
        // The face recognition algorithm can be one of these and perhaps more, depending on your version of OpenCV, which must be atleast v2.4.1:
        //    "FaceRecognizer.Eigenfaces":  Eigenfaces, also referred to as PCA (Turk and Pentland, 1991).
        //    "FaceRecognizer.Fisherfaces": Fisherfaces, also referred to as LDA (Belhumeur et al, 1997).
        //    "FaceRecognizer.LBPH":        Local Binary Pattern Histograms (Ahonen et al, 2006).
        public static BasicFaceRecognizer LearnCollectedFaces(List <Mat> preprocessedFaces, List <int> faceLabels, string facerecAlgorithm = "FaceRecognizer.Eigenfaces")
        {
            BasicFaceRecognizer model = null;

            Debug.Log("Learning the collected faces using the [" + facerecAlgorithm + "] algorithm ...");

            if (facerecAlgorithm == "FaceRecognizer.Fisherfaces")
            {
                model = FisherFaceRecognizer.create();
            }
            else if (facerecAlgorithm == "FaceRecognizer.Eigenfaces")
            {
                model = EigenFaceRecognizer.create();
            }

            if (model == null)
            {
                Debug.LogError("ERROR: The FaceRecognizer algorithm [" + facerecAlgorithm + "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer.");
                //exit(1);
            }

            // Do the actual training from the collected faces. Might take several seconds or minutes depending on input!
            MatOfInt labels = new MatOfInt();

            labels.fromList(faceLabels);
            model.train(preprocessedFaces, labels);

            return(model);
        }
Ejemplo n.º 3
0
 private bool LoadTrainingData()
 {
     mydb             = new DBConn();
     imagelabel       = mydb.getLabelNumList().ToArray();
     imageStringlabel = mydb.getLabelList().ToArray();
     trainingImages   = mydb.getTrainedImageList();
     Itrainingimage   = trainingImages;
     if (mydb.getImageCount() > 0)
     {
         if (trainingImages.Length != 0)
         {
             f_recognize = new FisherFaceRecognizer(0, 123.0);
             f_recognize.Train(Itrainingimage, imagelabel);
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
        public ImageService()
        {
            _fisherFaceRecognizer = new FisherFaceRecognizer();
            //_fisherFaceRecognizer = new FisherFaceRecognizer(FaceRecognizerNumberOfComponents, FaceRecognizerThreshold);

            _lbphFaceRecognizer = new LBPHFaceRecognizer();
            //_lbphFaceRecognizer = new LBPHFaceRecognizer(1, 8, 32, 32, 256);

            _cascadeClassifier = new CascadeClassifier(CascadeClassifierConfigPath);

            if (!Directory.Exists(DataFolder))
            {
                Directory.CreateDirectory(DataFolder);
            }

            if (File.Exists(FisherTrainingDataPath))
            {
                _fisherFaceRecognizer.Read(FisherTrainingDataPath);
            }

            if (File.Exists(LBPHTrainingDataPath))
            {
                _lbphFaceRecognizer.Read(LBPHTrainingDataPath);
            }
        }
        private static FaceRecognizer CreateRollCallRecognizer(int RollCallID)
        {
            //Tu rollcall ID, tao face recognizer, train
            //FaceRecognizer FaceRec = new LBPHFaceRecognizer(1, 8, 8, 8, 75);
            FaceRecognizer FaceRec = new FisherFaceRecognizer(80, RECOGNIZER_THREEHOLD);

            List <int> StudentIDs = new List <int>();
            List <Image <Gray, byte> > StudentImages = new List <Image <Gray, byte> >();

            RollCallBusiness RollBO = new RollCallBusiness();
            //Load danh sach student cua roll call
            RollCall RollCall = RollBO.GetRollCallByID(RollCallID);

            foreach (var Student in RollCall.Students)
            {
                foreach (var Image in Student.StudentImages)
                {
                    //Load ID va anh de train cho bo recognizer
                    StudentIDs.Add(Image.StudentID);
                    String             TrainingImagePath = TRAINING_FOLDER_PATH + "/" + Image.ImageLink;
                    Image <Gray, byte> TrainingImage     = new Image <Gray, byte>(TrainingImagePath);

                    TrainingImage._EqualizeHist();
                    StudentImages.Add(TrainingImage);
                }
            }

            FaceRec.Train(StudentImages.ToArray(), StudentIDs.ToArray());

            return(FaceRec);
        }
Ejemplo n.º 6
0
 public void Dispose()
 {
     f_recognize      = null;
     trainingImages   = null;
     imagelabel       = null;
     imageStringlabel = null;
     GC.Collect();
 }
Ejemplo n.º 7
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBoxAlgorithm.Text == "EigenFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces\\trainedDataEigen.dat";
                    eigenFaceRecognizer = new EigenFaceRecognizer(eigenTrainedImageCounter, 3000);
                    eigenFaceRecognizer.Load(dataDirectory);
                    richTextBox1.Text += "Trained Database Loaded.";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

            else if (comboBoxAlgorithm.Text == "FisherFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces\\trainedDataFisher.dat";

                    fisherFaceRecognizer = new FisherFaceRecognizer(fisherTrainedImageCounter, 3000);
                    fisherFaceRecognizer.Load(dataDirectory);
                    richTextBox1.Text += "Trained Database Loaded.";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

            else if (comboBoxAlgorithm.Text == "LBPHFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces\\trainedDataLBPH.dat";
                    lbphFaceRecognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 400);
                    lbphFaceRecognizer.Load(dataDirectory);
                    richTextBox1.Text += "Trained Database Loaded.";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Face recognition based on Fisher classifier using eigen
        /// fisher faces</summary>
        /// <param name="labels">The set of labels in the training set</param>
        /// <param name="trainingImages">The set of images(faces) in the
        /// training set</param>
        /// <param name="face">The face detected in gray scale
        /// to be recognized. The dimension of the image must be
        /// equal to the dimension of the images in the training set</param>
        /// <returns>A string representing the label of the face recognized
        /// or an empty string if no matches were found</returns>
        public String recognizeFisherFace(List <String> labels,
                                          List <Image <Gray, Byte> > trainingImages,
                                          Bitmap face)
        {
            String label = String.Empty;

            InitParams();
            Image <Bgr, Byte>  imageEmgu     = new Image <Bgr, Byte>(face);
            Image <Gray, Byte> extractedFace = imageEmgu.Convert <Gray, Byte>().Copy().Resize(
                100, 100, INTER.CV_INTER_CUBIC);

            extractedFace._EqualizeHist();

            if (trainingImages.ToArray().Length != 0)
            {
                FisherFaceRecognizer recognizer = new FisherFaceRecognizer(0, Treshold);
                int[] labelsInt = new int[labels.ToArray().Length];
                for (int i = 0; i < labels.ToArray().Length; i++)
                {
                    labelsInt[i] = i;
                }
                recognizer.Train(trainingImages.ToArray(), labelsInt.ToArray());
                FisherFaceRecognizer.PredictionResult pr;
                pr = recognizer.Predict(extractedFace);
                if (pr.Label != -1)
                {
                    label                   = labels[pr.Label];
                    MostSimilarFace         = trainingImages[pr.Label];
                    MostSimilarFaceIndex    = pr.Label;
                    MostSimilarFaceDistance = (float)pr.Distance;
                    MostSimilarFaceLabel    = labels[pr.Label];
                }
                else
                {
                    recognizer = new FisherFaceRecognizer(0, 10000);
                    recognizer.Train(trainingImages.ToArray(), labelsInt.ToArray());
                    pr = recognizer.Predict(extractedFace);
                    MostSimilarFace         = trainingImages[pr.Label];
                    MostSimilarFaceIndex    = pr.Label;
                    MostSimilarFaceDistance = (float)pr.Distance;
                    MostSimilarFaceLabel    = labels[pr.Label];
                }
            }
            return(label);
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            var images = new List <ImageInfo>();

            var imageId = 0;

            foreach (var dir in new DirectoryInfo(@"..\..\Images").GetDirectories())
            {
                var groupId = int.Parse(dir.Name.Replace("s", string.Empty)) - 1;
                foreach (var imageFile in dir.GetFiles("*.pgm"))
                {
                    images.Add(new ImageInfo
                    {
                        Image        = new Mat(imageFile.FullName, ImreadModes.Grayscale),
                        ImageId      = imageId++,
                        ImageGroupId = groupId
                    });
                }
            }

            var model = FisherFaceRecognizer.Create();

            model.Train(images.Select(x => x.Image), images.Select(x => x.ImageGroupId));

            var rnd           = new Random();
            var randomImageId = rnd.Next(0, images.Count - 1);
            var testSample    = images[randomImageId];

            Console.WriteLine("Actual group: {0}", testSample.ImageGroupId);
            Cv2.ImShow("actual", testSample.Image);

            var predictedGroupId = model.Predict(testSample.Image);

            Console.WriteLine("Predicted group: {0}", predictedGroupId);

            Cv2.WaitKey(0);
        }
        /// <summary>
        /// Raises the load button click event.
        /// </summary>
        public void LoadModel()
        {
            string loadDirectoryPath = Path.Combine(Application.persistentDataPath, saveDirectoryName);

            if (!Directory.Exists(loadDirectoryPath))
            {
                Debug.Log("load failure. saved train data file does not exist.");
                return;
            }

            // Restart everything!
            dispose();

            if (facerecAlgorithm == "FaceRecognizer.Fisherfaces")
            {
                model = FisherFaceRecognizer.create();
            }
            else if (facerecAlgorithm == "FaceRecognizer.Eigenfaces")
            {
                model = EigenFaceRecognizer.create();
            }

            if (model == null)
            {
                Debug.LogError("ERROR: The FaceRecognizer algorithm [" + facerecAlgorithm + "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer.");
                m_mode = R_MODES.MODE_DETECTION;
                return;
            }

            // load the train data.
            model.read(Path.Combine(loadDirectoryPath, "traindata.yml"));

            int maxLabel = (int)Core.minMaxLoc(model.getLabels()).maxVal;

            if (maxLabel < 0)
            {
                Debug.Log("load failure.");
                model.Dispose();
                return;
            }

            // Restore the save data.
            #if UNITY_WEBGL && !UNITY_EDITOR
            string format = "jpg";
            #else
            string format = "png";
            #endif
            m_numPersons = maxLabel + 1;
            personsNames = new string[m_numPersons];

            for (int i = 0; i < m_numPersons; ++i)
            {
                personsNames[i] = GameManager.instance.personsNames[i];

                m_latestFaces.Add(i);
                preprocessedFaces.Add(Imgcodecs.imread(Path.Combine(loadDirectoryPath, "preprocessedface" + i + "." + format), 0));
                if (preprocessedFaces[i].total() == 0)
                {
                    preprocessedFaces[i] = new Mat(faceHeight, faceWidth, CvType.CV_8UC1, new Scalar(128));
                }
                faceLabels.Add(i);
            }


            // go to the recognition mode!
            m_mode = R_MODES.MODE_RECOGNITION;
        }
Ejemplo n.º 11
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBoxAlgorithm.Text == "EigenFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces";

                    string[] files = Directory.GetFiles(dataDirectory, "*.jpg", SearchOption.AllDirectories);
                    eigenTrainedImageCounter = 0;
                    foreach (var file in files)
                    {
                        Image <Bgr, Byte> TrainedImage = new Image <Bgr, Byte>(file);
                        if (eqHisChecked.Checked == true)
                        {
                            TrainedImage._EqualizeHist();
                        }
                        eigenTrainingImages.Add(TrainedImage.Convert <Gray, Byte>());
                        eigenlabels.Add(fileName(file));
                        eigenIntlabels.Add(eigenTrainedImageCounter);
                        eigenTrainedImageCounter++;
                        richTextBox1.Text += fileName(file) + "\n";
                    }

                    /*
                     *  //TermCriteria for face recognition with numbers of trained images like maxIteration
                     *  MCvTermCriteria termCrit = new MCvTermCriteria(eigenTrainedImageCounter, 0.001);
                     *
                     *  //Eigen face recognizer
                     *  eigenObjRecognizer=new EigenObjectRecognizer(
                     *    eigenTrainingImages.ToArray(),
                     *    eigenlabels.ToArray(),
                     *    3000,
                     *    ref termCrit);
                     */
                    eigenFaceRecognizer = new EigenFaceRecognizer(eigenTrainedImageCounter, 2000);
                    eigenFaceRecognizer.Train(eigenTrainingImages.ToArray(), eigenIntlabels.ToArray());
                    //eigenFaceRecognizer.Save(dataDirectory + "\\trainedDataEigen.dat");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

            else if (comboBoxAlgorithm.Text == "FisherFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces";

                    string[] files = Directory.GetFiles(dataDirectory, "*.jpg", SearchOption.AllDirectories);
                    fisherTrainedImageCounter = 0;
                    foreach (var file in files)
                    {
                        Image <Bgr, Byte> TrainedImage = new Image <Bgr, Byte>(file);
                        fisherTrainingImages.Add(TrainedImage.Convert <Gray, Byte>());
                        if (eqHisChecked.Checked == true)
                        {
                            TrainedImage._EqualizeHist();
                        }
                        fisherlabels.Add(fileName(file));
                        fisherIntlabels.Add(fisherTrainedImageCounter);
                        fisherTrainedImageCounter++;
                        richTextBox1.Text += fileName(file) + "\n";
                    }
                    fisherFaceRecognizer = new FisherFaceRecognizer(fisherTrainedImageCounter, 2000);
                    fisherFaceRecognizer.Train(fisherTrainingImages.ToArray(), fisherIntlabels.ToArray());
                    //fisherFaceRecognizer.Save(dataDirectory + "\\trainedDataFisher.dat");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }

            else if (comboBoxAlgorithm.Text == "LBPHFaces")
            {
                try
                {
                    string dataDirectory = Directory.GetCurrentDirectory() + "\\TrainedFaces";

                    string[] files = Directory.GetFiles(dataDirectory, "*.jpg", SearchOption.AllDirectories);
                    lbphTrainedImageCounter = 0;
                    foreach (var file in files)
                    {
                        Image <Bgr, Byte> TrainedImage = new Image <Bgr, Byte>(file);
                        if (eqHisChecked.Checked == true)
                        {
                            TrainedImage._EqualizeHist();
                        }
                        lbphTrainingImages.Add(TrainedImage.Convert <Gray, Byte>());
                        lbphlabels.Add(fileName(file));
                        lbphIntlabels.Add(lbphTrainedImageCounter);
                        lbphTrainedImageCounter++;
                        richTextBox1.Text += fileName(file) + "\n";
                    }
                    lbphFaceRecognizer = new LBPHFaceRecognizer(1, 8, 8, 8, 400);
                    lbphFaceRecognizer.Train(lbphTrainingImages.ToArray(), lbphIntlabels.ToArray());
                    lbphFaceRecognizer.Save(dataDirectory + "\\trainedDataLBPH.dat");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }