Note This Project is still under construction and continuous improvement, nothing is final.
Table of Contents
This Project was achieved using OpenCV and Python for Face Detection and Face Recognition.
OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.
- Python
- OpenCV
With OpenCV we have a choice from multiple algorithms for Face Recognition :
- Eigenfaces.
- Fisherfaces.
- Local Binary Patterns Histograms.
I've used the Local Binary Patterns Histograms algorithm.
LBPH (Local Binary Pattern Histogram) is a Face-Recognition algorithm it is used to recognize the face of a person. It is known for its performance and how it is able to recognize the face of a person from both front face and side face.
It's a simple yet very efficient texture operator which labels the pixels of an image by thresholding the neighborhood of each pixel and considers the result as a binary number.
Note that face recognition is different of face detection :
- Face Detection: it has the objective of finding the faces (location and size) in an image and probably extract them to be used by the face recognition algorithm.
- Face Recognition: with the facial images already extracted, cropped, resized and usually converted to grayscale, the face recognition algorithm is responsible for finding characteristics which best describe the image.
So in order to get good recognition rates you'll need at least 8(+-1) images for each person and the Fisherfaces method doesn't really help here. The above experiment is a 10-fold cross validated result carried out with the facerec framework at: https://github.com/bytefish/facerec. This is not a publication, so I won't back these figures with a deep mathematical analysis. Please have a look into [168] for a detailed analysis of both methods, when it comes to small training datasets.
- LBPH is one of the easiest face recognition algorithms.
- It can represent local features in the images.
- It is possible to get great results (mainly in a controlled environment).
- It is robust against monotonic gray scale transformations.
- It is provided by the OpenCV library (Open Source Computer Vision Library).
Video Demo :
The following packages are needed for this project to work properly :
pip install opencv-python
pip install pillow --upgrade
Imports :
import cv2
import numpy as np
import os
import pickle
from PIL import Image
For Face Detection a Cascade Classifier (haarcascade_frontalface_alt2.xml) was used, it can be found in the following directory :
/cascades/haarcascade_frontalface_alt2.xml
face_cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_alt2.xml')
Here is the code responsible for it :
cap = cv2.VideoCapture(0)
faces = face_cascade.detectMultiScale(grayedFrame, scaleFactor=1.5, minNeighbors=5)
To train the Algorithm several photos were used from photos found on the internet of celebrities and yours truly :
├───images
│ ├───Emilia-Clarke
│ ├───Hamza-Ouggadi
│ ├───Jeff-Bezos
│ └───Peter-Dinklage
The names of the folders were used as labels for each person while training.
recognizer = cv2.face.LBPHFaceRecognizer_create()
The training creates a .yaml file that can be used on other inputs for recognition.
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yaml")
As can be seen below :
recognizer.read("trainner.yaml")
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Hamza Ouggadi - hamza.ouggadi@gmail.com
Project Link: https://github.com/HamzaOuggadi/Facial-Recognition-Detection
- OpenCV Face Recognition Documentation - OpenCV Docs
- OpenCV Python Installation and Usage - OpenCV Python
- TowardsDataScience.com - Face Recognition: Understanding LBPH Algorithm - Understanding LBPH Algorithm