Webcam face detection in C# using Emgu CV

Some time ago I wrote a post on how to do face detection in C# using OpenCV. I’ve since begun using the Emgu CV wrapper instead of opencvdotnet. Emgu CV is much better, in active development and it even runs on Mono. Two gotchas:

  1. You don’t have to install OpenCV, but instead have to copy the relevant dlls (included with the Emgu CV download) to the folder where you code executes.
  2. Open CV and X64 are not friends. If you’re running X64 Windows (and unless you are up to recompiling OpenCV) you have to make sure your app is compiled to X86, instead of the usual “Any CPU”.
  3. Remember to add PictureBox as per the original tutorial.

Here’s sample code:

using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace opencvtut
{
    public partial class Form1 : Form
    {
		private Capture cap;
		private HaarCascade haar;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
		using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
		{
			if (nextFrame != null)
			{
				// there's only one channel (greyscale), hence the zero index
				//var faces = nextFrame.DetectHaarCascade(haar)[0];
				Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
				var faces =
					grayframe.DetectHaarCascade(
						haar, 1.4, 4,
						HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
						new Size(nextFrame.Width/8, nextFrame.Height/8)
						)[0];

				foreach (var face in faces)
				{
					nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
				}
				pictureBox1.Image = nextFrame.ToBitmap();
			}
		}
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // passing 0 gets zeroth webcam
			cap = new Capture(0);
            // adjust path to find your xml
			haar = new HaarCascade(
                "..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
        }
    }
}

40 thoughts on “Webcam face detection in C# using Emgu CV”

  • Thank you for that code.

    Does anyone have some experience about training the HaarClassifier to create xml files for other objects…

  • Thanks, Sir. A question I want to consult you, how to access my webcam? When I run the code “cap = new Capture(0);” with my wired camera, there is no problem. However, I don’t know how to access my webcam. (I know the address of my webcam)

  • I used this code to get Facedetection working in C#

    however I have this problem when i complie.

    It says that ‘var’ could not b found

    and ‘Emgu.CV.HarrCascade’ does not contain a definition for ‘rect’

    and ‘face’ cannot be declared in this scope becaue it would give a different meaning to ‘face, which is already used in a ‘parent or current’.

  • I have this problem when i complie:
    using (Image nextFrame = cap.QueryFrame())

    Cannot convert type ‘Emgu.CV.Image’ to ‘System.Drawing.Image’

    Thanks for help. Lada

  • Thanks for posting your code. This is really helpful. For those getting the compile error, you’ll need to specify params to the Image object to ensure that it’s using Engu.CV.Image and not System.Drawing.Image:

    using (Image nextFrame = cap.QueryFrame())
    and
    Image grayframe = nextFrame.Convert();

  • Ah, I see that the blog software is stripping out the greater-than and less-than so my code snippets got edited. Let me try again:

    using (Image<Bgr, Byte> nextFrame = cap.QueryFrame())

    and

    Image<Gray, Byte> grayframe = nextFrame.Convert<Gray, Byte>();

  • Hi. When i run this code i get an exception at the line

    Image grayframe = nextFrame.Convert();

    and it says that this “conversion is not supported by OpenCV”
    Now can anyone tel me how to solve it?

  • hiii
    sir
    can u suggest me how o train the databse on your own..
    bcoz for side view the results are worst..
    or if u could suggest me a way to detect side face of a human in the video with arnd 80 % accuracy..

  • Thank you very very much for this entry. It really was super fast to get all the things working. Tested with VS2010, after hooking up the events, and enabling timer — just worked. You are the man, Michael!

  • Arpita Nagpal says:

    I used this code to get Facedetection and is working in C#.I used the web camera for this.
    but what if i want to detect faces from a vedio?
    at this line “capture = new Capture(0)”,instead of(0),i gave the path of the vedio file like”capture = new Capture(“path.avi”) but still its not working.

    what should i do if i want to get faces from vedio instead of camera capture.

  • it’s really 100% work’s…thank’s
    just enable timer and change

    using (Image nextFrame = cap.QueryFrame())

    and

    Image grayframe = nextFrame.Convert();

  • Great code.
    How could I compare the face detected by the webcam with somekind of image database, to identify that ME is ME ? Got it?
    Thanks.

  • Emgu CV is just like a bomb. It actually works. U can use this code,

    Image image = new Image(“DSC02624.jpg”); //Read the files as an 8-bit Bgr image
    Image gray = image.Convert(); //Convert it to Grayscale

    Stopwatch watch = Stopwatch.StartNew();
    //normalizes brightness and increases contrast of the image
    gray._EqualizeHist();

    //Read the HaarCascade objects
    HaarCascade face = new HaarCascade(“haarcascade_frontalface_alt_tree.xml”);
    HaarCascade eye = new HaarCascade(“haarcascade_eye.xml”);

    //Detect the faces from the gray scale image and store the locations as rectangle
    //The first dimensional is the channel
    //The second dimension is the index of the rectangle in the specific channel
    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
    face,
    1.1,
    10,
    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
    new Size(20, 20));

    foreach (MCvAvgComp f in facesDetected[0])
    {
    //draw the face detected in the 0th (gray) channel with blue color
    image.Draw(f.rect, new Bgr(Color.Blue), 2);

    //Set the region of interest on the faces
    gray.ROI = f.rect;
    MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
    eye,
    1.1,
    10,
    Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
    new Size(20, 20));
    gray.ROI = Rectangle.Empty;

    foreach (MCvAvgComp e in eyesDetected[0])
    {
    Rectangle eyeRect = e.rect;
    eyeRect.Offset(f.rect.X, f.rect.Y);
    image.Draw(eyeRect, new Bgr(Color.Red), 2);
    }
    }

    watch.Stop();
    //display the image
    ImageViewer.Show(image, String.Format(“Perform face and eye detection in {0} milliseconds”, watch.ElapsedMilliseconds));

  • I am having a problem with Capture();

    I downloaded the Emgu sdk and ran (out of the box) some examples of webcam use, much like yours.. it worked fine.

    I then did a rebuild all, and suddenly, I get this runtime error

    The type initializer for ‘Emgu.CV.CvInvoke’ threw an exception.
    “Unable to load DLL ‘cxcore210’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)”}

    THis happens on both VS2005 and VS2010

    Any suggestions on how to fix this??

  • Johnykutty Mathew says:

    I experienced the same problem that Chris got
    Anybody can give a solution??????????????
    I am using emgucv 2.2 and vs2008

  • I used this code to get Facedetection working in C#

    however I have this problem when i complie.

    It says that ‘var’ could not b found

    and ‘Emgu.CV.HarrCascade’ does not contain a definition for ‘rect’

    and ‘face’ cannot be declared in this scope becaue it would give a different meaning to ‘face, which is already used in a ‘parent or current’.

  • Hello,

    I’m getting a NullReferenceException at:

    using (Image nextFrame = cap.QueryFrame())

    (I think that’s where it is where the problem occurs anyway)

    Does anyone have any suggestions?

    Thanks.
    C.

  • Hi,
    I got this sample working with GTK# and it works pretty well. It does however use a lot of CPU. Any tricks on how to lower this without making the framerate too low?

  • @Chris: Just copy all the OpenCV dlls (including cxcore210) into your project folder. Also you must ensure that you are using EmguCV version <=2.1 since you are using OpenCV V2.1

  • The code compiles fine but while running it throws an exception in the line :

    using (Image nextFrame = cap.QueryFrame())

    The Exception is : System.NullReferenceException

    FYI…I am using laptop’s built in webcam.

    Can someone please help on this.

  • Error 1 The OutputPath property is not set for project ‘Emgu.CV.GPU.csproj’. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration=’Debug’ Platform=’MCD’. This error may also appear if some other project is trying to follow a project-to-project reference to this project, this project has been unloaded or is not included in the solution, and the referencing project does not build using the same or an equivalent Configuration or Platform. FaceDetection

    Hi m trying to open facedetection example of emgu with my VS2010…but m getting the above error can anyone help please…..

  • hi, i am doing my final year BE project.In my project I wiil allow some fluid to flow through the catridge(box like material ). when the fluid reaches the end of catridge , i should detect th change.My program is to capture the images of the catridge from camera & crop the required part & then to compare the cropped image.I need to find the whether there is a change in the cropped part. I am using visual c# (windows form application) with opencvdotnet.Can any one help me.if u can then send me a mail-sandhyakushi24@gmail.com

    My code is as below
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using OpenCVDotNet;
    using System.IO.Ports;
    using System.Threading;
    namespace program
    {
    public partial class Form1 : Form
    {
    private CVCapture capture;
    private CVImage backimage;
    private bool first = false;

    string pastimage, image;
    int count = 0, count1 = 0;

    public Form1()
    {
    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
    timer1.Enabled = !(timer1.Enabled);
    if (timer1.Enabled)
    {
    capture = new CVCapture(0);
    }
    else
    {
    capture.Release();
    }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    using (CVImage frame = capture.QueryFrame())
    {
    if (first == false)
    {
    backimage = frame;
    first = true;
    }

    frame.Resize(320,240);
    Bitmap new_frame = frame.ToBitmap();
    Bitmap new_frame1;
    Rectangle croprect3 = new Rectangle(30, 20, 47, 240);
    new_frame.Save(@”C:\Documents and Settings\Administrator\Desktop\new\c.jpg”);
    Bitmap target2 = new Bitmap(croprect3.Width, croprect3.Height);
    using (Graphics g = Graphics.FromImage(target2))
    {
    g.DrawImage(new_frame, new Rectangle(0, 0, target2.Width, target2.Height), croprect3, GraphicsUnit.Pixel);
    target2.Save(@”C:\Documents and Settings\Administrator\Desktop\new\c.jpg”, ImageFormat.Jpeg);
    new_frame1 = target2;

    }

    pictureBox1.Image = frame.ToBitmap();
    Bitmap new_backimage = backimage.ToBitmap();
    Bitmap new_backimage1;

    Rectangle croprect5 = new Rectangle(30, 20, 47, 240);
    new_backimage.Save(@”C:\Documents and Settings\Administrator\Desktop\new \e.jpg”);
    Bitmap target4 = new Bitmap(croprect5.Width, croprect5.Height);
    using (Graphics g = Graphics.FromImage(target4))
    {
    g.DrawImage(new_backimage, new Rectangle(0, 0, target4.Width, target4.Height), croprect5, GraphicsUnit.Pixel);
    target4.Save(@”C:\Documents and Settings\Administrator\Desktop\new \e.jpg”, ImageFormat.Jpeg);
    new_backimage1 = target4;

    }

    if (new_frame1.Width == new_backimage1.Width && new_frame1.Height == new_backimage1.Height)
    {

    for (int r = 0; r < new_frame1.Width; r++)
    {
    for (int c = 0; c 15)
    {

    MessageBox.Show(“change found”);

    }
    else
    {

    MessageBox.Show(“change not found”);
    }
    //count1 = 0;
    //count = 0;

    }
    }

    }

    else
    {
    MessageBox.Show(“Height and width don match”);
    }

    }
    }
    }
    }

  • Hey. I’m getting the [Cannot implicitly convert type ‘Emgu.CV.Image’ to ‘System.Drawing.Image’] error showing on this line in my code:

    InputImage.Image = TestImage;

    And it appears in the same line at 3 different places. Any help would be aprreciated

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>