This repository contains the implementation of a facial recognition system based on similarity measurement. We focuses on measuring the similarity between facial features and uses a similarity-based approach to recognize faces.
In this repository, we utilize the YOLOv5 model that has been pre-trained specifically for the task of face detection. YOLOv5 is a state-of-the-art object detection algorithm known for its accuracy and efficiency. I already put the yolov5s.pt
inside.
The Siamese Model is responsible for embedding facial images into feature vectors. It takes an input image and converts it into a feature vector A. The system then matches this feature vector with a pre-embedded database of facial feature vectors to find the feature vector B with the smallest angular difference from A. The output of this recognition process is the corresponding identity label for the feature vector B.
To measure the similarity between feature vectors, we employ the cosine similarity. The cosine similarity can be calculated using the formula:
In this equation, A
and B
are the feature vectors being compared. The cosine similarity value ranges from -1 to 1, where 1 indicates perfect similarity and -1 indicates complete dissimilarity.
We utilize the MagFace loss function, which is based on angular-margin-based classification. The MagFace loss function is described in the following paper:
To get started, follow these steps:
- Clone this repository by running the following command:
git clone https://github.com/htuannn/face-recognition
cd face-recognition
- Install all the necessary packages by running:
pip install -r requirements.txt
- Ensuring your image-database is set up according to the following structure:
face-recognition/
│
├── data_recognition/
│ ├── raw
│ │ ├── PersonA
| │ │ ├img1.jpg
| │ │ └...
│ │ ├── PersonB
| │ │ ├img1.jpg
| │ │ └...
| │ └...
| │
│ ├──preprocessed
│ │ ├── PersonA
| │ │ ├img1_cropped.jpg
| │ │ └...
| │ └...
Note-1:face-recognition/data_recognition/raw
is the place to store the entire database of each person's raw images. Each sub-folder here corresponds to one identity, with the name of the sub-folder being the person's name.
Note-2: face-recognition/data_recognition/preprocessed
is the place to store the entire database of each person's facial cropped images. The storage rules here are the same as above.
With the images that you have collected, which maybe a photo of the whole person, now we will cut out the face separately. To do this, run the following command:
python align_data.py
The processed images will be stored in the following path: data_recognition/preprocessed
To convert the preprocessed face images into embedding vectors, run the following command:
python face_embedding/embedding_feat.py --backbone iresnet50 --resume weights\models\magface_iresnet50_MS1MV2_dp.pth --folderdataset_dir data_recognition/preprocessed --feat_list data_recognition/preprocessed/face_embdding.txt
For real-time face recognition inference using a webcam input, run the following command:
python rec_cam.py --backbone iresnet50 --resume weights\models\magface_iresnet50_MS1MV2_dp.pth --view-img