Webcam Face Detection for .NET using EMGU.CV in C#

By FoxLearn 7/20/2024 2:24:10 AM   19.14K
Implementing face detection from a webcam or camera using EMGU.CV in a C# Windows Forms Application involves several steps.

EMGU.CV is a .NET wrapper for OpenCV, which provides powerful tools for computer vision tasks like face detection.

Here's a basic guide to get you started

Open your Visual Studio, then create a new Windows Forms Application project.

Opening your form designer, then drag and drop the Label, Combobox, PictureBox and Button controls from your Visual Studio Toolbox to your form designer.

c# face detection using webcam

You can install EMGU.CV via NuGet Package Manager in Visual Studio. Search for "Emgu.CV" and install the latest version.

Face detection and recognition using emgu, opencv and c#

Next, Search and Install 'AForge'. 'Emgu' to your project.

emgu aforge c#

After installation, add references to Emgu.CV, Emgu.CV.UI, Emgu.CV.Util in your C# project.

Declare two variables as shown below.

 // Initialize the capture device (default webcam)
FilterInfoCollection filter;
VideoCaptureDevice device;

Adding a Form_Load event handler that allows you to detect your webcam name, then add the device name to the Combobox control.

private void Form1_Load(object sender, EventArgs e)
{
    filter = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    foreach (FilterInfo device in filter)
        cboDevice.Items.Add(device.Name);
    cboDevice.SelectedIndex = 0;
    device = new VideoCaptureDevice();
}

You need to download haarcascade_frontalface_alt_tree.xml file from github website which contains the trained model for face detection, then create a cascadeClassifier variable.

This is a training data file helps you detect faces in image.

// Perform face detection using HaarCascade
static readonly CascadeClassifier cascadeClassifier = new CascadeClassifier("haarcascade_frontalface_alt_tree.xml");

Adding a click event handler to the Detect button allows you to capture image from the webcam.

private void btnDetect_Click(object sender, EventArgs e)
{
    device = new VideoCaptureDevice(filter[cboDevice.SelectedIndex].MonikerString);
    device.NewFrame += Device_NewFrame;
    device.Start();
}

You should implement NewFrame event handler allows you to detect face from webcam.

// Perform face detection here on 'frame'
private void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
    Image<Bgr, byte> grayImage = new Image<Bgr, byte>(bitmap);
    Rectangle[] rectangles = cascadeClassifier.DetectMultiScale(grayImage, 1.2, 1);
    foreach (Rectangle rectangle in rectangles)
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            using (Pen pen = new Pen(Color.Red, 1))
            {
                graphics.DrawRectangle(pen, rectangle);
            }
        }
    }
    // Display the frame in PictureBox
    pic.Image = bitmap;
}

You can also draw squares on faces that you detect.

And don't forget to close your webcam when closing your form.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Release the capture device
    if (device.IsRunning)
        device.Stop();
}

VIDEO TUTORIAL