This article aims to quickly build a Python face recognition program to easily train multiple images per person and get started with recognizing known faces in an image.
In this article, the code uses ageitgey’s face_recognition API for Python. This API is built using dlib’s face recognition algorithms and it allows the user to easily implement face detection, face recognition and even real-time face tracking in your projects or from the command line.
At first, make sure the required libraries are installed on your computer.
pip install face_recognition Scikit-learn dlib docopt
If you want dlib to use CUDA on GPU, make sure CUDA and cuDNN are installed correctly then install dlib using pip. For detailed instructions for installation on different platforms, check out face_recognition’s Installation Guide.
Now, create a directory with the training images for each person in the below format. Make sure that you’ve enough training images (at least 5 for each person) otherwise Support Vector Classifier (SVC) (that is used in this program – produces meaningless results on very small datasets.
face_recognize.py test_image.jpg train_dir/ person_1/ person_1_face-1.jpg person_1_face-2.jpg . . person_1_face-n.jpg person_2/ person_2_face-1.jpg person_2_face-2.jpg . . person_2_face-n.jpg . . person_n/ person_n_face-1.jpg person_n_face-2.jpg . . person_n_face-n.jpg
The face_recognition API generates face encodings for the face found in the images. A face encoding is basically a way to represent the face using a set of 128 computer-generated measurements. Two different pictures of the same person would have similar encoding and two different people would have totally different encoding.
After all the face encodings are generated, Support Vector Classifier (SVC) with scikit-learn is trained on the face encodings along with their labels from all the known faces in the training directory. Finally, the API detects all the faces in the test image you provide and the trained SVC predicts all the known faces in the test image.
Code : To recognize multiple faces.
How to run it on the terminal:
python face_recognize.py -d train_dir -i test_image.jpg
- Python | Face recognition using GUI
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- ML | Face Recognition Using PCA Implementation
- Deep Face Recognition
- ML | Face Recognition Using Eigenfaces (PCA Algorithm)
- ML | Implement Face recognition using k-NN with scikit-learn
- Face Comparision Using Face++ and Python
- Opencv Python program for Face Detection
- Face Detection using Python and OpenCV with webcam
- Python | Named Entity Recognition (NER) using spaCy
- Python | Speech recognition on large audio files
- Python | Reading contents of PDF using OCR (Optical Character Recognition)
- Google Chrome Dino Bot using Image Recognition | Python
- Speech Recognition in Python using Google Speech API
- Multiple inheritance in Python
- ML | Unsupervised Face Clustering Pipeline
- Back-Face Detection Method
- Python - Replace multiple words with K
- Chain Multiple Decorators in Python
- Python | Append multiple lists at once
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.