Face Detection

In the beginning, there is an image that we get as input. We know about it only simple parameters like the dimensions. We first have to find out whether there is a face at all or perhaps even several. If we have found faces, we know where they are located in the image, so that we can create appropriate crops.

Live Demo: Face Detection
Bounding Box Color:

Get started with an image that contains one or more human faces.

- or -

Now that we've cropped the faces out of our input image, we can move on to face recognition, right? While we could do that, and it would work to a certain extent, there's one more step in the middle that will greatly improve our recognition result: Aligning the face.

Hands-on examples

Face recognition may be more impressive than just finding faces. Nevertheless, there are quite some useful practical applications that we can already implement at this point. To see what you can do with face detection only, check out some of our demos:

Try this yourself

If you're writing your own application, you can use the exact same face detection mechanism that is used in the demo above from your own code. The following example uses C# as programming language and leverages the FaceAiSharp library.

  1. Install a recent version of .NET.
  2. Create a new console app project by running this command in your favorite shell in an empty folder:
    
    dotnet new console
  3. Install two packages providing the relevant code and models:
    
    dotnet add package Microsoft.ML.OnnxRuntime
    dotnet add package FaceAiSharp.Bundle
  4. Replace the content of the Program.cs file with the following code:
    
    using FaceAiSharp;
    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.PixelFormats;
    
    using var hc = new HttpClient();
    var groupPhoto = await hc.GetByteArrayAsync(
        "https://raw.githubusercontent.com/georg-jung/FaceAiSharp/master/examples/obama_family.jpg");
    var img = Image.Load<Rgb24>(groupPhoto);
    
    var det = FaceAiSharpBundleFactory.CreateFaceDetector();
    var faces = det.DetectFaces(img);
    foreach (var face in faces)
    {
        Console.WriteLine($"Found a face with confidence {face.Confidence}: {face.Box}");
    }
  5. Run the program you just created:
    
    dotnet run
    You should see output similar to:
    
    Found a face with confidence 0.89665085: RectangleF [ X=176.05847, Y=79.44571, Width=62.074905, Height=84.46396 ]
    Found a face with confidence 0.88412976: RectangleF [ X=454.50806, Y=88.49284, Width=65.97302, Height=78.17378 ]
    Found a face with confidence 0.8757608: RectangleF [ X=383.41547, Y=82.51477, Width=63.718292, Height=95.04758 ]
    Found a face with confidence 0.87154955: RectangleF [ X=249.2981, Y=103.57684, Width=57.793823, Height=83.17267 ]
An error has occurred. This application may no longer respond until reloaded. Reload 🗙