无法在emgucv中检测来自视频的脸部

时间:2018-05-04 14:28:35

标签: c# video-processing emgucv face-detection

我能够通过在C#中使用Emgucv从图像中检测到脸部但无法从视频中检测到脸部。在我的解决方案中,视频正在播放但未检测到面部。

我的代码如下:

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);
            }
        }
    }
}

提前致谢!!

1 个答案:

答案 0 :(得分:0)

首先,您必须为此过程创建一个事件,并且需要获取视频的每个帧并检查每个帧以进行面部检测。使用VideoCapture类中的QueryFrame方法,您可以将每个帧作为图像进行操作并检测人脸。

示例

    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);
        }
    }