current position:Home>Learn how to use opencv and python to realize face recognition!
Learn how to use opencv and python to realize face recognition!
2022-01-31 12:34:47 【TSINGSEE】
In this article , We'll look at what face recognition is and how it differs from face detection ?
Let's first briefly understand the principle of face recognition , Then jump to the coding section !!
At the end of this article , You can also develop a human face recognition program to recognize faces in images !!!
Overview of face detection
What if the machine could automatically detect objects in the image without human intervention ?
Everyone's face may be slightly different , But after all, it's safe to say , All faces have specific features . At present, there are various face detection algorithms , but Viola-Jones Algorithm is the oldest method still in use today .
Face detection is usually a step towards many face related applications ( Such as face recognition or face verification ) The first step . however , Face detection has very useful applications . One of the most successful applications of face detection may be “ Taking pictures ”. Example : When you click a friend's photo , The camera with built-in face detection algorithm will detect the position of the face and adjust the focal length accordingly .
Overview of face recognition
Generally speaking , Face recognition is a method of face recognition or personal identity verification . Various algorithms can be used for face recognition , But their accuracy may vary . In this article, I will discuss with you how to use deep learning for face recognition . Now let's learn how to use deep learning to recognize faces .
ad locum , We use face embedding , Each face is converted into a vector . The technology of converting face into vector is called depth measurement learning . Let me divide this process into three simple steps , In order to be better 、 It's easier to understand :
1. Face detection :
The first task we perform is to detect the image ( Photo ) Or face in video stream . Now we know the exact coordinates of the face / Location , So we extract this face for further processing .
2. feature extraction :
Now we see that we have cut the face from the image , Therefore, we extract specific features . ad locum , We will see how to use face embedding to extract these features of human face .
As we know , The neural network takes the face image as the input , And output a vector representing the most important features of the face ! In machine learning , This vector is nothing more than an embedding , So we call it vector face embedding .
Now how will this help identify different people's faces ? When we train neural networks , Network learning outputs similar vectors for faces that look similar . Let's consider an example , If I have multiple face images at different time intervals , Obviously, some characteristics may change, but not too much . So in this case , The vectors associated with faces are similar , Or we can say that they are very close in vector space .
thus , We've seen how this network works , Let's see how to use this network on our own data . ad locum , We pass all the images in the data to this pre trained network , To get the corresponding embeddings and save them in a file for the next step .
3. Face comparison :
We save the face embedding of each face in the data in the file , The next step is to identify new images that are not in our data . therefore , The first step is to calculate the face embedding of the image using the same network we used before , Then compare this embedding with the rest we have . If the generated embedding is closer or similar to any other embedding , We can recognize faces .
Understand what is OpenCV
In the field of artificial intelligence , Computer vision is one of the most interesting and challenging tasks . Computer vision acts as a bridge between computer software and visualization . Computer vision allows computer software to understand and understand the visualization of the surrounding environment .
Let's look at an example : According to the shape 、 Color and size determine the fruit . This task is easy for the human brain , But in the computer vision pipeline , First, we need to collect data , Then we perform data processing operations , Then we train and teach the model to understand how to distinguish fruit based fruit The size of the fruit 、 Shape and color .
Now , There are various software packages that can be used to perform machine learning 、 Deep learning and computer vision problems . up to now , Computer vision is the best module to solve such complex problems .OpenCV It's an open source library . It is used by different programming languages such as R、Python Other support . It may run on most platforms , Such as Windows、Linux and macOS.
OpenCV The advantages of :
1. Open CV It's free. , It's an open source library .
2. Open CV Fast , Because it uses C/C++ language-written , Compared with other languages
3. System RAM The less ,OpenCV The better the result. .
4. Support most operating systems , Such as Windows、Linux、macOS.
perform
In this section , We will use OpenCV and Python Face recognition is realized .
First , Let's see what libraries we need and how to install them :
1.OpenCV
2.dlib
3. Face recognition
OpenCV Is a video and image processing library , For image and video analysis , Such as face detection 、 License plate reading 、 Photo editing 、 Advanced robot vision, etc .
dlib The library contains our “ Depth measurement learning ” Realization , It is used to construct a face embedding algorithm for the actual recognition process .
face_recognition The library is very easy to use , We'll use it in our code .
First , In the installation face_recognition Remember to install before dlib library . Use OpenCV The output of any face recognition application will be as follows :
To install OpenCV、dlib And face recognition , Type the following code snippet at the command prompt .
pip install opencv-pythonconda install -c conda-forge dlibpip install face_recognition
Copy code
Now let's do the code !
Extract features from human faces
First you need a data set , Even create your own dataset . Just make sure all the images are arranged in a folder , Each folder contains only one person's image .
Now? , Save the dataset in the same folder as the file you want to make . This is the code. , I added comments where needed :
from imutils import paths #imutils includes opencv functions
import face_recognition
import pickle
import cv2
import os
#get paths of each file in folder named Images
#Images here that contains data(folders of various people)
imagePath = list(paths.list_images('Images'))
kEncodings = []
kNames = []
# loop over the image paths
for (i, ip) in enumerate(imagePath):
# extract the person name from the image path
name = ip.split(os.path.sep)[-2]
# load the input image and convert it from BGR
image = cv2.imread(ip)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb,model='hog')
# compute the facial embedding for the any face
encodings = face_recognition.face_encodings(rgb, boxes)
# loop over the encodings
for encoding in encodings:
kEncodings.append(encoding)
kNames.append(name)
#save emcodings along with their names in dictionary data
data = {"encodings": kEncodings, "names": kNames}
#use pickle to save data into a file for later use
f = open("face_enc", "wb")
f.write(pickle.dumps(data))#to open file in write mode
f.close()#to close file
Copy code
Now we see that we have stored the embedded in a file named “face_enc” In the file of , So we can use them to identify images ( Photo ) Or face in real-time video stream . In the next section, we'll see how to recognize faces from images .
How to recognize the face in the image
The following script is used to detect and recognize faces in images . I give comments next to the code where needed to help beginners understand the code effectively .
import face_recognition
import imutils #imutils includes opencv functions
import pickle
import time
import cv2
import os
#to find path of xml file containing haarCascade file
cfp = os.path.dirname(cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
# load the harcaascade in the cascade classifier
fc = cv2.CascadeClassifier(cfp)
# load the known faces and embeddings saved in last file
data = pickle.loads(open('face_enc', "rb").read())
#Find path to the image you want to detect face and pass it here
image = cv2.imread(Path-to-img)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#convert image to Greyscale for HaarCascade
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = fc.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=6,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
# the facial embeddings for face in input
encodings = face_recognition.face_encodings(rgb)
names = []
# loop over the facial embeddings incase
# we have multiple embeddings for multiple fcaes
for encoding in encodings:
#Compare encodings with encodings in data["encodings"]
#Matches contain array with boolean values True and False
matches = face_recognition.compare_faces(data["encodings"],
encoding)
#set name =unknown if no encoding matches
name = "Unknown"
# check to see if we have found a match
if True in matches:
#Find positions at which we get True and store them
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
count = {}
# loop over the matched indexes and maintain a count for
# each recognized face face
for i in matchedIdxs:
#Check the names at respective indexes we stored in matchedIdxs
name = data["names"][i]
#increase count for the name we got
count[name] = count.get(name, 0) + 1
#set name which has highest count
name = max(count, key=count.get)
# will update the list of names
names.append(name)
# do loop over the recognized faces
for ((x, y, w, h), name) in zip(faces, names):
# rescale the face coordinates
# draw the predicted face name on the image
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX,
0.75, (0, 255, 0), 2)
cv2.imshow("Frame", image)
cv2.waitKey(0)
Copy code
Output
Conclusion
Now we have completed the process of face recognition .
Expanding reading
Face recognition technology is a high-precision technology 、 Easy to use 、 High stability 、 Difficult to counterfeit biometric technology , It has an extremely broad market application prospect . In public security 、 The defence 、 customs 、 traffic 、 Finance 、 social security 、 There is a wide range of demand in industries and departments such as medical and other civil safety control .
We TSINGSEE The R & D personnel of Qingxi video are also actively developing face detection recently 、 Face recognition 、 People flow statistics 、 Helmet detection, etc AI technology , And actively integrate into the existing video platform . Typical examples are EasyCVR Video convergence cloud service , have AI Face recognition 、 License plate recognition 、 Voice talk 、 Pan tilt control 、 Audible and visual alarm 、 The ability of monitoring video analysis and data collection , It is widely used in residential areas 、 Intelligent access control of buildings , Suspicious personnel around the perimeter are detected 、 In scenes such as traffic statistics in the scenic spot .
copyright notice
author[TSINGSEE],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311234445940.html
The sidebar is recommended
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
guess what you like
-
How IOS developers learn python programming 3-operator 2
-
How IOS developers learn python programming 2-operator 1
-
[Python applet] 8 lines of code to realize file de duplication
-
Python uses the pynvml tool to obtain the working status of GPU
-
Data mining: Python actual combat multi factor analysis
-
Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
-
Use Python VTK to batch read 2D slices and display 3D models
-
Complete image cutting using Python version VTK
-
Python interface automation test framework (basic) -- common data types Dict
-
Django (make an epidemic data report)
Random recommended
- Python specific text extraction in actual combat challenges the first step of efficient office
- Daily python, Part 8 - if statement
- Django model class 1
- The same Python code draws many different cherry trees. Which one do you like?
- Python code reading (Chapter 54): Fibonacci sequence
- Django model class 2
- Python crawler Basics
- Mapping 3D model surface distances using Python VTK
- How to implement encrypted message signature and verification in Python -- HMAC
- leetcode 1945. Sum of Digits of String After Convert(python)
- leetcode 2062. Count Vowel Substrings of a String(python)
- Analysis of Matplotlib module of Python visualization
- Django permission management
- Python integrated programming -- visual hot search list and new epidemic situation map
- [Python data collection] scripy realizes picture download
- Python interface automation test framework (basic part) -- loop statement of process control for & while
- Daily python, Chapter 9, while loop
- Van * Python | save the crawled data with docx and PDF
- Five life saving Python tips
- Django frequency control
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
- leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
- Python interface automation test framework (basic) -- basic syntax
- Detailed explanation of Python derivation
- Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
- A brief note on the method of creating Python virtual environment in Intranet Environment
- [worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
- [Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
- The unknown side of Python functions
- Python based interface automation test project, complete actual project, with source code sharing
- A python artifact handles automatic chart color matching