Пример #1
0
        private void LoadTrainnedFace()
        {
            try
            {
                LabelList.Clear();
                TrainingImages.Clear();

                // Load haarcascades for face detection
                Face = new HaarCascade("haarcascade_frontalface_default.xml");

                // Load the number of trained faces
                var files = GetAllFiles(Application.StartupPath + "/TrainedFaces");
                TrainedFacesCounter = files.Count;

                foreach (var file in files)
                {
                    TrainingImages.Add(new Image <Gray, byte>(file.FullName));
                    LabelList.Add(file.Name.Split('@')[0]);
                }
            }
            catch
            {
                // By pass
            }
        }
Пример #2
0
        public bool AddTrainingImage(string filename)
        {
            if (File.Exists(filename))
            {
                if (!TrainingImages.Contains(filename))
                {
                    var fn        = Path.GetFileNameWithoutExtension(filename);
                    var extension = Path.GetExtension(filename);
                    var fn_mark   = Path.Combine(Path.GetDirectoryName(filename), fn + "_train" + extension);

                    if (File.Exists(fn_mark))
                    {
                        MarkedTrainingImages.Add(fn_mark);
                        TrainingImages.Add(filename);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        private void FaceMonitoring(object sender, EventArgs e)
        {
            try
            {
                // Get the current frame form capture device
                CurrentFrame = Grabber.QueryFrame().Resize(320, 240, INTER.CV_INTER_CUBIC);

                //Video.Serve(CurrentFrame.Bytes);
                //System.Diagnostics.Debug.Print(Convert.ToBase64String(CurrentFrame.Bytes));

                // Convert it to Grayscale
                Gray = CurrentFrame.Convert <Gray, byte>();

                // Face Detector
                MCvAvgComp[][] facesDetected = Gray.DetectHaarCascade(Face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));

                if (facesDetected == null)
                {
                    return;
                }

                // Action for each element detected
                foreach (MCvAvgComp face in facesDetected[0])
                {
                    Result = CurrentFrame.Copy(face.rect).Convert <Gray, byte>().Resize(100, 100, INTER.CV_INTER_CUBIC);

                    // Draw the face detected in the 0th (gray) channel with blue color
                    CurrentFrame.Draw(face.rect, new Bgr(Color.Red), 2);

                    // Check if there is trained images to find a match
                    if (TrainingImages.ToArray().Length != 0)
                    {
                        var eigenDistanceThreshold = 3000;
                        var eps = 0.001;

                        // TermCriteria for face recognition with numbers of trained images like maxIteration
                        MCvTermCriteria termCrit = new MCvTermCriteria(TrainedFacesCounter, eps);

                        // Eigen face recognizer
                        FaceRecognitionEngine recognizer = new FaceRecognitionEngine(TrainingImages.ToArray(), LabelList.ToArray(), eigenDistanceThreshold, ref termCrit);
                        var imgLabel = recognizer.Recognize(Result);
                        LogStats($"{imgLabel} - Recognized");

                        // Draw the label for each face detected and recognized
                        // CurrentFrame.Draw(imgLabel, ref Font, new Point(face.rect.X - 2, face.rect.Y - 2), new Bgr(Color.White));
                        MethodInvoker inv = delegate { lblLabelName.Text = imgLabel.Replace("_", " ").ToUpper(); };
                        Invoke(inv);
                    }
                }

                // Show the face procesed and recognized
                imageBoxFrameGrabber.Image = CurrentFrame;
            }
            catch
            {
            }
        }
Пример #4
0
        void button2Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                MessageBox.Show("vui long nhap ten truoc khi luu....");
                return;
            }
            else
            {
                try
                {
                    ContTrain = ContTrain + 1;
                    grey      = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                    MCvAvgComp[][] facesDetected = gray.DetectHaarCasscade(
                        face,
                        1.2,
                        10,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUMING,
                        new Size(20, 20));

                    foreach (MCvAvgComp f in faceDetected[0])
                    {
                        TraniedFace = currentFrame.Copy(f.rect).Convert <Gray, byte>();
                        break;
                    }
                    TrainedFace = result.Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                    TrainingImages.Add(TrainedFace);
                    labels.Add(textBox1.Text);

                    imageBox1.Image = TrainedFace;

                    File.writeAllText(Application.StartupPath + "/TrainedFaces/Trainedlabels.txt", trainingImages.ToArray().Length.ToString() + "%");
                    for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
                    {
                        rainingImages.ToArray()[i - 1].Save(Application.StartupPath + "TrainedFaces/face" + i + ".bmp");
                        File.AppendAllText(Application.StartupPath + "/TrainedFaces/Trainedlabels.txt", labels.ToArray()[i - 1] + "%");
                    }
                    MessageBox.Show("khuon mat duoc phat hien va them vao", "Training ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("vui long chon chuc nang phat hien khuon mat truoc", "Training fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
Пример #5
0
 public void Clear()
 {
     TrainingImages.Clear();
     MarkedTrainingImages.Clear();
     RecoginzingImages.Clear();
 }