使用haar级联从脸部进行眼和嘴检测 [英] Eye and Mouth detection from face using haar-cascades

查看:110
本文介绍了使用haar级联从脸部进行眼和嘴检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从脸上提取了眼睛和嘴巴,但是想从眼睛和嘴巴中提取情感。.但是,不能正确检测到嘴巴。.
这是我的代码。.

I have extracted eyes and mouth from the face, but want to extract emotions from eyes and mouth.. However, mouth is not detected properly.. This is my code..

private void timer1_Tick(object sender, EventArgs e)
{
    using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
    {
        if (nextFrame != null)
        {
            // there's only one channel (greyscale), hence the zero index
            //var faces = nextFrame.DetectHaarCascade(haar)[0];
            Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
            Image<Gray, Byte> gray = nextFrame.Convert<Gray, Byte>();
            Image<Gray, Byte> gray1 = nextFrame.Convert<Gray, Byte>();

            var faces = grayframe.DetectHaarCascade(
                            haar, 1.4, 4,
                            HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                            new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                            )[0];

            MCvAvgComp[][] eyes = gray.DetectHaarCascade(eye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            gray.ROI = Rectangle.Empty;

            MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth, 1.1, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            gray1.ROI = Rectangle.Empty;

            foreach (MCvAvgComp mouthsnap in mouthsDetected[0])
            {
                Rectangle mouthRect = mouthsnap.rect;
                // mouthRect.Offset(f.rect.X, f.rect.Y);
                nextFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                detectedmouth = mouthRect;

            }
            foreach (MCvAvgComp eyesnap in eyes[0])
            {
                Rectangle eyeRect = eyesnap.rect;
                // mouthRect.Offset(f.rect.X, f.rect.Y);
                nextFrame.Draw(eyeRect, new Bgr(Color.Green), 2);
            }
            foreach (var face in faces)
            {
                nextFrame.Draw(face.rect, new Bgr(Color.LightGreen), 3);
                facesnap = face.rect;
            }

            pictureBox1.Image = nextFrame.ToBitmap();
        }
    }

}

private void Form1_Load(object sender, EventArgs e)
{
    cap = new Capture(0);
    // adjust path to find your xml
    //haar = new HaarCascade("haarcascade_frontalface_alt2.xml");
    haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
    mouth = new HaarCascade("Mouth.xml");

    eye = new HaarCascade("haarcascade_eye_tree_eyeglasses.xml");
}

private void button1_Click(object sender, EventArgs e)
{
    Image snap = pictureBox1.Image;

    snap.Save("c:\\snapshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    pictureBox2.Image = snap;
    pictureBox3.Image = cropImage(snap,facesnap);
    pictureBox4.Image = cropImage(snap, detectedmouth);

}

private static Image cropImage(Image img, Rectangle croparea)
{
    Bitmap bmpImage = new Bitmap(img);
    Bitmap bmpCrop = bmpImage.Clone(croparea, bmpImage.PixelFormat);
    return (Image)(bmpCrop);
}

请帮助我进行c#情绪检测和更好的嘴部检测。

Please help me in emotion detection and better mouth detection using c#.

推荐答案

我会尝试在矩形矩形中寻找嘴巴,而不是检查孔的图片。

I would try to look for a mouth in a face rectangle, instead of checking the hole picture.

var faces = grayframe.DetectHaarCascade(
                            haar, 1.4, 4,
                            HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                            new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                            )[0];
 foreach (var f in faces)
 {
    //draw the face detected in the 0th (gray) channel with blue color
    image.Draw(f.rect, new Bgr(Color.Blue), 2);


     //Set the region of interest on the faces
     gray.ROI = f.rect;
     var mouthsDetected = gray.DetectHaarCascade(mouth, 
                              1.1, 10, 
                              Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, 
                              new Size(20, 20));
     gray.ROI = Rectangle.Empty;


     foreach (var m in mouthsDetected [0])
     {
          Rectangle mouthRect = m.rect;
          mouthRect.Offset(f.rect.X, f.rect.Y);
          image.Draw(mouthRect , new Bgr(Color.Red), 2);
     }
   }

这篇关于使用haar级联从脸部进行眼和嘴检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆