FaceAuth or how to easily embed FaceID into any .NET application

There are many different authentication methods in programming, and each of them has both its pros and cons. In this article, I would like to review a library that allows you to easily add face authentication to your .NET application.

A few words about me

My name is Salakhetdinov Orkhan and I am a .NET developer. I like to learn something new in the field of programming (and not only) and I want to contribute to the development of open-source. I would also like to add that this is my first article, I would like to write another (maybe several) articles about face recognition systems and how they work in simple language. If there is a good asset and feedback, then why not? Judge as you wish, since I am glad for any feedback, the exception is the total annihilation of the post (optional).

How did it all start?

Not so long ago, just a few months ago, I became curious about the topic of computer vision and its part – face recognition. I’ve always liked Apple’s FaceID feature as it’s very convenient and practical, and I thought: is there such a library for the .NET platform? Unfortunately, after a short search, I have not found something similar for .NET. In the end, I decided to create my own face authentication library. The purpose of this library is primarily PR show .NET (and beyond) developers how easy it is to use Face Authentication in their projects. She, by the way, is in NuGet so in GitHub

Let’s get started

Let’s install it ourselves first Nuget

dotnet add package FaceAuth

I would like to add that when you download this Nuget package, you will additionally download its dependencies, namely Emgu.CV And Emgu.CV.runtime.windows. If you have Linux or Mac, then we replace Emgu.CV.runtime.windows on Emgu.CV.runtime.ваша ос.

After installing the library, we can write the simplest code for face authentication.

using FaceAuth;

var auth = new FaceAuthProvider();

// Check and Initialize out camera
if (auth.CameraInitialize())
{
    // Initialize FaceRecognizer model
    auth.LoadFaceRecognizer();
    
    //Register
    auth.RegisterIFace("The name of the person you are registering", 10);

    // Recognize face
    auth.Recognize();
}
else
{
    Console.WriteLine("Your Camera Not Found!");
}

Let’s analyze the code in detail

1.

var auth = new FaceAuthProvider();

A DNN (deep neural network) is initialized to find and select a face and the model itself to determine the face. Also when you first build the project on the way \bin\Debug\net6.0\ folder is created Assetswhich will contain two files: deploy.prototxt And res10_300x300_ssd_iter_140000_fp16.caffemodel. These files are needed to initialize our DNN.

2.

auth.CameraInitialize()

As the name implies, our camera is initialized, or rather, it is searched for. If there is a camera, we will be returned trueotherwise – false.

auth.LoadFaceRecognizer();

In this code, the face recognition model is searched and trained.

First, there is a folder search TrainedFaces, which contains the faces to be trained. If there is none, then it is created. If the folder exists, then all photos of faces are taken from there, and they are fed to the model.

The face storage structure for training looks like this:

└── TrainedFaces/           # Папка для хранения изображений лиц
    ├── Имя 1/              # Имя зарегестрированного человека
        ├── Фотография1.jpg
        ├── Фотография2.jpg
        ├── Фотография3.jpg
    ├── Имя 2/              # Имя зарегестрированного человека
        ├── Фотография1.jpg
        ├── Фотография2.jpg
        ├── Фотография3.jpg
    ├── Имя 3/              # Имя зарегестрированного человека
        ├── Фотография1.jpg
        ├── Фотография2.jpg
        ├── Фотография3.jpg                

All images are, of course, black and white to make the face recognition model work more efficiently.

4.

auth.RegisterIFace("The name of the person you are registering", 10);

This is where the registration process takes place. under the word регистрирования they are meant to be saved in the project, according to the structure described above.

5.

auth.Recognize();

And finally, face recognition itself. The first thing the method does Recognize() so it checks if the model is trained, if not then it throws an exception Recognition Model not trained.

About the face recognition model itself

Our face recognition model, namely the Fisher Face Recognizer, uses algorithms Principal Component Analysis (PCA) And Linear Discriminant Analysis (LDA) to extract the characteristic features of the face. It is not worth going into details about how facial recognition models work in principle in this article. This is worth writing a separate article about, but I can say briefly that our model looks at different parts of the face and looks for those that help distinguish one person from another. She compares the new face with the faces she has trained on and determines if it is registered or not.

Adviсe

Here are tips from GitHub the library itself.

  • For a big job, you need from 50-200 images of one person in different head positions.

  • You can put auth.Recognize() in a 10 iteration loop and get at least 1 true for successful authentication.

        // Recognize face
        for (int i = 0; i < 10; i++)
        {
            if (auth.Recognize())
            {
                Console.WriteLine("Successful authentication");
                return;
            }
        }
    
        Console.WriteLine("Failed authentication");
  • Recognize() is desirable to do not in an infinite loop, for more efficient authentication.

  • It is recommended to make an anti-spam system to protect against hacking.

    bool TryRecognize(FaceAuthProvider _auth, ref int _recognizeCount)
    {
        _recognizeCount++;
        return _auth.Recognize();
    }
    
    int recognizeCount = 0;
    
    for (int i = 0; i < 10; i++)
    {
        if (recognizeCount <= 5)
        {
            TryRecognize(auth, ref recognizeCount);
        }
        else
        {
            Console.WriteLine("You cant recognize more");
            return;
        }
    }

Conclusion

I would really like to see FaceAuth in large projects, maybe I will add it to my projects. I hope I’ve been able to make some kind of contribution to the open-source community, and to .NET developers in general. I hope you enjoyed this article, I look forward to comments and activity 🙂

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *