繁体   English   中英

Emgu CV微笑检测

[英]Emgu CV smile detection

我正在做一个在你微笑时拍张照片的项目,但是我本身并不是在做微笑检测。

(我的项目正在微笑时拍照)

如何进行笑容检测以便可以拍照?

这是我的项目源代码:

public partial class Form1 : Form
{
    private Capture capture;        
    private bool captureInProgress;
    private HaarCascade haar;
    private HaarCascade mouth;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("haarcascade_mcs_mouth.xml");

    }
    private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> ImageFrame = capture.QueryFrame();

        if (ImageFrame !=null)
        {
            Image<Gray, byte> grayFrame = ImageFrame.Convert<Gray, byte>();
            Image<Gray, Byte> gray = ImageFrame.Convert<Gray, Byte>();
            var faces = grayFrame.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0];
            foreach (var face in faces)
            {
                ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);

                MCvAvgComp[][] 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 (MCvAvgComp e in mouthsDetected[0])
                {
                    Rectangle mouthRect = e.rect;
                    mouthRect.Offset(face.rect.X, face.rect.Y);
                    ImageFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                }
            }

        }

        CamImageBox.Image = ImageFrame;       

    }

    private void btnstart_Click(object sender, EventArgs e)
    {
        #region if capture is not created, create it now
        if (capture == null)
        {
            try
            {
                capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        #endregion

        if (capture != null)
        {
            if (captureInProgress)
            {  //if camera is getting frames then stop the capture and set button Text
                // "Start" for resuming capture
                btnStart.Text = "Başlat"; //
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //if camera is NOT getting frames then start the capture and set button
                // Text to "Stop" for pausing capture
                btnStart.Text = "Durdur";
                Application.Idle += ProcessFrame;
            }

            captureInProgress = !captureInProgress;
        }
    }
    private void ReleaseData()
    {
        if (capture != null)
            capture.Dispose();
    }  
  }
}

如果您要查看有关图像,请从您的处理中,

Emgu CV建议使用ImageBox用于显示目的控制,由于以下原因。 可以将其与上述代码示例中的ImageFrame(Image)对象结合使用。

ImageBox是用于显示图像的高性能控件。 只要有可能,它就会显示一个与Image对象共享内存的位图,因此不需要内存副本(非常快)。

当显示图像时,用户将能够检查图像像素值,视频帧速率,颜色类型。

只需单击几下鼠标,即可执行简单的图像操作。

转换为位图

Image类具有ToBitmap()函数,该函数返回一个Bitmap对象,该对象可以使用Windows Form轻松显示在PictureBox控件上。

检测嘴/微笑

您将需要提供一个Haarcascade XML,并进行相应捕获。 请参阅以下代码 ,该代码可以在嘴上绘制一个矩形,

CascadeClassifier mouth = new CascadeClassifier(Application.StartupPath + "/haarcascade_mcs_mouth.xml");
Image<Bgr, Byte> currentframe= null;
Image<Gray, byte> grayFrame = null;
Capture grabber = new Capture();

currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

            if (currentframe != null)
            {
                grayFrame = currentframe.Convert<Gray, Byte>();

                Rectangle[] mouthDetected = mouth.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty, Size.Empty);

                // to draw rectangle 
                foreach (Rectangle mouthFound in mouthDetected)
                {
                   ...
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM