Face recognition with Python "including source code"

Posted by ina on Thu, 10 Feb 2022 16:14:52 +0100

Python can detect and recognize your face from images or videos.

Face detection and recognition is one of the research hotspots in the field of computer vision.

The applications of face recognition include face unlocking, security protection, etc. doctors and medical staff use face recognition to obtain medical records and medical history and better diagnose diseases.

It's actually easy to get started with Python, but we have to keep learning. It's difficult to keep learning every day. I believe many people give up after learning for a week. Why? In fact, without good learning materials for you to learn, it is difficult for you to adhere to. This is the python introductory learning materials collected by Xiaobian. Pay attention to, forward, and send a private letter Xiaobian "01" for free! I hope it will help you

About Python face recognition

In this python project, we will build a machine learning model that recognizes people from images. We used the face recognition API and OpenCV in the project.

Keep abreast of the latest technology trends
Join DataFlair's telegram!

Tools and libraries

  • Python-3.x
  • CV2-4.5.2
  • Pudgy -1.20.3
  • Face recognition-1.3.0

To install the above packages, use the following command.

pip install numpy opencv-python

To install FaceRecognition, first install the dlib package.

pip install dlib

Now install the facial recognition module using the following command

pip install face_recognition

Download face recognition Python code

Please download the source code of python face recognition project: face recognition engineering code

Project dataset

We can use our own data set to complete this face recognition project. For this project, let's take the popular American network series "friends" as the data set. The dataset is included in the facial recognition project code, which you downloaded in the previous section.

Steps of establishing face recognition model

Before continuing, let's know what face recognition and detection is.

Face recognition is the process of recognizing or verifying a person's face from photos and video frames.

Face detection refers to the process of locating and extracting faces (position and size) from images for use by face detection algorithms.

Face recognition method is used to locate the only specified feature in the image. In most cases, facial images have been removed, cropped, scaled, and converted to grayscale. Face recognition includes three steps: face detection, feature extraction and face recognition.

OpenCV is an open source library written in C + + It includes various algorithms for computer vision tasks and the implementation of deep neural network.

1. Prepare data set

Create 2 directories, training and testing. Choose a picture for each actor from the Internet and download it to our "train" directory. Ensure that the image you select can well display the characteristics of the face, so as to classify the classifier.

To test the model, let's take a picture containing all the casts and put it in our "test" directory.

For the sake of your training program and comfort, we have added our data.

2. Model training

First import the necessary modules.

import face_recognition as frimport cv2import numpy as npimport os

The implementation of various face recognition utility programs.

Now, create 2 lists to store the names of the images (people) and their respective face codes.

path = "./train/"known_names = []known_name_encodings = []images = os.listdir(path)

Face coding is a vector of values, which represents important measures between facial features, such as the distance between eyes, the width of forehead and so on.

We cycle through each image in the train directory, extract the name of the person in the image, calculate its face coding vector, and store the information in the corresponding list.

for _ in images:image = fr.load_image_file(path + _)image_path = path + _encoding = fr.face_encodings(image)[0]known_name_encodings.append(encoding)known_names.append(os.path.splitext(os.path.basename(image_path))[0].capitalize())

3. Test the model in the test data set

As mentioned earlier, our test data set contains only one image containing all personnel.

Use the CV2 imread() method to read the test image.

test_image = "./test/test.jpg"image = cv2.imread(test_image)

The face recognition library provides a method called Face_Locations() is a useful method to locate the coordinates (left, bottom, right, top) of each face detected in the image. Using these position values, we can easily find the face code.

face_locations = fr.face_locations(image)face_encodings = fr.face_encodings(image, face_locations)

We loop through each face position and its encoding in the image. Then, we compare this coding with the face coding in the "train" dataset.

Then calculate the face distance, that is, calculate the similarity between test image coding and training image coding. Now, we select the minimum distance from it to indicate that the face of the test image is one of the people in the training data set.

Now, draw a rectangle with face position coordinates using the method in CV2 module.

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):   matches = fr.compare_faces(known_name_encodings, face_encoding)   name = ""   face_distances = fr.face_distance(known_name_encodings, face_encoding)   best_match = np.argmin(face_distances)   if matches[best_match]:       name = known_names[best_match]   cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)   cv2.rectangle(image, (left, bottom - 15), (right, bottom), (0, 0, 255), cv2.FILLED)   font = cv2.FONT_HERSHEY_DUPLEX   cv2.putText(image, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

Use the imShow() method of CV2 module to display the image.

cv2.imshow("Result", image)

Use the imwrite() method to save the image to the current working directory.

cv2.imwrite("./output.jpg", image)

Release resources that have not been released (if any).


Python face recognition output

Let's look at the output of the model.


In this machine learning project, we use our own custom data set to develop a human face recognition model in python and OpenCV.

Topics: Python Programming Programmer AI Computer Vision