In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to build a face recognition model with Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to build a face recognition model with Python".
01 introduction
Do you realize that every time you upload a photo to Facebook, the platform will use a face recognition algorithm to identify the people in the picture? At present, some governments are using face recognition technology to identify and catch criminals. In addition, the most common application is to unlock the phone through your own face.
The subfield of computer vision is widely used, and many business activities around the world have benefited from it. The use of face recognition models will continue to grow in the next few years, so let's learn how to build face recognition models from scratch.
Firstly, this paper will introduce the internal working principle of the face recognition model. Then combined with a simple case, we will practice the case through Python. In the * * section of this article, you will complete your face recognition model!
02 understand the working principle of face recognition
In order to understand the working principle of face recognition algorithm, let's first understand the concept of feature vector. (translator's note: the eigenvector here refers to the concept of machine learning, which is different from matrix theory. )
Each machine learning algorithm takes the dataset as input and learns from it. The algorithm traverses the data and identifies the patterns in the data. For example, suppose we want to recognize the faces of people in a given picture, many objects can be thought of as patterns:
The length / width of the face.
Because the picture scale will be adjusted, the length and height may not be reliable. However, after zooming the picture, the scale remains the same-the ratio of the length and width of the face does not change.
The complexion of the face.
The width of local details on the face, such as mouth, nose, etc.
Obviously, there is a pattern at this time-different faces have different dimensions, and similar faces have similar dimensions. The challenge is to convert specific faces into numbers, because machine learning algorithms can only understand numbers. A number that represents a face (or an element in a training set) can be called an eigenvector. A feature vector includes various numbers in a particular order.
To take a simple example, we can map a face to an eigenvector. Feature vectors are composed of different features, such as:
Length of face (cm)
Width of the face (cm)
The average skin color of the face (R _ GMAE _ B)
Lip width (cm)
Nose length (cm)
When a picture is given, we can label different features and convert them into the following feature vectors:
In this way, our picture is now transformed into a vector, which can be expressed as (23.1, 15.8, 255, 224, 189, 4.4). Of course, we can also derive countless other features (such as hair color, beard, glasses, etc.) from the picture. In this simple example, however, we only consider these five simple features.
Now, once we decode each picture into a feature vector, the problem becomes easier. Obviously, when we use two facial images of the same person, the extracted feature vectors are very similar. In other words, the "distance" between the two Eigenvectors becomes very small.
At this time, machine learning can help us accomplish two things:
Extract feature vectors. Because there are too many features, it is very difficult to list all the features manually. A machine learning algorithm can label many features automatically. For example, a complex feature might be the ratio of nose length to forehead width. It is very difficult to list all these derived features manually.
Matching algorithm: once the feature vector is obtained, the machine learning algorithm needs to match the new picture with the feature vector in the corpus.
Now that we have a basic understanding of how face recognition works, let's use some widely used Python libraries to build our own face recognition algorithms.
03 case study
First of all, give some pictures of the faces of some people-perhaps some celebrities, such as Mark Zuckerberg, Warren Buffett, Bill Gates, Shah Rukh Khan, etc., and regard these faces as our corpus. Now, we give some new pictures of other celebrities ("new people") and determine whether these "new characters" are in the corpus.
The following are the pictures in the corpus:
As shown in the picture, the celebrities we listed are Barack Obama, Bill Gates, Jeff Bezos, Mark Zuckerberg, Ray Dalio and Shah Rukh Khan.
Now, suppose the "new people" are as follows:
▲ Note: all the above pictures are from Google pictures.
Obviously, this is Shah Rukh Khan. However, for computers, this task is very challenging. Because for us, we can easily combine a variety of features of the picture to determine which character it is. However, for computers, it is very unintuitive to learn how to recognize other people's faces.
There is a magical but simple python library that encapsulates the above-mentioned content-you can generate feature vectors based on facial features and know how to distinguish different faces. This python library is called face_recognition. It uses dlib-- 's modern C++ toolkit, which contains machine learning algorithms to help complete complex C++-based applications.
The face_ options library in Python can accomplish a number of tasks:
Find all the faces in a given picture.
Discover and process the facial features in the picture.
Identify the face in the picture.
Real-time face recognition.
Next, we will explore the third of these tasks-identifying faces in pictures.
You can get the source code for the face_ alternatives library at the following link on github.
04 Python application
This part includes the code to build a simple face recognition system using face_ replacement library. This is a part of the application operation, and we will interpret the code in the next section to understand more details.
# import the libraries import os import face_recognition # make a list of all the available images images = os.listdir ('images') # load your image image_to_be_matched = face_recognition.load_image_file (' my_image.jpg') # encoded the loaded image into a feature vector image_to_be_matched_encoded = face_recognition.face_encodings (image_to_be_matched) [0] # iterate over each image for image in images: # load the Image current_image = face_recognition.load_image_file ("images/" + image) # encode the loaded image into a feature vector current_image_encoded = face_recognition.face_encodings (current_image) [0] # match your image with the image and check if it matches result = face_recognition.compare_faces ([image_to_be_matched_encoded] Current_image_encoded) # check if it was a match if result [0] = = True: print "Matched:" + image else: print "Not matched:" + image
The file structure is as follows:
Facialrecognition:
Fr.py
My_image.jpg
Images/
Barack_obama.jpg
Bill_gates.jpg
Jeff_bezos.jpg
Mark_zuckerberg.jpg
Ray_dalio.jpg
Shah_rukh_khan.jpg
Warren_buffett.jpg
Our root directory, facialrecognition, includes:
Fr.py in the form of face recognition code.
My_image.jpg-the picture to be identified ("new people").
Images/-Corpus.
If you create the file structure and execute the code as described above, here are the results you can get:
Matched: shah_rukh_khan.jpg Not matched: warren_buffett.jpg Not matched: barack_obama.jpg Not matched: ray_dalio.jpg Not matched: bill_gates.jpg Not matched: jeff_bezos.jpg Not matched: mark_zuckerberg.jpg
Obviously, the new celebrity is Shah Rukh Khan and our face recognition system can recognize it!
05 understand Python code
Now let's interpret the code and understand how it works:
# import the libraries import os import face_recognition
The above is the introduction operation. We will read all the pictures in the corpus through the built os database, and complete the algorithm part through face_recognition.
# make a list of all the available images images = os.listdir ('images')
This simple code will help us identify the paths of all the pictures in the corpus. Once this code is executed, we can get:
Images = ['shah_rukh_khan.jpg',' warren_buffett.jpg', 'barack_obama.jpg',' ray_dalio.jpg', 'bill_gates.jpg',' jeff_bezos.jpg', 'mark_zuckerberg.jpg']
Now, the following code will load the picture of the new character:
# load your image image_to_be_matched = face_recognition.load_image_file ('my_image.jpg')
In order to ensure that the algorithm can parse the image, we convert the face image into a feature vector:
# encoded the loaded image into a feature vector image_to_be_matched_encoded = face_recognition.face_encodings (image_to_be_matched) [0]
The rest of the code is relatively simple:
# iterate over each image for image in images: # load the image current_image = face_recognition.load_image_file ("images/" + image) # encode the loaded image into a feature vector current_image_encoded = face_recognition.face_encodings (current_image) [0] # match your image with the image and check if it matches result = face_recognition.compare_faces ([image_to_be_matched_encoded] Current_image_encoded) # check if it was a match if result [0] = = True: print "Matched:" + image else: print "Not matched:" + image
At this point, we:
Loop each image.
The image is parsed into feature vectors.
Compare the pictures that have been loaded and the pictures of new characters identified in the corpus.
If the two match, we'll show it. If it doesn't match, we also have to display the results.
As shown above, the results show that this simple face recognition algorithm is going well. Let's try to replace my_image with another picture:
When you run the algorithm again, you will see the following results:
Not matched: shah_rukh_khan.jpg Not matched: warren_buffett.jpg Not matched: barack_obama.jpg Not matched: ray_dalio.jpg Not matched: bill_gates.jpg Not matched: jeff_bezos.jpg Not matched: mark_zuckerberg.jpg
Obviously, the system does not recognize Jack Ma as any of the above celebrities. This means that our algorithm performs well in the following areas:
Correctly identify those who are stored in the corpus.
Label the characters that do not exist in the corpus.
Application of 06 face recognition algorithm
Face recognition is a mature research direction, which has been widely used in industry and academia. For example, a criminal arrested in China may benefit from a face recognition system: the system recognizes his face and issues an alarm. Thus, facial recognition can be used to reduce crime. There are many other interesting cases of face recognition:
Facial authentication: Apple introduced Face ID in iPhones for facial authentication. Some banks also try to unlock it using facial authentication.
User service: some banks in Malaysia have installed new face recognition systems to identify valuable bank customers so that banks can provide them with personal services. In turn, banks can make more money by maintaining such users and improving user satisfaction.
Insurance industry: many insurance companies are making the compensation process easier by using face recognition systems to match people's faces with photos provided by ID.
Thank you for reading, the above is the content of "how to build a face recognition model with Python". After the study of this article, I believe you have a deeper understanding of how to build a face recognition model with Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.