Facial Recognition using OpenCV

The face can be considered as the unique identity of an individual. People across the world have unique faces and facial features. Considering these facts, facial recognition is implemented in the real world.

A Facial Recognition System can be defined as a technology that can identify or verify a person from a digital image or video source by comparing and analyzing patterns based on the person’s facial contours.

Since face is a unique way of identifying people, facial recognition has gained high attention and growing rapidly across the world for providing safe and reliable high level of security. Facial recognition is now considered to have more advantages when compared to other biometric systems like palm print and fingerprint since facial recognition doesn’t need any human interaction.

By using Deep Learning, part of AI, a Facial Recognition System can be built. Here we use the OpenCV to build a simple Face Recognition Model.OpenCV is an open source computer vision and machine learning software library.


*Import all the required libraries.

*Define some functions which are used for face detection.

*The first function detects the face in the image and returns a grayscale image with a box around the face by using haar classif.

*The second function takes the directory as the input and returns the faces along with its labels.

*The remaining functions are used for training Haar classifier, drawing bounding boxes around the faces and to input the text on the boxes.

*Once all the functions are defined, we pass a test image to recognize the faces in the test image.

*The next step is to train our model.

*In the final step, our model detects the face in the test image and draws a bounding box around the face and tries to predict the person in the image.


  • In the present world, Facial Recognition is being extensively used in surveillance systems.
  • It is also being used in crime detection and forensic.
  • Facial recognition is also being used in payments to make secure and reliable online payments.
  • It is being used in mobile phones for unlocking. This is a powerful way to protect personal data and ensure that, if a phone is stolen, sensitive data remains inaccessible by the perpetrator.