This article was originally published here.
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:
- Microsoft Visual Studio 2010. Or if you don't have one, you can use 2008 version
- 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
- Basic knowledge of C# Programming
- Familiar in WPF development
After you've got what you need, it's time to rock!
First thing you should do is install Emgu CV. Your installation path should be like C:\Emgu\emgucv-windows-x86 22.214.171.1240. And you can see inside C:\Emgu\emgucv-windows-x86 126.96.36.1990\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.
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:
Now, copy the below code to make our user experience. Put this code in your MainWindow.xaml file.
<Image Name="image1" Stretch="Fill" />
Next, let's code it! Open your MainWindow.xaml.cs and add this code on top.
Initialize two objects
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;
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);
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);
foreach (var face in detectedFaces)
currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
image1.Source = ToBitmapSource(currentFrame);
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
private static extern int DeleteObject(IntPtr o);
public static BitmapSource ToBitmapSource(IImage image)
using (System.Drawing.Bitmap source = image.Bitmap)
IntPtr ptr = source.GetHbitmap();
BitmapSource bs = System.Windows.Interop
Here is the result of our work:
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 188.8.131.520\bin.