Randoom a Michael Friis production

Posted
22 March 2009 @ 9pm

Categories
C#, Facedetection, Interactive art

Tagged
, ,

You're reading Randoom, a Michael Friis production

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 nextFrame = cap.QueryFrame())
		{
			if (nextFrame != null)
			{
				// there's only one channel (greyscale), hence the zero index
				//var faces = nextFrame.DetectHaarCascade(haar)[0];
				Image grayframe = nextFrame.Convert();
				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");
        }
    }
}

8 Comments

[...] UPDATE: Recommend EmguCV for C# wrapping OpenCV, read an updated guide. [...]


Posted by
Stefan
10 May 2009 @ 6pm

Thank you for that code.

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


Posted by
Steven
13 May 2009 @ 8am

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)


Posted by
dee
26 June 2009 @ 3pm

Have you tried running emgu on a shared hosting server that has trust=Medium forced? it doesn’t seem to like it at all.


Posted by
friism
26 June 2009 @ 3pm

@dee Yeah — that would make sense since EmguCV runs on top of OpenCV which is un-managed C++ code.


Posted by
bijan
8 July 2009 @ 2pm

hello sir
very good your document
tanks


Posted by
bijan
8 July 2009 @ 2pm

i’m iranian


Posted by
Alex Park
29 July 2009 @ 11am

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’.


Leave a Comment