Facedectection in C# with less than 50 LoC

Posted on |

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 UPDATE: Dud link, read the updated guide
  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 🙂

Comments

Esmail on

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

Reply

friism on

@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

Reply

sunder raj on

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.

Reply

Abhilash on

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.

Reply

juanid on

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

Reply

atzu on

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.

Reply

1ks on

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.

Reply

Menda on

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

Reply

MinhTai on

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

Thanks very much

Reply

Andrew on

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

Reply

friism on

@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

Reply

sutasom on

can u send me CVHaar.h please?

Reply

consuelo on

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

Reply

Jamal Sami on

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

Reply

Menda on

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 ?

Reply

friism on

@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

Reply

chamy on

Please send me the CVHaar.h
Thank you. 🙂

Reply

rahul on

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

Reply

J on

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…

Reply

Stam on

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

Reply

orpheus on

Please send me the CVHaar.h
Thank you. 🙂

Reply

admin on

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

Reply

delta on

Please send me the CVHaar.h
Thank you.

Reply

arvind on

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

Reply

eorra on

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

Reply

koraim on

Please send me the CVHaar.h
Thank you.

Reply

Alexis Banaag on

Hello,
I’ve been trying to work on a face detection program in c#. But the things is it doesn’t involve webcams. I want to create that a user uploads an image then the user clicks a button to detect the face in the uploaded image. Can you help me with this?

Thanks..:)

Reply

eliecer on

Me gusta trabajar con imagenes pero aun no me funciona el ejemplo porque me hace falta la libreria CVHaar.h quien me la podria enviar a [email protected] de ante mano gracias

Reply

Yonatan H. on

Nice job, pls don’t stop working… u v good eyez in pgming…
BTW,I’m BSE grad of CE

Reply

bulut on

ı can’t install opencvdotnet to opencv2.2 why

Reply

niken on

Could you please send me the cvHaar.h header file to my e-mail? I will be very very thankful for you.
because I can’t find the link to download.
please. I need this for my final project in college.

Reply

dave on

looks like a 50 loc with a call to a method with 1000s loc 😀
don’t fool people mate 😉

Reply

htc-hd2-android on

Although there is a OpenCVDotNet to wrapper OpenCV with C# language, it seems very difficult for common users to implement Simple Face Detection/Face Recognition as a simple way.Now you don’t need to worry about it, because there is a developer has built a pure C# Face Detection Library.http://htc-hd2-android.blogspot.com/2011/03/c-face-detectionface-recognition.html

Reply

Tanvir Ahmed on

It’s a nice tutorial.
But I need some help or link so that I can detect teeth from face efficiently using c#.

Your help will be highly appreciated

Reply

Kassem Bagher on

hello,
what about Face recognition?
can you please suggest a good library for .NET

Greets,
Kassem

Reply

Cyber noob on

Hi,

You sample look awesome. I have problems in creating image processing apps with open cv for my project and seems like i can use this emgu cv as the alternative. Can you give some samples to move mouse pointer with hand gestures using emgu cv? thanks.

Reply

Razie on

Hi,I need face detection but I cant get it Could you please send me the cvHaar.h header file and face detection code to my e-mail? Thanks for your help…:-)

Reply

Leave a Reply to Menda Cancel reply

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