Master the face recognition library 'InsightFace', learn about face authentication and the structure of AI models (1)

What is Face Authentication?

Face authentication is the process of verifying whether a person is who they claim to be. In a face authentication system, the provided facial image is compared with pre-registered images to confirm the identity of the person. Systems unlocking doors through face recognition are becoming commonplace.

What's the Difference with Face Recognition?

Face recognition involves extracting features from a face image and searching for a person with those features. Face authentication confirms a match with a pre-registered person. Face authentication answers the question 'Is this person who they claim to be?' whereas face recognition answers 'Who is this person in the database?'. Although there’s a clear difference between them, face authentication often utilizes face recognition, leading to potential confusion. In this course, we will also use face recognition for implementing face authentication.

What is InsightFace?

InsightFace is a python library that can be used for face analysis tasks like face recognition, face detection, and face attribute estimation. It employs deep learning models and is entirely open-source, with its code publicly available. It is an ideal learning material for those looking to delve into application development using deep learning, and for understanding the mechanisms behind modern AI systems.

Is InsightFace Free to Use?

Yes, InsightFace is open-source and completely free. It is also under the MIT license, making it available for commercial use.

All source codes of InsightFace can be found here.

Let’s Try Using InsightFace (Sample Code Included)

Let's try using InsightFace. Install it using the pip command. After the installation is complete, try running the code below. This code takes two images from the camera with a one-second interval, detects the face from the images, and extracts face features (embedding). By comparing these vectors, you can calculate face similarity. Try running the code below, and the similarity of the faces displayed will be shown.

import cv2
import numpy as np
import insightface
from import FaceAnalysis
import time

# 1枚目の画像を撮影
cap = cv2.VideoCapture(0)
img_1 =[1]
h, w = img_1.shape[:2]
# rgbへ変換
img_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2RGB)

# 1秒末

# 2枚目の画像を撮影
cap = cv2.VideoCapture(0)
img_2 =[1]
h, w = img_2.shape[:2]
# rgbへ変換
img_2 = cv2.cvtColor(img_2, cv2.COLOR_BGR2RGB)

app = FaceAnalysis()
app.prepare(ctx_id=0, det_size=(640, 640))
# 1枚目の画像から顔を検出
faces = app.get(img_1)
if len(faces) == 0:
face_1 = faces[0]
# 2枚目の画像から顔を検出
faces = app.get(img_2)
if len(faces) == 0:
face_2 = faces[0]

# Compare face embeddings
embedding_1, embedding_2 = face_1.embedding, face_2.embedding
similarity =, embedding_2) / (
    np.linalg.norm(embedding_1) * np.linalg.norm(embedding_2)
print(f"1枚目と2枚目に映った顔の類似度は: {similarity}")

With just this code, you’ve implemented face authentication. InsightFace is a library that allows easy implementation of face authentication. However, in this course, the goal is not just to use InsightFace but to dissect it and learn how AI is implemented. If your goal is just to quickly use face authentication, the above code is sufficient and you might not need to read further. The content hereafter is aimed at intermediate programmers and may include complex content.

Breaking Down the Sample Code

Now, let's dissect InsightFace while looking at the actual source code. Implementing practical AI models is significantly complex compared to using functions through libraries.

There are two main points in the sample source code above. The first one is,

app = FaceAnalysis()
app.prepare(ctx_id=0, det_size=(640, 640))

The second one is,

faces = app.get(img_1)

These are very simple three lines of code, but what are these codes actually doing? Let’s look inside the InsightFace code!

Let’s Look Inside the InsightFace Code

Once you have installed InsightFace, open the installation folder. You can confirm the installation folder with,


pip show insightface

With that, you should be able to check the source code. You can also confirm the same on the InsightFace’s GitHub repository, which should be identical to the content in the installation folder.

The relevant page of the official GitHub repository of InsightFace can be found here

Let's Take a Look at

Open the found in the installation folder. Inside this file, there’s a FaceAnalysis class with a prepare method. It appears this class is used to perform face authentication. Below is that part

class FaceAnalysis:
    def __init__(self, name=DEFAULT_MP_NAME, root='~/.insightface', allowed_modules=None, **kwargs):
        self.models = {}
        self.model_dir = ensure_available('models', name, root=root)
        onnx_files = glob.glob(osp.join(self.model_dir, '*.onnx'))
        onnx_files = sorted(onnx_files)
        for onnx_file in onnx_files:
            model = model_zoo.get_model(onnx_file, **kwargs)
            if model is None:
                print('model not recognized:', onnx_file)
            elif allowed_modules is not None and model.taskname not in allowed_modules:
                print('model ignore:', onnx_file, model.taskname)
                del model
            elif model.taskname not in self.models and (allowed_modules is None or model.taskname in allowed_modules):
                print('find model:', onnx_file, model.taskname, model.input_shape, model.input_mean, model.input_std)
                self.models[model.taskname] = model
                print('duplicated model task type, ignore:', onnx_file, model.taskname)
                del model
        assert 'detection' in self.models
        self.det_model = self.models['detection']

    def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640)):
        self.det_thresh = det_thresh
        assert det_size is not None
        print('set det-size:', det_size)
        self.det_size = det_size
        for taskname, model in self.models.items():
            if taskname == 'detection':
                model.prepare(ctx_id, input_size=det_size, det_thresh=det_thresh)
    // omitted below

AI Models Are Downloaded When FaceAnalysis Class Is Executed

If you closely follow the code above, you’ll realize that during the initialization of the FaceAnalysis class, AI models are downloaded within the ensure_available() function. We’re omitting the internal code of ensure_available() here, but reading the code tells us that a '.insightface' directory is created in the home directory of the PC being used, and models are downloaded there.

The source page for the download is here.

Looking inside, you can see multiple files with the '.onnx' extension.

Similarly, on your PC, in the '.insightface' folder in the home directory, you should find the same files downloaded.

The Downloaded .onnx Files

  1. 1k3d69.onnx
  2. 2d106det.onnx
  3. det_10g.onnx
  4. genderage.onnx
  5. w600k_r50.onnx

Surprisingly, you’ll find five .onnx models downloaded. It seems InsightFace’s face authentication operates using these five AI models! We will explore each of these models in the next article, so stay tuned!

By the Way, What is ONNX?

ONNX stands for Open Neural Network Exchange. It’s a common format designed to easily share and port models between different machine learning frameworks and tools. Models trained in popular machine learning frameworks like PyTorch, TensorFlow, and MXNet can be exported in the ONNX format. This makes it easy to migrate models to different frameworks for inference. As found in our investigation, InsightFace also handles AI models in the ONNX format.