Face Recognition with Python and Deep Learning in 5 Lines 10 Minutes
In this post I’ll show you how to create Face Recognition in 5 line with Python by using deep learning. Face Recognition and Detection is mostly the first project of any ML or AI student. Now you no need to write thousand of lines to perform this simple task.
All the code and resources are provided at the end, if you love my content just clap the article and subscribe me YouTube for future updates.
Demo and Code-
Content-
- What is Face Recognition
- What is Face Detection
- Introduction of ‘auto_face_recognition’ python library
- How things works
- Code
- Future Updates
- Resources and GitHub Code
Let’s Start-
1. What is Face Recognition-
In simple words, identify individual faces / person’s face.
According to Wikipedia- A facial recognition system is a technology capable of identifying or verifying a person from a digital image or a video frame from a video source.
2. What is Face Detection
In simple words, detecting faces of humans. Detection doesn’t means recognition, only finding features of human face from image or video.
According to Wikipedia- Face detection is a computer technology being used in a variety of applications that identifies human faces in digital images. Face detection also refers to the psychological process by which humans locate and attend to faces in a visual scene.
3. Introduction of ‘auto-face-recognition’ python library
It is the python library created by me, I want to reduce the complexity of this task, so that’s why I have created this library. Will talk about its working later.
You can check official Pypi page of this library here: https://pypi.org/project/auto-face-recognition/
Install the latest version- pip install auto-face-recognition
About auto-face-recognition: It is a python library for the Face Recognition. This library make face recognition easy and simple. This library uses Tensorflow 2.0+ for the face recognition and model training.
Prerequisite- To use it only Python (> 3.6) is required.
4. How things works-
Generally machine learning architecture follow few steps- data gathering, preprocessing, training, then prediction on unseen data with exported model.
What if this data gathering, training and prediction can be done using few lines of code for Image Recoginition.
The answers is yes, its possible using auto-face-recognition.
5. Code-
This is the complete code you need-
Explanation-
Import Libray-
import auto_face_recognition
Create Object-
obj = auto_face_recognition.AutoFaceRecognition()
Create Dataset-
obj.datasetcreate(haarcascade_path='haarcascade/haarcascade_frontalface_default.xml',
eyecascade_path='haarcascade/haarcascade_eye.xml',
class_name='Dipesh')
This will create dataset for you. You need to pass these three params atleast-
:param class_name: str (example: ‘folder of class’)
:param haarcascade_path: str (example: haarcascade_frontalface_default.xml)hars
:param eyecascade_path: str (example: haarcascade_eye.xml)
haarcascade_frontalface_default.xml
and haarcascade_eye.xml
is the opencv haarcascade files. You need to place these files in your local storage and provide path to the params. class_name
is the folder name you want to create.
A folder name ‘datasets/Dipesh’ will be created which contains training images-
Note- Create Atleast Two folder for the classification. One folder can be also trained but it doesn’t make any sense.
Training-
obj.face_recognition_train()
It will automatically start training, and it will automatically find path of dataset. If you canged the param of dataset dir in ‘datasetcreate’ function then change the dataset dir in ‘face_recognition_train’ accordingly. The default directory is ‘datasets’.
Training Summary-
This graphs will generated automatically-
Prediction-
obj.predict_faces(class_name=['Dipesh', 'Jay'], color_mode=True)
Here make sure to pass the class_name=[‘class_1’, ‘class2 ’]. You can chosse color_mode is True / Flase.
It will start recognizing your face-
Optional Parameter You Can Play Around-
Parameters You Can Choose-
datasetcreate()
datasetcreate(dataset_path='datasets', class_name='Demo',
haarcascade_path='haarcascade/haarcascade_frontalface_default.xml',
eyecascade_path='haarcascade/haarcascade_eye.xml', eye_detect=False,
save_face_only=True, no_of_samples=100,
width=128, height=128, color_mode=False)
""""
Dataset Create by face detection
:param dataset_path: str (example: 'folder_of_dataset')
:param class_name: str (example: 'folder_of_dataset')
:param haarcascade_path: str (example: 'haarcascade_frontalface_default.xml)
:param eyecascade_path: str (example: 'haarcascade_eye.xml):param eye_detect: bool (example:True)
:param save_face_only: bool (example:True)
:param no_of_samples: int (example: 100)
:param width: int (example: 128)
:param height: int (example: 128)
:param color_mode: bool (example:False):return: None
"""
face_recognition_train()
face_recognition_train(data_dir='datasets', batch_size=32, img_height=128, img_width=128, epochs=10,
model_path='model'):
"""
Train TF Keras model according to dataset path
:param data_dir: str (example: 'folder_of_dataset')
:param batch_size: int (example:32)
:param img_height: int (example:128)
:param img_width: int (example:128)
:param epochs: int (example:10)
:param model_path: str (example: 'model')
:return: None
"""
predict_faces()
predict_faces(self, class_name=None, img_height=128, img_width=128,
haarcascade_path='haarcascade/haarcascade_frontalface_default.xml',
eyecascade_path='haarcascade/haarcascade_eye.xml', model_path='model',
color_mode=False):
"""
Predict Face
:param class_name: Type-List (example: ['class1', 'class2'] )
:param img_height: int (example:128)
:param img_width: int (example:128)
:param haarcascade_path: str (example: 'haarcascade_frontalface_default.xml)
:param eyecascade_path: str (example: 'haarcascade_eye.xml)
:param model_path: str (example: 'model')
:param color_mode: bool (example: False)
:return: None
"""
6. Future-
Note- I haven’t gone through the stress test, like maximum number of classes.
Finetuning, custom models and your suggestions.
Follow me here or YouTube or Instagram for future updates.
7. Resources and GitHub Code-
Pipy: https://pypi.org/project/auto-face-recognition/
Github: https://gist.github.com/Dipeshpal/ed0431234f1c8873249cfdd5f4dc88e4
YouTube: https://www.youtube.com/DIPESHPAL17
YouTube Face Recognition Demo: https://youtu.be/EU0LHtEJTnE