示例#1
0
        private void SavePicture(object sender, EventArgs e)
        {
            try
            {
                TrainedFacesCounter++;

                // Get a gray frame from capture device
                Gray = Grabber.QueryGrayFrame().Resize(320, 240, INTER.CV_INTER_CUBIC);

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

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

                LogIt($"Handle frame - scaleFactor:{scaleFactor} minNeighbors:{minNeighbors} detectionType:{detectionType.ToString()}");

                string labelName = textBox1.Text.Trim().Replace(" ", "_").ToLower();

                // Resize face detected image in order to force to compare the same size with the
                // Test image with cubic interpolation type method
                TrainedFace = Result.Resize(100, 100, INTER.CV_INTER_CUBIC);

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

                var imgPath = $"/TrainedFaces/{labelName}@face{DateTime.Now.ToString("yyyymmddhhmmssmm")}.bmp";
                TrainedFace.Save(Application.StartupPath + imgPath);
            }
            catch { }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                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;
                }

                TrainedFace.Save(Application.StartupPath + "/face" + 1 + ".bmp");
                if (SendIamge(Image.FromFile(Application.StartupPath + "/face" + 1 + ".bmp")))
                {
                    File.Delete(Application.StartupPath + "/face" + 1 + ".bmp");
                    MessageBox.Show(UserLogin.Username + "´s face detected and added :)", "Training OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Some Thing Go Wrong Contact Support Now..", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch
            {
                MessageBox.Show("Enable the face detection first", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }