Simple Face Recognition with Facial Landmark & K-Nearest Neighbors

Ragil Prasetyo
6 min readJul 2, 2023

--

Face recognition is a biometric technology that aims to identify or verify an individual’s identity by analyzing and comparing unique facial features. It involves capturing an image or video of a person’s face, extracting distinctive facial characteristics, and matching them against a database of known faces. This technology has gained significant importance in real-world applications due to its numerous advantages and potential applications.

One of the key reasons why face recognition is important in the real world is its ability to provide a non-intrusive and convenient method of identification. Unlike traditional methods such as ID cards or passwords, face recognition allows for seamless and contactless authentication, making it suitable for various scenarios such as access control, surveillance, and border control. It eliminates the need for physical tokens or memorizing complex passwords, enhancing user convenience and improving efficiency.

Another crucial aspect is the high accuracy and reliability of face recognition systems. With advancements in machine learning and computer vision algorithms, modern face recognition systems can achieve remarkable accuracy in identifying individuals, even in challenging conditions such as variations in lighting, pose, and facial expressions. This reliability makes face recognition a valuable tool in law enforcement, where it can assist in identifying suspects or missing persons, preventing crimes, and enhancing public safety.

in today technology there are many methods that can be used to perform face recognition such as:

  • Local Binary Patterns (LBP) : LBP encodes the local texture patterns of an image by comparing the intensity of each pixel with its neighboring pixels. It captures facial texture information and has been widely used for face recognition.
  • Deep Learning-based Method : Deep learning approaches, particularly Convolutional Neural Networks (CNNs), have achieved remarkable success in face feature extraction. Pre-trained CNN models, such as VGGFace, FaceNet, and DeepFace, can extract high-level facial features that are effective for face recognition tasks.
  • Eigenfaces: Eigenfaces is a dimensionality reduction technique based on Principal Component Analysis (PCA). It represents faces as a linear combination of basis images, known as eigenfaces, which capture the most significant variations in the face images.
  • Gabor Wavelets: Gabor wavelets are used to capture the spatial-frequency characteristics of an image. Gabor filter responses at different frequencies and orientations are computed to extract texture information from facial images.
  • And many more method

There are many methods that can be used to perform face recognition. Among these many methods, each method has its own advantages and disadvantages. Here will try to use a slightly different method to do face recognition

We will create a simple face recognition using Facial Landmarks and the K-Nearest Neighbors (KNN) algorithm. But before that, we will explain a little about what is Facial Landmark and K-nearest Neighbors

Facial Landmark

Facial Landmark refer to specific points or key locations on a face that are used to identify and analyze facial features. These landmarks are typically defined as coordinates or positions on the face, such as the corners of the eyes, nose, mouth, and other facial structures. By accurately detecting and tracking these landmarks, facial analysis algorithms can extract valuable information about facial expressions, gestures, and pose. Facial landmarks provide a foundation for tasks such as face alignment, emotion recognition, facial animation, and facial feature extraction, enabling a deeper understanding of the intricate details and dynamics of the human face.

Facial Landmark Example

Using the dots generated by the facial landmark, we will create a feature of those points. then the results of the feature extraction will be trained using the KNN algorithm.

K-Nearest Neighbors

k-Nearest Neighbors (k-NN) is a simple yet effective algorithm used for both classification and regression tasks. It works on the principle of finding the k closest data points in the training dataset to a given query point and using their labels (in the case of classification) or values (in the case of regression) to make predictions for the query point. The algorithm measures the distance between the query point and each training sample using a distance metric (e.g., Euclidean distance) and selects the k nearest neighbors. For classification, the most frequent class label among the k neighbors is assigned to the query point, while for regression, the average or weighted average of the target values is used. The k-NN algorithm is easy to understand, flexible, and doesn’t require training. However, the choice of k and the distance metric can significantly impact its performance.

K-Nearest Neighbor Illustration

using the KNN algorithm we will create a machine learning that can do face recognition

Preprocessing Image

The first process is to do simple preprocessing on images such as resize and grayscale images then use haarcascade to get faces from images then use facial landmarks to get points that will later be used for feature extraction. The following is the preprocessing process that will be carried out later

Preprocessing Image

Feature Extraction Explain

Feature extraction in the image is done by calculating the distance of several points on the face with the midpoint on the nose. This method is done with the assumption that each face of the person will be different in distance from the midpoint in the nose. The following is a visualization of the point distance that will be calculated against the midpoint of the nose.

Feature Extraction Illustration

The points used are the corner of the eye and the corner of the lips. In total there are 6 points that are calculated the distance from the midpoint of the nose. To calculate the distance itself using the Eucladian Distance method, here is the Eucladian Distance formula.

Eucladian Distance Formula

Dataset

In this discussion we will use a dataset as below, the dataset used this time is only a few but can be implemented for larger datasets

Dataset

Here we will use 3 faces as training data and make some changes to the image such as giving glasses as data testing.

Result

The training dataset will be preprocessed and feature extraction to get the feature. Furthermore, the feature will be used by KNN for the training process. After the training model, the model can predict the image on the testing data. Here are the results

Face Recognition Results

The 5 images in the testing dataset were successfully classified using the KNN algorithm and correct all of them. This indicates that the method works even with a small dataset. The confusion matrix below is shown to show the results more clearly

Confusion Matrix

Summary

The method above gets 100% accuracy results in doing face recognition, with these results it can be said that this method is quite successful in doing face recognition tasks. even with a small dataset, KNN can still do a good classification. But keep in mind that the image used in this experiment is a simple image. So when implemented in real-word, this method may decrease in accuracy.

To see the complete code, you can see the following github repo

Thank you for reading this article until the end, see you in another articles.

--

--

Ragil Prasetyo

Data Enthusiast | Data Science Lecture at Purwadhika Digital Technology School