In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve face recognition based on face_recognition. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Preparatory work
Our face recognition is based on the face_ alternative library. Face_recognition is based on dlib and uses deep learning training data, and the accuracy of the model is as high as 99.38%. Before we start our work, let's install face_recognition
Pip install face_recognition
Face digitization
The step of face recognition is to detect the face area in the photo, and then convert the face image data into a vector with a length of 128. These 128 data represent the 128 feature indicators of the face, as shown below.
For each known face, such a 128-bit vector is generated. For an unknown face, compare its 128-bit vector with the 128-bit vector of all known faces to find the one with similarity * *, that is, to find the person corresponding to the unknown face.
Picture data
We have prepared two photos, one of the Duchess of Cambridge and one of Prince William, which are saved as catherine.jpg and william.jpg respectively. The faces in these two photos are our known faces.
Our goal is to identify the two faces in the group photo below and mark their names in the picture. The following picture is saved as unknown.jpg
Code implementation
Next, let's start our programming work.
Import cv2 import face_recognitionnames = ["catherine", "william",]
First we define the label set, which is stored in the names array.
The tag name is also the file name of our picture.
Images = [] for name in names: filename = name + ".jpg" image = face_recognition.load_image_file (filename) images.append (image) unknown_image = face_recognition.load_image_file ("unknown.jpg")
Call face_recognition.load_image_file to read data from the picture.
Here we read the data containing the pictures of known faces and unknown faces, and the picture of unknown faces is the group photo unknown.jpg above.
Face_encodings = [] for image in images: encoding = face_recognition.face_encodings (image) [0] face_encodings.append (encoding) unknown_face_encodings = face_recognition.face_encodings (unknown_image)
Face_recognition.face_encodings returns the 128bit vectors of all the faces in the picture. There is only one face in a single photo, so face_recognition.face_encodings (image) [0] takes only * * elements. The group photo contains two faces, so the unknown_face_encodings contains two 128bit vectors.
Face_locations = face_recognition.face_locations (unknown_image) for i in range (len (unknown_face_encodings)): unknown_encoding = unknown_face_ encodings [I] face_location = face_ locations [I] top, right, bottom, left = face_location cv2.rectangle (unknown_image, (left, top), (right, bottom), (0,255,0) 2) results = face_recognition.compare_faces (face_encodings, unknown_encoding) for j in range (len (results)): if results [j]: name = names [j] cv2.putText (unknown_image, name, (left-10, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,255,0) 2) unknown_image_rgb = cv2.cvtColor (unknown_image, cv2.COLOR_BGR2RGB) cv2.imshow ("Output", unknown_image_rgb) cv2.waitKey (0)
Face_locations stores the location information of each face.
In the loop we call the cv2.rectangle box to show each face detected.
Face_recognition.compare_faces compares the 128bit vectors of known faces with the 128bit vectors of each unknown face, and the results are stored in the results array. Each element in the results array is True or False, and the length is equal to the number of faces. Every element in results corresponds to the known face one by one, and the element in a certain position is True, which indicates that the unknown face is recognized as this known face.
For each face identified, we call cv2.putText to label the image.
That's all of the code.
test
The exciting moment has come again! Let's test our results.
By running the above program, you can see the following results
The faces of Prince William and the Duchess of Cambridge have been accurately identified. The green box shows the face area, and the name of the identified person is marked at the top of the box.
On how to achieve face recognition based on face_recognition to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.