Camera Face Detection in C# using Emgu CV and WPF
Detecting faces from an image is simple with the power of Emgu CV, wrapper of OpenCV in .NET
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 or 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:
- 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: 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!
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.
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:
Step 3
Now, copy code below to make our user experience. Put this code in your MainWindow.xaml
file
Step 4
Next, let’s code it! Open your MainWindow.xaml.cs
and add this code on top.
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.
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.
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
.
Here is the result of our work:
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
Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Download Links
Fork or download the completed project on GitHub.
Further Reading
If you’re interested in Computer Vision topic, not just limited to face detection, I suggest to read the latest book written by Dr. Adrian Kaehler and Dr. Gary Rost Bradski: Learning OpenCV 3 (or the older Learning OpenCV book published in 2008, which is cheaper in price but using the old API).
Even though the code samples are written in C/C++, you can easily transform it into C# with Emgu CV wrapper.