0

I was able to detect face from images but not able to detect face from videos by using Emgucv in C#. In my solution, the video is playing but not detecting faces.

My code is below:

namespace Emgucv33Apps
{
    public partial class FormFaceDetection : Form
    {
        VideoCapture capture;
        bool Pause = false;

      //  Image<Bgr, byte> imgInput;
        public FormFaceDetection()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                capture = new VideoCapture(ofd.FileName);
                Mat m = new Mat();
                capture.Read(m);
                pictureBox1.Image = m.Bitmap;
            }
        }

        private void DetectFaceHaar(Image<Bgr, byte> img)
        {
            try
            {
                string facePath = Path.GetFullPath(@"../../data/haarcascade_frontalface_default.xml");
                string eyePath = Path.GetFullPath(@"../../data/haarcascade_eye.xml");

                CascadeClassifier classifierFace = new CascadeClassifier(facePath);
                CascadeClassifier classifierEye = new CascadeClassifier(eyePath);

                   var imgGray = img.Convert<Gray, byte>().Clone();
                   Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, 1.1, 4);
                   foreach (var face in faces)
                   {
                    img.Draw(face, new Bgr(0, 0, 255), 2);

                       imgGray.ROI = face;

                    Rectangle[]eyes=   classifierEye.DetectMultiScale(imgGray, 1.1, 4);
                    foreach (var eye in eyes)
                       {
                           var e = eye;
                           e.X += face.X;
                           e.Y += face.Y;
                        img.Draw(e, new Bgr(0, 255, 0), 2);
                       }
                   }

                pictureBox1.Image = img.Bitmap;
                pictureBox2.Image = img.Bitmap;
            }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message);
               } 
        }

        private async void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (capture == null)
            {
                return;
            }

            try
            {
                while (true)
                {
                    Mat m = new Mat();
                    capture.Read(m);

                    if (!m.IsEmpty)
                    {
                        pictureBox1.Image = m.Bitmap;
                        DetectFaceHaar(m.ToImage<Bgr, byte>());
                        double fps = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);
                        await Task.Delay(1000 / Convert.ToInt32(fps));
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Thanks in advance!!

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
  • Please describe your **specific** and **isolated** issue. Have you tried to debug this? Are there any exceptions? Does your algorithm work as expected? – dymanoid May 04 '18 at 15:11

1 Answers1

1

First, You must create an event for this process and you need to get each frame of the video and check each frame for face detection. With the QueryFrame method in the VideoCapture class, you can manipulate each frame as an image and detect faces.

Example

    private VideoCapture m_videoCapture;

    public MainWindow()
    {
        InitializeComponent();
        try
        {
            m_videoCapture = new VideoCapture("controlcam.avi");
            Application.Idle += onProcessFrame;
        }
        catch (NullReferenceException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void onProcessFrame(Object sender, EventArgs e)
    {
        Image<Bgr, Byte> frameImage = m_videoCapture.QueryFrame().ToImage<Bgr, Byte>();

        // Call your function or write your code here.
        DetectFaceHaar(frameImage);
    }

    private void DetectFaceHaar(Image<Bgr, byte> img)
    {
        try
        {
            string facePath = Path.GetFullPath(@"../../data/haarcascade_frontalface_default.xml");
            string eyePath = Path.GetFullPath(@"../../data/haarcascade_eye.xml");

            CascadeClassifier classifierFace = new CascadeClassifier(facePath);
            CascadeClassifier classifierEye = new CascadeClassifier(eyePath);

            var imgGray = img.Convert<Gray, byte>().Clone();
            Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, 1.1, 4);
            foreach (var face in faces)
            {
                img.Draw(face, new Bgr(0, 0, 255), 2);

                imgGray.ROI = face;

                Rectangle[] eyes = classifierEye.DetectMultiScale(imgGray, 1.1, 4);
                foreach (var eye in eyes)
                {
                    var e = eye;
                    e.X += face.X;
                    e.Y += face.Y;
                    img.Draw(e, new Bgr(0, 255, 0), 2);
                }
            }

            pictureBox1.Image = img.Bitmap;
            pictureBox2.Image = img.Bitmap;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
alimsahy
  • 11
  • 2