Windows Forms: Face Detection for .NET using Emgu CV in C#

This post shows you How to use Emgu CV to detect faces in C# .NET Windows Forms Application.

OpenCV (Open Computer Vision) is an open source library used to solve computer vision problems. Thanks to a system of specialized algorithms, optimized for computer vision processing. This article will introduce the EmguCV image processing library and how to integrate EmguCV into .NET.

What is Emgu CV?

Emgu CV is a cross-platform image processing library that allows programmers to call OpenCV functions from .NET compatible languages ​​such as C#, VB, VC++, IronPython ...etc

Creating a new Windows Forms Application, then open your form designer.

Next, You need to drag PictureBox and Button controls from the visual studio toolbox to your winform.

Face Detection for .NET using EmguCV

Face detection emgucv C#

You need to install EMGU.CV from the Manage Nuget Packages to your project. This library help you create applications that support face detection and recognition using emgu, opencv and c#.

emgu cv nuget

After finishing installing EMGU.CV library, you need to download the haarcascade_frontalface_alt_tree.xml file from the face recognition c# github, then copy it into your project and don't forget to set the Copy to Output Directory property of your file to Copy always.

copy to output directory

Adding a click event handler to the Start button allows you to select an image file from your disk, then draw image detection into image in c# as shown below.

private void btnStart_Click(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg" })
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            pic.Image = Image.FromFile(openFileDialog.FileName);
            Bitmap img = new Bitmap(pic.Image);
            Image<Bgr, byte> grayImage = new Image<Bgr, byte>(img);
            Rectangle[] rectangles = Classifier.DetectMultiScale(grayImage, 1.4, 0);
            foreach (Rectangle rectangle in rectangles)
            {
                using (Graphics gr = Graphics.FromImage(img))
                {
                    using (Pen pen = new Pen(Color.Red, 1))
                    {
                        gr.DrawRectangle(pen, rectangle);
                    }
                }
            }
            pic.Image = img;
        }
    }
}

Through this c# example, i hope so you can understand how into install EmguCV to .NET project, then detect a face from any image file.

And don't forget to create a Classifier variable to read your training file.

static readonly CascadeClassifier Classifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");

You can use the DetectMultiScale method to detect faces, which will return recognized rectangular arrays.

The first parameter of the DetectMultiScale method is the grayscale image. The second parameter is the window scale factor. This parameter should be greater than 1.0 and the closer it is to 1.0, the longer it will take to detect faces, but you'll most likely find all faces and 1.4 is a good factor to get started.

Related