Face Detection for .NET using Emgu CV in C#

By FoxLearn 7/19/2024 2:46:58 AM   21.9K
To detect faces using Emgu CV (OpenCV wrapper for C#) in a Windows Forms Application, you can follow these steps.

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 and drop the PictureBox and Button controls from the Visual Studio toolbox to your winform.

face detection for .net using emgucv

Face detection emgucv C#

If you haven't already, you need to install Emgu CV NuGet package into your project.

You can do this through NuGet Package Manager in Visual Studio.

Right-click on your project in Solution Explorer, then Select "Manage NuGet Packages...". Next, Search for "EmguCV" and install the latest stable version.

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);
            // Detect faces
            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))
                    {
                        // Draw rectangles around the faces
                        gr.DrawRectangle(pen, rectangle);
                    }
                }
            }
            // Display the image with detected faces
            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.

// Create a cascade classifier (this is used for detecting faces)
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.

For face detection, you need a Haar Cascade XML file which describes how a face looks. Emgu CV does not include these files by default, but you can find them in the OpenCV repository or create your own using OpenCV tools. You need to download a pre-trained XML file (e.g., haarcascade_frontalface_default.xml) and specify its path in your code.

Through this example, you should be able to integrate face detection capabilities using Emgu CV into your C# Windows Forms Application successfully.

Related