C# Tutorial – Webcam Face Detection for .NET using EMGU.CV in C# | FoxLearn

Welcome to FoxLearn In this tutorial, I’ll show you how to detect faces from webcam using the Emgu.CV library You should create a new Windows Forms Application project to practice this demo After you finish creating the Windows Forms Application project you need to open your form designer, then drag the Label, Combobox PictureBox and Button controls from the Visual Studio Toolbox to your form designer You can create a simple user interface that allows you to detect faces from your webcam using the Emgu.CV library Right click on your project, then select Nuget Manage Packages Next, You need to search and install the AForge library to your project AForge.NET is an open source c# framework designed for developers and researchers in the fields of Computer Vision, Artificial Intelligence image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc The AForge.Video.DirectShow library contains classes, which allow to access video sources using DirectShow interface. Such as, USB web cameras, capture devices, video files, etc You need to detect the webcam from your computer, then add the webcam name to the Combobox control You need to add a new frame event handler that allows you to detect faces captured from your webcam then we will update the image to the PictureBox control You should close the webcam when closing your application You need to install the Emgu.CV library from the Manage Nuget Packages to your project Emgu.CV is a cross platform .Net wrapper to the OpenCV image processing library Allowing OpenCV functions to be called from .NET compatible languages such as c-sharp, VB, C++, IronPython etc We will load the training data into the CascadeClassifier class The CascadeClassifier class helps you detect objects in the video stream You should download training data from the Github website, then copy training data into your project You should copy the image object captured from the webcam into the Bitmap class After detect face is finished, we will get an array of rectangles Next, You need to draw the rectangle on the Bitmap, and finally display the Bitmap into the PictureBox control Thank you for watching this video and don’t forget to subscribe to my channel

, , , , , , , , , , , , , , , , , , , ,

Post navigation

33 thoughts on “C# Tutorial – Webcam Face Detection for .NET using EMGU.CV in C# | FoxLearn

  1. We need a project that compares the face, for example: – You have a group of photos of certain people, the number of thousands, and you take a picture of a modern person, the program compares the new image with the old photos and extracts the old image of his
    I think it's termed "image processing"

  2. Wow! This is a wonderful tutorial… Thank you FoxLearn!

    By the way, is it possible to compare the face from the image saved in the database using that method? If a face was similar on one of the image saved in database, then it will say something! I hope you can make a tutorial for that.😁😊

  3. Thanks for this tutorial yet. so i am waiting the system login and authorization that you have implement on JRE rental software with Devexpress.

  4. Good job with the tutorials, there is really cool videos on your channel.

    Would be possible to upload more WPF videos please?

  5. Thanks for the tutorial. But pls I have a problem, the camera captures images well but it's not detecting the face. I had issues downloading the engine.cv, the one with over 200k download, so I downloaded the one with over 100k download. Could that be the cause?

  6. Follow code instruction, it can show webcam but it could not show rectangle on the face. Debug code in frame event, it always returns zero rectangle, so it could not draw red rectangle on the face. What happen to me?

  7. So, i'm having a problem on line 29 where it says "
    Error CS1061 'VideoCaptureDevice' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'VideoCaptureDevice' could be found (are you missing a using directive or an assembly reference?)"
    but I don't know how to fix it

  8. Thanks is works, but is not sensitive as expected and have mirror effect~

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows.Forms;

    using AForge.Video;

    using AForge.Video.DirectShow;

    using Emgu.CV;

    using Emgu.CV.Structure;

    namespace FaceDetectionCamera


    public partial class Form1 : Form


    public Form1()




    FilterInfoCollection filter;

    VideoCaptureDevice device;

    private void Form1_Load(object sender, EventArgs e)


    filter = new FilterInfoCollection(FilterCategory.VideoInputDevice);

    foreach (FilterInfo device in filter)


    cboDevice.SelectedIndex = 0;

    device = new VideoCaptureDevice();


    private void btnDetect_Click(object sender, EventArgs e)


    device = new VideoCaptureDevice(filter[cboDevice.SelectedIndex].MonikerString);

    device.NewFrame += Device_NewFrame;



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

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




    pic.Image = bitmap;


    private void Form1_FormClosing(object sender, FormClosingEventArgs e)


    if (device.IsRunning)





  9. I've copied the code exactly to see if my errors were spacings or anything little I could miss but I still get the
    Error: CS1503 Argument 1: cannot convert from 'System.Drawing.Bitmap' to 'byte[*,*,*]'

  10. My VideoCaptureDevice in Page Load event is throwing "Object Reference not set" error. How to add the input device in dropdown? (BDW I am creating this application in Laptop)

  11. Image<Bgr, byte> grayImage = new Image<Bgr, byte>(bitmap);

    I have an error on the last "bitmap". It says
    Error CS1503 Argument 1: cannot convert from 'System.Drawing.Bitmap' to 'byte[*,*,*]'
    I need Help

Leave a Reply

Your email address will not be published. Required fields are marked *