Click here to Skip to main content
Click here to Skip to main content
Go to top

Camera Face Detection in C# using Emgu CV and WPF

, 5 Aug 2014
Rate this:
Please Sign up or sign in to vote.
How to perform Face Detection using your camera / Webcam.

Camera Face Detection in C# using Emgu CV and WPF
Hi there, 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 how to do that.

This 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: http://www.emgu.com/wiki/index.php/Main_Page 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 installing Emgu CV. Your installation path should be like C:\Emgu\emgucv-windows-x86 2.2.1.1150. And you can see inside C:\Emgu\emgucv-windows-x86 2.2.1.1150\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 code below to make our user experience. Put this code in your MainWindow.xaml file

<Window x:Class="WpfFaceDetectionTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800" Loaded="Window_Loaded">
  <Grid>
    <Image Name="image1" Stretch="Fill" />
  </Grid>
</Window>

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 class 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()
{
  InitializeComponent();
}

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);
  timer.Start();
}

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.

[DllImport("gdi32")]
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
      .Imaging.CreateBitmapSourceFromHBitmap(
      ptr,
      IntPtr.Zero,
      Int32Rect.Empty,
      System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

    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 to 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 in the same directory with your executable file. You can find those files inside C:\Emgu\emgucv-windows-x86 2.2.1.1150\bin.

References

http://friism.com/webcam-face-detection-in-c-using-emgu-cv
http://www.emgu.com/wiki/index.php/Main_Page

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Junian Triajianto
Software Developer (Senior) Junian Legacy
Indonesia Indonesia
Just an ordinary geek.
 
For more information visit my blogs:
Junian Legacy
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionUsing Kinect Sensor Instead PinmemberMember 107553948-Jul-14 14:15 
QuestionQuery PinmemberMember 1066498116-Mar-14 20:39 
BugLoading issue PingroupAkshay 00712-May-13 0:34 
QuestionFace Detection program not working PinmemberMalikSanawer19-Nov-12 9:49 
My first question is that what will be coded in haarcascade_frontalface_alt_tree.xml file ???
 
I paste the code from Emgu/emgucv-windows-x86 2.4.0.1717/bin/haarcascade_frontal_default.xml
to my xml file but still its not working and program crashes at the start.
 
Please help me asap....
AnswerRe: Face Detection program not working PinmemberMember 1093824710-Jul-14 9:46 
QuestionBlank Image Box [modified] Pinmembertushar gupta11-Nov-12 21:12 
GeneralMy vote of 5 Pinmemberamish kumar6-Oct-12 0:31 
GeneralRe: My vote of 5 Pinmembermomina sheikh8-Nov-12 3:17 
QuestionFace Recognition? PinmemberCenk KIZILDAĞ26-Sep-12 20:14 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 5 Aug 2014
Article Copyright 2012 by Junian Triajianto
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid