public string SaveString(string inputpath, string label, ref int index)
        {
            try
            {
                ContTrain = ContTrain + 1;
                bool detected = false;
                gray = new Image <Gray, byte>(inputpath);

                MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
                    face,
                    1.2,
                    10,
                    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                    new Size(20, 20));

                foreach (MCvAvgComp f in facesDetected[0])
                {
                    TrainedFace = gray.Copy(f.rect).Convert <Gray, byte>();
                    detected    = true;
                    break;
                }

                if (!detected)
                {
                    return(string.Empty);
                }

                TrainedFace = TrainedFace.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                TrainedFace = new Image <Gray, byte>(ImageProcessing.ImagePreProcessing(TrainedFace.ToBitmap()));

                trainingImages.Add(TrainedFace);
                labels.Add(label);



                UpdateRecognizer();

                return(BasicOperations.SaveImage(TrainedFace.ToBitmap(), ref index));;
            }
            catch
            {
                return(string.Empty);
            }
        }
Пример #2
0
        private void btnBroweImg_Click(object sender, EventArgs e)
        {
            if (frmLogin.admin == false)
            {
                frmLogin loginForm = new frmLogin();
                loginForm.Show();
                return;
            }
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter name first.");
                return;
            }
            openFileDialog1.FileName = String.Empty;
            openFileDialog1.Filter   = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            openFileDialog1.Title    = "Please select an image file to training.";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                grabber      = new Capture(openFileDialog1.FileName);
                currentFrame = grabber.QueryFrame().Resize(473, 355, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                //Trained face counter
                ContTrain = ContTrain + 1;

                //Get a gray frame from capture device
                gray = currentFrame.Convert <Gray, Byte>();
                //gray = grabber.QueryGrayFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

                //Face Detector
                MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
                    face,
                    1.2,
                    10,
                    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                    new Size(20, 20));

                //Action for each element detected
                foreach (MCvAvgComp f in facesDetected[0])
                {
                    TrainedFace = currentFrame.Copy(f.rect).Convert <Gray, byte>();
                    break;
                }

                //resize face detected image for force to compare the same size with the
                //test image with cubic interpolation type method
                TrainedFace = TrainedFace.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                trainingImages.Add(TrainedFace);
                labels.Add(txtName.Text + "|" + (ContTrain).ToString());

                //Show face added in gray scale
                imageBox1.Image = TrainedFace;

                //Write the number of triained faces in a file text for further load
                File.WriteAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", trainingImages.ToArray().Length.ToString() + "%");

                //Write the labels of triained faces in a file text for further load
                for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
                {
                    trainingImages.ToArray()[i - 1].Save(Application.StartupPath + "/TrainedFaces/face" + i + ".bmp");
                    File.AppendAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt", labels.ToArray()[i - 1] + "%");
                }

                MessageBox.Show(txtName.Text + "´s face detected and added :)", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Text = "";
            }
        }