×

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python.

Face recognition is of great utility in real-world scenarios. It is an extended step of face detection. The former allows only the detection of the location of human faces in a human body, but the latter allows the identification of humans in any picture or even in a video.

Application

Face recognition is mainly used for security purposes. Let us see some of the applications.

  1. It is useful for ride-sharing companies to confirm the right person is picked by the right driver.
  2. It is needed in biometrics in a number of areas.
  3. It is of great use at the checkpoints of international borders to ensure the right person is crossing the boundary.
  4. It can automate many existing human-aided systems.

Earlier Methods:

Now, let us learn the involved steps in the detection of faces.

Following are the four steps that were performed one by one in a separate module in the earlier days

  1. Detection of Face
  2. Alignment of Face
  3. Extractions of features
  4. Recognition of face

Newer Methods:

These days, A single library is available which enables the user to perform these four steps in one step only.

Steps:

  • Installing the required librarie

dilib

# installing the library named dilib
pip install dlib


face recognition

# installing the  library named face recognition
pip install face recognition

Opencv

# installing the library named opencv
pip install opencv
  • Importing the required libraries

After the installation of the libraries, these three libraries need to be imported.

import cv2
import numpy as np
import face_recognition
  • Loading the image

Image loading comes after importing.

A library named face_recognition loads images in the form of BGR,
To be able to print the image, with the aid of OpenCV, one should convert the image into RGB  (Red, Green and Blue).

imgelon_bgr = face_recognition.load_image_file('flower.jpg')
imgelon_rgb = cv2.cvtColor(imgelon_bgr,cv2.COLOR_BGR2RGB)
cv2.imshow('bgr', imgelon_bgr)
cv2.imshow('rgb', imgelon_rgb)
cv2.waitKey(0)
  • Draw Bounding Boxes after finding the location of the face

One needs to draw a bounding box around the human face just to show whether the human face has been detected or not.

Training the Image for Face Recognition

This library is made in such a way that it discovers the face and works only on faces, Thus, the requirement of cropping the face out of pictures gets eliminated.

Training:

At this step, we alter the train image into nearly encodings and accumulate the encodings with the provided name of the individual for that image.

Testing:

For testing, an image is loaded and converted into encodings, and then encodings are matched with the stored encodings during training, this matching is grounded on finding extreme resemblance. When the encoding matching the test image is found, one gets the tag connected with train encodings.

If the individual in both images is the same, True is returned. Otherwise, False is returned.

Building a Face Recognition System

To build a face recognition system, one needs to import certain libraries.

Following are the libraries that need to be imported:

import cv2
import face_recognition
import os
import numpy as np
from datetime import datetime
import pickle

Challenges confronted by the Face Recognition Systems

Making a face recognition system is not as easy as it seems to be. There exist numerous challenges in building the model. Following are the challenges faced during the creation of the model:

  • Illumination: Illumination causes change in the appearance of the face drastically; it is detected that even the slightest deviations in lighting conditions causes an important influence on its results.
  • Pose: Facial Recognition systems are extremely delicate to the pose, which may result in damaged recognition or no recognition if the folder is only accomplished on the front face view.
  • Facial Expressions: The same person can give different expressions and the model can get confused. However, Modern recognizers can easily overcome this issue.
  • Low Resolution: In this, Training should be done on a picture of good quality or good resolution otherwise the model will fail to recognize the image and extract features from it.
  • Aging: With the growing age, the features of the human face such as figure, shapes, and texture changes too.

Summary:

In this tutorial, we have understood face recognition in python, its applications, and constraints. We saw some steps to create a face recognition model.


Related Topics

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Difference between Expression and Statement in Python

What is an expression in Python? Expression is a combination of operands and operators. Expression helps us to produce some other values. In the python programming language,  expressions produce some other value...

6 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Find Words in String Python

Python : Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in...

5 minutes read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.