Emgu.CV.FaceRecognizer.Train C# (CSharp) Method

Train() public method

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
return void
        public void Train(IImage[] images, int[] labels)
        {
            Debug.Assert(images.Length == labels.Length, "The number of labels must equals the number of images");

             IntPtr[] ptrs = new IntPtr[images.Length];
             for (int i = 0; i < images.Length; i++)
             {
            ptrs[i] = images[i].Ptr;
             }

             GCHandle imagesHandle = GCHandle.Alloc(ptrs, GCHandleType.Pinned);
             GCHandle labelsHandle = GCHandle.Alloc(labels, GCHandleType.Pinned);

             try
             {
            CvInvoke.CvFaceRecognizerTrain(_ptr, imagesHandle.AddrOfPinnedObject(), labelsHandle.AddrOfPinnedObject(), images.Length);
             }
             finally
             {
            imagesHandle.Free();
            labelsHandle.Free();
             }
        }

Usage Example

Exemplo n.º 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);
                }
            }
        }
All Usage Examples Of Emgu.CV.FaceRecognizer::Train