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:
- Install OpenCV and OpenCVDotNet as per the instructions
- Get CVHaar.h from this discussion and place it in
C:\Program Files\OpenCVDotNet\src\OpenCVDotNet
UPDATE: Dud link, read the updated guide - Open
OpenCVDotNet.sln
, addCVHaar.h
to the solution and include it inOpenCVDotNet.cpp
- Rebuild the solution
- 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 🙂
39 thoughts on “Facedectection in C# with less than 50 LoC”-
-
-
-
-
Pingback: Randoom – Server based real-time face detection in Flex
-
-
Pingback: | Randoom
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Esmail says:
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
friism says:
@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
sunder raj says:
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.
Abhilash says:
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.
juanid says:
would you please send me the CVHaar.h file .Please send me i am very thankfull to you
atzu says:
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.
1ks says:
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.
Menda says:
Please email me the cvHaar.h as well please… Thanks very much
MinhTai says:
Very good!
But I can’t find CVHaar.h
Please email me the CVHaar.h
Thanks very much
Andrew says:
Hey… this looks really interesting… could you please mail me the CVHaar.h file to my e-mail…
friism says:
@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
sutasom says:
can u send me CVHaar.h please?
consuelo says:
Hi, can you send me CVHaar.h please?
Thank you!
Cheers,
Consuelo
Jamal Sami says:
can u please send the CVHaar.h to me.
thanks in advance
Menda says:
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 ?
friism says:
@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
chamy says:
Please send me the CVHaar.h
Thank you. 🙂
rahul says:
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
J says:
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…
Stam says:
Hi
Very interesting article
Can you send me the file CVHaar.h cause the discussion page is removed from google groups
Thank you
orpheus says:
Please send me the CVHaar.h
Thank you. 🙂
admin says:
@orpheus, @Stam I don’t have CVHaar.h anymore — sorry. I recommend you use EmguCV instead: http://www.emgu.com
delta says:
Please send me the CVHaar.h
Thank you.
arvind says:
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
eorra says:
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
koraim says:
Please send me the CVHaar.h
Thank you.
Alexis Banaag says:
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..:)
eliecer says:
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
Yonatan H. says:
Nice job, pls don’t stop working… u v good eyez in pgming…
BTW,I’m BSE grad of CE
bulut says:
ı can’t install opencvdotnet to opencv2.2 why
niken says:
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.
dave says:
looks like a 50 loc with a call to a method with 1000s loc 😀
don’t fool people mate 😉
htc-hd2-android says:
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
Tanvir Ahmed says:
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
Kassem Bagher says:
hello,
what about Face recognition?
can you please suggest a good library for .NET
Greets,
Kassem
Cyber noob says:
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.
Razie says:
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…:-)