基于haar级联的人脸眼口检测

本文关键字:检测 haar 级联 基于 | 更新日期: 2023-09-27 17:52:43

我已经从脸部提取了眼睛和嘴巴,但是想从眼睛和嘴巴中提取情感。然而,口腔没有被正确检测到。这是我的代码。

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#进行情绪检测和更好的口腔检测。

基于haar级联的人脸眼口检测

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

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

我已经将面面积划分为顶部和底部两个矩形,并将底部矩形应用于灰色roi。而且很管用……这是两个矩形的代码。

 int halfheight = facesnap.Height/2;
                    int start = facesnap.X;
                    int start1 = facesnap.Y;
                    Rectangle top = new Rectangle(start,start1,facesnap.Width,halfheight);
                    int start2 = top.Bottom;
                    Rectangle bottom = new Rectangle(start, start2, facesnap.Width, halfheight);
                    nextFrame.Draw(bottom, new Bgr(Color.Yellow), 2);
                    //Set the region of interest on the faces
                    gray.ROI = bottom;
                    MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth,
                                                    1.1, 10,
                                                  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                                    new Size(20, 20));