Python OpenCV Human Face Recognition Project

Hello and welcome to PythonGeeks! We are developing an advanced system that utilizes facial recognition technology to accurately identify individuals based on their unique facial features.

Facial Recognition: Understanding the Basics

Facial recognition technology is a system that utilizes a person’s unique facial features, such as the distance between their eyes or the shape of their jawline, to identify them within images or videos. It is an advanced technology that has become increasingly prevalent in recent years, used in a variety of applications from security systems to mobile devices. The technology compares these features with a database of facial images to find a match. We can use Facial Recognition in many ways, like for security scanning, focusing camera on face, CCTV and making personalized recommendations.

Face Detection VS Face Recognition

When we talk about analyzing facial features, there are two main things that come in picture.

1. Detecting the presence of face
2. Recognizing who that face belongs to

Face detection is when a computer looks at an image or videos and draws the bounding box over it if there is a face in it. It does not care about who the face belongs to, just that it’s there.

face detection

Face recognition is when a computer looks at a face and tries to identify who that person is, based on features like the distance between their eyes and the shape of their face.

face recognition

Understanding OpenCV

OpenCV is a library that helps computers to understand and make sense of visual information like images and videos. It provides programmers with a set of tools and functions that they can use to create applications that can analyze and manipulate these types of data. We can use this library to enable machines to process and understand visual information, allowing them to perceive and interpret the world around them through images and videos.

Facial Feature Extraction with Neural Networks

Facial feature extraction is finding specific parts of the face, like the eyes, nose, and mouth, from a picture. This is really important for computer programs that work with faces, like recognizing people or reading their emotions. To do this, the computer looks for special points on the face and then figures out where the features are using math and also extract the distance between your eyes with some special features. Neural networks are a type of computer program that work like the human brain. They can learn from lots of examples and find complex patterns in data. Using neural networks can make it easier and more accurate to find facial features in pictures. A type of neural network called Convolutional Neural Networks (CNNs) is especially good at this.

Cascades in Face Recognition

Cascades are a machine learning algorithm used for face recognition, which scans an image with a sliding window to identify specific features of the face. This algorithm employs a technique that involves the identification of basic rectangular patterns to recognize facial features, including the eyes, nose, and mouth. The cascade classifier applies a series of filters to determine if the features are present, resulting in a hierarchy of classifiers. Cascades are efficient and effective in detecting and recognizing faces in real-time, but may have limitations in certain situations, such as changes in lighting conditions. Other techniques, like neural networks, may be more suitable in those cases.

Prerequisites for Human Face Recognition using Python OpenCV

It is important to have a solid understanding of the Python programming language and the OpenCV library in order to perform effective human face recognition using Python and OpenCV. You should also know about face detection and recognition algorithms and various libraries required for the project.

Additionally, you should be familiar with preprocessing techniques to ensure the model performs well. Apart from this you should have the following system requirements.

  1. Python 3.7 (64-bit) and above
  2. Any python editor (VS code, Pycharm)
  3. Cmake and dlib setup

Download Python OpenCV Human Face Recognition Project

Please download the source code of Python OpenCV Human Face Recognition Project from the following link: Python OpenCV Human Face Recognition Project

Setting up CMake and Dlib involves the following steps

1. Download cmake software from the link below and install it on your system.

2. Download visual studio from the link below and install it on your system. After installing the setup file select Desktop environment with C++ and click on install.

setting up cmake

Installation

Open windows cmd as administrator

1. To install the opencv library run the command from the cmd.

pip install opencv-python

2. To install face_recognition run the command from cmd.

pip install face_reccognition

3. To install dlib run the command from cmd.

pip install dlib

Real-time Face Recognition using webcam

For doing face recognition we need a dataset of images with name as label of it. For example we are having the images of Mahendra Singh Dhoni, Virat Kohli, Rohit Sharma, and Rishabh Pant.

Note :- Add face_encoding.py file into main folder containing main.py file

This Python script uses face_recognition and OpenCV libraries for face detection and recognition in images or video frames. It has a class that loads images and detects faces in video frames. It uses face encodings to compare faces and retrieve their names. This script provides a basic framework for face detection and recognition in images or video using these libraries.

Folder Structure

folder structure

Let’s Implement

First we are doing face recognition by using a webcam and at the end I will tell you how to do the face recognition in video.

1. We need to import some libraries that will be used in our implementation.

import cv2
from face_encoding import Face

2. By using the function of opencv (VideoCapture) we are opening our intel ie. laptop’s camera. For an external webcam you have to pass ‘1’ in the VideoCapture() function.

cap = cv2.VideoCapture(0)

3. We are calling the Face() class from face_encoding.py in our main file and making the object with the name encode. By using load_encoding_images we are training our dataset.

encode = Face()
encode.load_encoding_images("images/")

4. The script reads the camera input and converts the BGR images to RGB images. This is done to make face identification faster. Once known faces are detected, the script extracts their position coordinates (y1, x2, y2, x1) and draws a rectangle around the face region. It then puts the name of the person over the frame window. If an unknown face is detected, the script prints “unknown” over the frame window instead of a name.

while True:
    ret, frame = cap.read()

    face_area, recognize_name = encode.detect_known_faces(frame)
    for face_coordinates, name in zip(face_area, recognize_name):
        top, right, bottom, left = face_coordinates[0], face_coordinates[1], face_coordinates[2], face_coordinates[3]

        cv2.putText(frame, name,(left, top - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 200), 2)
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 200), 4)

    cv2.imshow("PythonGeeks", frame)

5. When the ‘ESC’ key is pressed, all windows associated with the program will be closed and the program will stop running.

   key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

Face Recognition in Video

After doing the face recognition using a webcam let’s do this in video.

  1. Whatever images you have trained take .mp4 format video of them.
  2. Copy the path of this video and paste in the VideoCapture() function.
cap = cv2.VideoCapture(“virat kohli.mp4”)

opencv face recognition output

This way it will recognize faces from the given video.

Python OpenCV Human Face Recognition Output

face recognition output

Conclusion

OpenCV is a useful tool for creating face recognition systems with high accuracy and reliability. This technology has many practical applications in security, surveillance, and biometric authentication. As computer vision and artificial intelligence continue to advance, face recognition technology is expected to become even more advanced. Developers can use OpenCV to create advanced face recognition applications that can identify people and detect their emotions and facial expressions.

If you are Happy with PythonGeeks, do not forget to make us happy with your positive feedback on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *