Click here to Skip to main content
15,390,016 members
Articles / Desktop Programming / WPF
Technical Blog
Posted 20 Sep 2012

Tagged as


99 bookmarked

Camera Face Detection in C# using Emgu CV and WPF

Rate me:
Please Sign up or sign in to vote.
4.79/5 (41 votes)
3 Oct 2018MIT2 min read
How to perform Face Detection using your camera / Webcam

This article was originally published here.

Camera Face Detection in C# using Emgu CV and WPF

This is a new tutorial category in my blog. It's Computer Vision. In this blog, I'd like to show you something cool. It's how to perform Face Detection using your camera / webcam. You'll see how your application can detect faces from a captured image. Curious about it? Let's take a look at how to do that.

This is what you need to follow this tutorial:

  1. Microsoft Visual Studio 2010. Or if you don't have one, you can use 2008 version
  2. Emgu CV (OpenCV in .NET). You can download the latest version in this link: and follow the installation instruction
  3. Basic knowledge of C# Programming
  4. Familiar in WPF development

After you've got what you need, it's time to rock!

Lena windows

Step 1

First thing you should do is install Emgu CV. Your installation path should be like C:\Emgu\emgucv-windows-x86 And you can see inside C:\Emgu\emgucv-windows-x86\bin some DLLs and sample programs. You can see a simple face detection app Example.FaceDetection.exe and you'll see something like the first picture in this post.

Emgu CV Location

Step 2

Next, let's open your Visual Studio and create a new WPF Project. Add some references and make sure it'll look like the picture below:

Emgu CV Reference

Step 3

Now, copy the below code to make our user experience. Put this code in your MainWindow.xaml file.

<Window x:Class="WpfFaceDetectionTest.MainWindow"
    Title="MainWindow" Height="600" 
    Width="800" Loaded="Window_Loaded">
    <Image Name="image1" Stretch="Fill" />

Step 4

Next, let's code it! Open your MainWindow.xaml.cs and add this code on top.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Emgu.CV.Structure;
using Emgu.CV;
using System.Runtime.InteropServices;

Step 5

Initialize two objects Capture and HaarCascade. Those are important classes in this tutorial, so you have to make it. And we also need DispatcherTimer to capture the picture every millisecond.

private Capture capture;
private HaarCascade haarCascade;
DispatcherTimer timer;

public MainWindow()

private void Window_Loaded(object sender, RoutedEventArgs e)
  capture = new Capture();
  haarCascade = new HaarCascade(@"haarcascade_frontalface_alt_tree.xml");
  timer = new DispatcherTimer();
  timer.Tick += new EventHandler(timer_Tick);
  timer.Interval = new TimeSpan(0, 0, 0, 0, 1);

Step 6

This last part is the routine. What this code will do is capture image every millisecond, and then convert it to gray frame. After converted, faces will be detected. Each detected faces will be marked by black rectangular.

void timer_Tick(object sender, EventArgs e)
  Image<Bgr,Byte> currentFrame = capture.QueryFrame();

  if (currentFrame != null)
    Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
    var detectedFaces = grayFrame.DetectHaarCascade(haarCascade)[0];
    foreach (var face in detectedFaces)
      currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
    image1.Source = ToBitmapSource(currentFrame);

Step 7

Finally, this additional code is needed to convert plain Bitmap class to BitmapSource so WPF can read it as an image and view it on image1.

private static extern int DeleteObject(IntPtr o);

public static BitmapSource ToBitmapSource(IImage image)
  using (System.Drawing.Bitmap source = image.Bitmap)
    IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

    BitmapSource bs = System.Windows.Interop

    DeleteObject(ptr); //release the HBitmap
    return bs;

Here is the result of our work:

Result 1

Result 2

Okay, I think that's all I can do in this post. See you in my next post.

Note: If you can't run your project, just build it and make sure all opencv_xxxx.dll files and haarcascade_frontalface_alt_tree.xml are in the same directory with your executable file. You can find those files inside C:\Emgu\emgucv-windows-x86\bin.



This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Junian Triajianto
Software Developer (Senior)
Indonesia Indonesia
5+ years experience in Software Engineering. I love to develop solution using C# and .NET Technology very much. I also enjoy building iOS and Android apps using Xamarin. My strength is in algorithm design and analysis. I also have a great ability in solving programming and system trouble.

I regularly write on my blog.

Comments and Discussions

Questioni am facing intialization error in starting Pin
shahab_sns2-Apr-20 6:01
Membershahab_sns2-Apr-20 6:01 
QuestionSeverity Code Description Project File Line Suppression State Suppression State Error CS0103 The name 'Timer' does not exist in the current context WpfMVVM C:\Users\Appinventiv\source\repos\WpfMVVM\WpfMVVM\MainWindow.xaml.cs 30 Active Pin
akashAppinventiv30-Jan-20 0:36
MemberakashAppinventiv30-Jan-20 0:36 
Questionhello master Pin
Member 114897005-Oct-18 1:00
MemberMember 114897005-Oct-18 1:00 
Questionfps Pin
cyttic4-Oct-18 23:02
Membercyttic4-Oct-18 23:02 
QuestionHow to save and load trained face into/from sql server? Pin
Faiq FK12-Nov-17 20:53
MemberFaiq FK12-Nov-17 20:53 
QuestionWhich version of EMGU lib you using? Pin
fan1784-Jun-17 6:19
Memberfan1784-Jun-17 6:19 
QuestionNamespace could not be found Pin
olaoye4life15-May-17 21:05
Memberolaoye4life15-May-17 21:05 
QuestionError "NullReferenceException was unhandled" Pin
Sunil Hirole16-Feb-15 21:27
MemberSunil Hirole16-Feb-15 21:27 
Questioneror Pin
Member 112986459-Dec-14 9:38
MemberMember 112986459-Dec-14 9:38 
QuestionEmgu - rear facing camera Pin
Keeyan16-Oct-14 20:25
MemberKeeyan16-Oct-14 20:25 
QuestionUsing Kinect Sensor Instead Pin
Member 107553948-Jul-14 14:15
MemberMember 107553948-Jul-14 14:15 
GeneralRe: Using Kinect Sensor Instead Pin
maxoptimus5-Oct-18 0:34
Membermaxoptimus5-Oct-18 0:34 
QuestionQuery Pin
Member 1066498116-Mar-14 20:39
MemberMember 1066498116-Mar-14 20:39 
BugLoading issue Pin
Akshay 00712-May-13 0:34
MemberAkshay 00712-May-13 0:34 
QuestionFace Detection program not working Pin
MalikSanawer19-Nov-12 9:49
MemberMalikSanawer19-Nov-12 9:49 
AnswerRe: Face Detection program not working Pin
Member 1093824710-Jul-14 9:46
MemberMember 1093824710-Jul-14 9:46 
QuestionBlank Image Box Pin
tushar gupta11-Nov-12 21:12
professionaltushar gupta11-Nov-12 21:12 
GeneralMy vote of 5 Pin
amish kumar6-Oct-12 0:31
Memberamish kumar6-Oct-12 0:31 
GeneralRe: My vote of 5 Pin
laiza ramal8-Nov-12 3:17
Memberlaiza ramal8-Nov-12 3:17 
if you find the way plz send it to
QuestionFace Recognition? Pin
AstroTheDog26-Sep-12 20:14
MemberAstroTheDog26-Sep-12 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.