Randoom a Michael Friis production

Posted
3 May 2008 @ 1pm

Categories
C#, Facedetection, Interactive art

Tagged
,

You're reading Randoom, a Michael Friis production

Facedectection in C# with less than 50 LoC

I’ve been mucking around with face detection a bit lately. Here’s a short guide to getting face detection running in C# in a short amount of time.

UPDATE: Recommend EmguCV for C# wrapping OpenCV, read the updated guide.

I’ve identified two free computer vision libraries that do face detection:  Torch3vision and OpenCV(I’m sure there are plenty more, but these seem to be comprehensive, recently updated and freely available). Torch3vision claims to be better than OpenCV but on the other hand more people are building libraries and wrappers around OpenCV and there’s even a wrapper for .Net. While it doesn’t yet wrap the face detection components of OpenCV, it seems to be the most promising solution.

To get face detection in OpenCV to work with C#, do the following:

  1. Install OpenCV and OpenCVDotNet as per the instructions
  2. Get CVHaar.h from this discussion and place it in C:\Program Files\OpenCVDotNet\src\OpenCVDotNet
  3. Open OpenCVDotNet.sln, add CVHaar.h to the solution and include it in OpenCVDotNet.cpp
  4. Rebuild the solution
  5. Create a Windows forms application as described in the tutorial, but do something like the code 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 OpenCVDotNet;

namespace opencvtut
{
    public partial class Form1 : Form
    {
        private CVCapture cap;
        private CVHaar haar;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (CVImage nextFrame = cap.QueryFrame())
            {
                Rectangle[] faces = haar.detectPatterns(1.3, nextFrame, 20, 20, 1.1, 2, 1);
                for (int i = 0; i < faces.Length; i++)
                {
                    nextFrame.DrawRectangle(faces[i], Color.Yellow, 3);
                }
                pictureBox1.Image = nextFrame.ToBitmap();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // passing 0 gets zeroth webcam
            cap = new CVCapture(0);
            haar = new CVHaar(
                "C:Program FilesOpenCVdatahaarcascadeshaarcascade_frontalface_alt2.xml");
        }
    }
}

Happy detecting :-)

Faces


27 Comments

Posted by
Esmail
12 August 2008 @ 3pm

Hi,

I am PhD student at Newcastle University , recently I am working on face detection in digital images. I found your site very intersting for me. Could you please send to me your complete latest version.

Cheers.

Esmail


Posted by
friism
12 August 2008 @ 6pm

@Esmail: I recommend using EmguCV instead of OpenCV.net. Instructions and tutorial here:
http://www.emgu.com/wiki/index.php/Main_Page

My latest work with OpenCV is here, there’s even a bit of code at the end of the post:
http://www.itu.dk/~friism/blog/?p=70


Posted by
sunder raj
11 November 2008 @ 2pm

hi.
I am M.Tech student at J.N.T University ,Hyderabad,India. and I am working on project “image and face detection on handheld devices”. I found your site very intersting for me.I want to know about “OpenCV and OpenCVDotNet”. Could you please help me with this.


Posted by
Abhilash
20 January 2009 @ 12pm

hai i am a PG student from India. I really liked your work. I tried it and its working.I would like to know whether some methods available for tracking lip and eye movements. If you could advice me on that it would be a great help.


[...] prototype of Martin Speelmans, informed by my work with Flash and web cams and my experiments with OpenCV. The basic premise is that a flex application running in a users browser grabs web cam shots, [...]


Posted by
juanid
26 February 2009 @ 3pm

would you please send me the CVHaar.h file .Please send me i am very thankfull to you


Posted by
| Randoom
22 March 2009 @ 9pm

[...] 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 [...]


Posted by
atzu
22 April 2009 @ 1pm

Hi, I’m trying to traduce my face detection project to C# but I’m having some problems. Could you please send me the cvHaar.h header file to my e-mail? I’ve tried to download it from the discussion site but it’s been erased due to terms violation. Thank you very much and congrats for this great work.


Posted by
1ks
18 June 2009 @ 8pm

Hi Michael, my name is Alberto, i’m studying at IT Tepic, México, I’m trying download cvHaar.h but the link is offline. Could you send me the cvHaar.h header file to my e-mail? Thank you very much.


Posted by
Menda
3 July 2009 @ 7am

Please email me the cvHaar.h as well please… Thanks very much


Posted by
MinhTai
4 July 2009 @ 3am

Very good!
But I can’t find CVHaar.h
Please email me the CVHaar.h

Thanks very much


Posted by
Andrew
24 July 2009 @ 3pm

Hey… this looks really interesting… could you please mail me the CVHaar.h file to my e-mail…


Posted by
friism
24 July 2009 @ 6pm

@Menda, @MinhTai and @Andrew I don’t think I have CVHaar.h anymore, recommend using EmguCV instead: http://friism.com/webcam-face-detection-in-c-using-emgu-cv


Posted by
sutasom
13 August 2009 @ 1pm

can u send me CVHaar.h please?


Posted by
consuelo
4 October 2009 @ 3pm

Hi, can you send me CVHaar.h please?
Thank you!
Cheers,
Consuelo


Posted by
Jamal Sami
14 October 2009 @ 1pm

can u please send the CVHaar.h to me.
thanks in advance


Posted by
Menda
2 November 2009 @ 12am

Hey guys why can’t anyone give us even a link to download the CVHaar.h cz this code is so good and interesting but without it we can’t do anything doen’t the author has the code ? and does that mean he’s not the author or what ?


Posted by
friism
2 November 2009 @ 9am

@Menda bugger off. Why don’t you just use EmguCV like I recommend at the top of the post? I wrote a similar guide which is found here: http://friism.com/webcam-face-detection-in-c-using-emgu-cv


Posted by
chamy
20 November 2009 @ 9am

Please send me the CVHaar.h
Thank you. :)


Posted by
rahul
5 January 2010 @ 12pm

i am doing a project on face dectection ,recognition as a part of home security system.i want code in c# .pls help ..i want working code pleaseeeee


Posted by
J
8 January 2010 @ 5am

What the heck is with the posters on this site? They all are asking ridiculous questions. Guys, work it out yourselves!

It almost seems like some scam is going on…


Posted by
Stam
9 January 2010 @ 8pm

Hi
Very interesting article
Can you send me the file CVHaar.h cause the discussion page is removed from google groups
Thank you


Posted by
orpheus
11 January 2010 @ 3pm

Please send me the CVHaar.h
Thank you. :)


Posted by
admin
11 January 2010 @ 3pm

@orpheus, @Stam I don’t have CVHaar.h anymore — sorry. I recommend you use EmguCV instead: http://www.emgu.com


Posted by
delta
19 January 2010 @ 8pm

Please send me the CVHaar.h
Thank you.


Posted by
arvind
19 February 2010 @ 2pm

Hi, I’m trying to do my face detection project to C# but I’m having some problems.Could you please send me the cvHaar.h header file to my e-mail?i will be very very thankful for you


Posted by
eorra
23 February 2010 @ 8pm

Hi, im tying to do face recognition for my final project from cam, i found this site to solve my face detection. would you send me cvHaar.h to my e-mail pliz. thanks


Leave a Comment