Detect Faces With C# And Dlib In Only 40 Lines Of Code
By: Mark Farragher
Tags: Programming, Dotnet, Dotnet Core, Facedetection, Computer Vision

A hot research area in computer vision is to build software that understands the human face. The most obvious application is Face Recognition, but we can also do lots of other cool stuff like Head Pose Estimation, Emotion Detection, Eye Gaze Detection, and Blink Detection.

A cool example of facial analysis in real life is Chrysler’s self-driving car system called Supercruise. When the car is in self-driving mode, it wants the driver to have their attention on the road at all times. So when the driver looks away or appears to fall asleep, an alarm sounds right away.

How does the car pull this off? There’s a little camera on the steering wheel pointed at the driver, and the car software performs face analysis in real time.

Building face analysis apps is surprisingly easy. There are amazing computer vision libraries available that make building computer vision apps a breeze.

In this article I’ll use Dlib. This is the go-to library for face detection. It’s intended for C and C++ projects, but Takuya Takeuchi has created a NuGet package called DlibDotNet that exposes the complete Dlib API to C#.

I am going to build an app that can detect all faces that are visible in any image. I’ll use C#, Dlib, DlibDotNet, and NET Core v3, and try to achieve my goal with the minimum of code.

NET Core is the Microsoft multi-platform NET Framework that runs on Windows, OS/X, and Linux. It’s the future of cross-platform NET development.

I’ll use the following image to test my app:

This is the famous selfie Ellen DeGeneres took at the Oscars in 2014. It’s a great test image because everybody is looking at the camera and we have a couple of celebrities with their face only partly visible.

Save this image as ‘input.jpg’.

Let’s get started. Here’s how to set up a new console project in NET Core:

$ dotnet new console -o DetectFaces

$ cd DetectFaces

Next, I need to install the ML.NET packages I need:

$ dotnet add package DlibDotNet

That was easy! This single NuGet package installs Dlib and the DlibDotNet wrapper, and sets everything up for your operating system.

If you’re working on a Mac like me, you’ll have to perform one extra step. DlibDotNet requires the XQuartz library but it’s not installed by default on a clean OS/X system.

You can easily install XQuartz with homebrew:

$ brew cask install xquartz

And that’s it.

Now I’m ready to add some code. Here’s what Program.cs should look like:

The Dlib.GetFrontalFaceDetector method loads a face detector that’s optimized for frontal faces: people looking straight at the camera.

Next, I have to load the image and perform face detection:

The Dlib.LoadImage<RgbPixel> method loads the image in memory with interleaved color channels. The Operator method then performs face detection on the image.

The faces variable now holds an array of Rectangle structs. Each rectangle describes where the face detector found a face in the image.

To highlight the detection results I’m calling Dlib.DrawRectangle to draw a rectangle on the image at the location of each face.

The final step is to save the modified image. Add this to the end of the Main method:

This uses the Dlib.SaveJpeg method to save the image as output.jpg.

And that’s it!

You can run this app on Linux, OS/X or Windows with Visual Studio Code: