Пример #1
0
        private void btnDetectFaces_Click(object sender, RoutedEventArgs e)
        {
            //Convert from Bgr to Gray Image
            if (picCapture.Source != null)
            {
                CvInvoke.CvtColor(Image, grayImage, ColorConversion.Bgr2Gray);
                //Enchance the image to get better result
                CvInvoke.EqualizeHist(grayImage, grayImage);

                System.Drawing.Rectangle[] faces = faceCascadeClassifier.DetectMultiScale(grayImage, 1.1, 3, System.Drawing.Size.Empty, System.Drawing.Size.Empty);

                if (faces.Length > 0)
                {
                    foreach (var face in faces)
                    {
                        CvInvoke.Rectangle(Image, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 5);
                        Image <Bgr, Byte> resultImage = Image.Convert <Bgr, Byte>();
                        picCapture.Source = BitmapHelpers.ToBitmapSource(resultImage);
                    }
                }
            }
            else
            {
                MessageBox.Show("Choose photo");
            }
        }
Пример #2
0
        private void btnRecognizeFaces_Click(object sender, RoutedEventArgs e)
        {
            if (picCapture.Source != null)
            {
                CvInvoke.CvtColor(Image, grayImage, ColorConversion.Bgr2Gray);
                //Enchance the image to get better result
                CvInvoke.EqualizeHist(grayImage, grayImage);

                System.Drawing.Rectangle[] faces = faceCascadeClassifier.DetectMultiScale(grayImage, 1.1, 3, System.Drawing.Size.Empty, System.Drawing.Size.Empty);

                if (faces.Length > 0)
                {
                    foreach (var face in faces)
                    {
                        CvInvoke.Rectangle(Image, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 4);

                        Image <Bgr, Byte> resultImage = Image.Convert <Bgr, Byte>();
                        resultImage.ROI = face;


                        Image <Gray, Byte> grayFaceResult = resultImage.Convert <Gray, Byte>().Resize(200, 200, Inter.Cubic);
                        CvInvoke.EqualizeHist(grayFaceResult, grayFaceResult);
                        var result = recognizer.Predict(grayFaceResult);

                        //Debug.WriteLine(result.Label + ". " + result.Distance);
                        //Here results found known faces
                        if (result.Label != -1 && result.Distance < 2000)
                        {
                            CvInvoke.PutText(Image, PersonsNames[result.Label], new System.Drawing.Point(face.X - 2, face.Y - 2),
                                             FontFace.HersheyComplex, 2.0, new Bgr(System.Drawing.Color.Green).MCvScalar);
                            CvInvoke.Rectangle(Image, face, new Bgr(System.Drawing.Color.Blue).MCvScalar, 5);
                        }
                        //here results did not found any know faces
                        else
                        {
                            CvInvoke.PutText(Image, "Unknown", new System.Drawing.Point(face.X - 2, face.Y - 2),
                                             FontFace.HersheyComplex, 4.0, new Bgr(System.Drawing.Color.Orange).MCvScalar);
                            CvInvoke.Rectangle(Image, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                        }

                        resultImage       = Image.Convert <Bgr, Byte>();
                        picCapture.Source = BitmapHelpers.ToBitmapSource(resultImage);
                    }
                }
                else
                {
                    MessageBox.Show("Choose photo");
                }
            }
        }
Пример #3
0
        private void btnImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();

            op.Title  = "Select a picture";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                        "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                        "Portable Network Graphic (*.png)|*.png";
            if (op.ShowDialog() == true)
            {
                Image             = new Image <Bgr, byte>(op.FileName);
                picCapture.Source = BitmapHelpers.ToBitmapSource(Image);
            }
        }
Пример #4
0
        private void ProccesFrame(object sender, EventArgs e)
        {
            videoCapture.Retrieve(frame, 0);
            if (!picCapture.CheckAccess())
            {
                picCapture.Dispatcher.Invoke(new Action(delegate()
                {
                    currentFrame = frame.ToImage <Bgr, Byte>().Resize(Convert.ToInt32(picCapture.Width), Convert.ToInt32(picCapture.Height), Inter.Cubic);
                }));
            }
            else
            {
                currentFrame = frame.ToImage <Bgr, Byte>().Resize(Convert.ToInt32(picCapture.Width), Convert.ToInt32(picCapture.Height), Inter.Cubic);
            }

            //detect faces
            if (!picCapture.CheckAccess())
            {
                picCapture.Dispatcher.Invoke(new Action(delegate()
                {
                    if (faceDetectionEnabled)
                    {
                        //Convert from Bgr to Gray Image
                        Mat grayImage = new Mat();
                        CvInvoke.CvtColor(currentFrame, grayImage, ColorConversion.Bgr2Gray);
                        //Enchance the image to get better result
                        CvInvoke.EqualizeHist(grayImage, grayImage);

                        System.Drawing.Rectangle[] faces = faceCascadeClassifier.DetectMultiScale(grayImage, 1.1, 3, System.Drawing.Size.Empty, System.Drawing.Size.Empty);

                        if (faces.Length > 0)
                        {
                            foreach (var face in faces)
                            {
                                CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);

                                Image <Bgr, Byte> resultImage = currentFrame.Convert <Bgr, Byte>();
                                resultImage.ROI = face;

                                if (faceRecognationEnabled)
                                {
                                    Image <Gray, Byte> grayFaceResult = resultImage.Convert <Gray, Byte>().Resize(200, 200, Inter.Cubic);
                                    CvInvoke.EqualizeHist(grayFaceResult, grayFaceResult);
                                    var result = recognizer.Predict(grayFaceResult);

                                    //Debug.WriteLine(result.Label + ". " + result.Distance);
                                    //Here results found known faces
                                    if (result.Label != -1 && result.Distance < 2000)
                                    {
                                        CvInvoke.PutText(currentFrame, PersonsNames[result.Label], new System.Drawing.Point(face.X - 2, face.Y - 2),
                                                         FontFace.HersheyComplex, 1.0, new Bgr(System.Drawing.Color.Orange).MCvScalar);
                                        CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Green).MCvScalar, 2);
                                    }
                                    //here results did not found any know faces
                                    else
                                    {
                                        CvInvoke.PutText(currentFrame, "Unknown", new System.Drawing.Point(face.X - 2, face.Y - 2),
                                                         FontFace.HersheyComplex, 1.0, new Bgr(System.Drawing.Color.Orange).MCvScalar);
                                        CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                                    }
                                }
                            }
                        }
                    }
                }));
            }


            else
            {
                if (faceDetectionEnabled)
                {
                    //Convert from Bgr to Gray Image
                    Mat grayImage = new Mat();
                    CvInvoke.CvtColor(currentFrame, grayImage, ColorConversion.Bgr2Gray);
                    //Enchance the image to get better result
                    CvInvoke.EqualizeHist(grayImage, grayImage);

                    System.Drawing.Rectangle[] faces = faceCascadeClassifier.DetectMultiScale(grayImage, 1.1, 3, System.Drawing.Size.Empty, System.Drawing.Size.Empty);

                    if (faces.Length > 0)
                    {
                        foreach (var face in faces)
                        {
                            CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);

                            Image <Bgr, Byte> resultImage = currentFrame.Convert <Bgr, Byte>();
                            resultImage.ROI = face;


                            if (faceRecognationEnabled)
                            {
                                Image <Gray, Byte> grayFaceResult = resultImage.Convert <Gray, Byte>().Resize(200, 200, Inter.Cubic);
                                CvInvoke.EqualizeHist(grayFaceResult, grayFaceResult);
                                var result = recognizer.Predict(grayFaceResult);

                                Debug.WriteLine(result.Label + ". " + result.Distance);
                                //Here results found known faces
                                if (result.Label != -1 && result.Distance < 2000)
                                {
                                    CvInvoke.PutText(currentFrame, PersonsNames[result.Label], new System.Drawing.Point(face.X - 2, face.Y - 2),
                                                     FontFace.HersheyComplex, 1.0, new Bgr(System.Drawing.Color.Orange).MCvScalar);
                                    CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Green).MCvScalar, 2);
                                }
                                //here results did not found any know faces
                                else
                                {
                                    CvInvoke.PutText(currentFrame, "Unknown", new System.Drawing.Point(face.X - 2, face.Y - 2),
                                                     FontFace.HersheyComplex, 1.0, new Bgr(System.Drawing.Color.Orange).MCvScalar);
                                    CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                                }
                            }
                        }
                    }
                }
            }



            //Render the video capture into Picture Box picCapture

            if (!picCapture.CheckAccess())
            {
                picCapture.Dispatcher.Invoke(new Action(delegate()
                {
                    picCapture.Source = BitmapHelpers.ToBitmapSource(currentFrame);
                }));
            }
            else
            {
                picCapture.Source = BitmapHelpers.ToBitmapSource(currentFrame);
            }
        }