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 mainly explains "how to use Python to achieve face recognition", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Python to achieve face recognition"!
1. Installation
It is best to use Linux or Mac environment to install, Windows installation will have a lot of problems. Before installing face_recognition, you need to install the following libraries, pay attention to the order!
1.1 install cmake and boost first
Pip install cmakepip install boost
1.2 install dlib
Pip install dlib
It may take a few minutes to install here. If there is an installation error, it is recommended to use the whl file to install
1.3 install face_recognition
Face_recongnition is usually used with opencv.
Pip install face_recognitionpip install opencv-python
two。 Face recognition
For example, there are a total of three pictures here, of which two are known, and the third is the one that needs to be identified.
First of all, get the information in the face.
Kobe_image = face_recognition.load_image_file ("kobe.jpg") # known Kobe photos jordan_image = face_recognition.load_image_file ("jordan.jpeg") # known Jordan photos unknown_image = face_recognition.load_image_file ("unkown.jpeg") # unknown photos kobe_face_encoding = face_recognition.face_encodings (kobe_image) [0] jordan_face_encoding = face_recognition.face_encodings ( Jordan_image) [0] unknown_face_encoding = face_recognition.face_encodings (unknown_image) [0]
The first three lines of the code load three image files and return the numpy array of the image, and the last three lines return the face coding of each face in the image.
Then compare the face in the unknown image with the face in the known image, using the compare_faces () function, the code is as follows:
Known_faces = [kobe_face_encoding, jordan_face_encoding] results = face_recognition.compare_faces (known_faces, unknown_face_encoding) # list of recognition results print ("is this unknown photo Kobe? {}" .format (results [0]) print ("is this unknown photo Jordan? {}" .format (results [1]))
The running results are as follows:
Less than 20 lines of code can identify who the face is, whether it is so easy or not!
3. Face tagging
Just recognizing the face in the picture always feels bad, so isn't it more interesting to mark the identified face by name?
The recognition of the known picture is basically the same as the previous code, the unknown picture code has more recognition of the location of the face, and uses the face_locations () function. The code is as follows:
Face_locations = face_recognition.face_locations (unknown_image) face_encodings = face_recognition.face_encodings (unknown_image, face_locations)
The function passes in two parameters, and returns the list of facial positions in the above, right, lower, and left fixed order. The function is to compare the known facial position with the unknown facial code, and get the Euclidean distance. I don't know exactly what it is. Distance equals acquaintance.
Function description: face_distance (face_encodings, face_to_compare)
Face_encodings: known facial coding
Face_to_compare: facial codes to compare
The first two pictures remain unchanged, the third one is replaced by a group photo of Kobe Bryant and Jordan, and the final result is as follows:
On the left is the original picture, and on the right is the picture that is automatically tagged after recognition.
Import face_recognitionfrom PIL import Image, ImageDrawimport numpy as npdef draws (): kobe_image = face_recognition.load_image_file ("kobe.jpg") kobe_face_encoding = face_recognition.face_encodings (kobe_image) [0] jordan_image = face_recognition.load_image_file ("jordan.jpeg") jordan_face_encoding = face_recognition.face_encodings (jordan_image) [0] known_face_encodings = [kobe_face_encoding, jordan_face_encoding] known_face_names = ["Kobe" Unknown_image = face_recognition.load_image_file ("two_people.jpeg") face_locations = face_recognition.face_locations (unknown_image) face_encodings = face_recognition.face_encodings (unknown_image, face_locations) pil_image = Image.fromarray (unknown_image) draw = ImageDraw.Draw (pil_image) for (top, right, bottom, left), face_encoding in zip (face_locations, face_encodings): matches = face_recognition.compare_faces (known_face_encodings) Face_encoding) name = "Unknown" face_distances = face_recognition.face_distance (known_face_encodings, face_encoding) best_match_index = np.argmin (face_distances) if matchs [best _ match_index]: name = known_face_ namespace [best _ match_index] draw.rectangle (left, top), (right, bottom), outline= (0,0,255)) text_width, text_height = draw.textsize (name) draw.rectangle ((left, bottom-text_height-10), (right) Bottom), fill= (0,0,255), outline= (0,0,255)) draw.text ((left + 6, bottom-text_height-5), name, fill= (255,255,255,255) del draw pil_image.show () pil_image.save ("image_with_boxes.jpg")
4. Give makeup to the face
This feature needs to be used in conjunction with PIL. The usage is similar. First, load the picture file into the numpy array, and then recognize all the facial features in the face into a list.
Image = face_recognition.load_image_file ("bogute.jpeg") face_landmarks_list = face_recognition.face_landmarks (image)
Traverse the elements in the list and modify the eyebrows
D.polygon (face_landmarks ['left_eyebrow'], fill= (68,54,39,128)) d.polygon (face_landmarks [' right_eyebrow'], fill= (68,54,39,128)) d.line (face_landmarks ['left_eyebrow'], fill= (68,54,39,150), width=5) d.line (face_landmarks [' right_eyebrow'], fill= (68,54,39150), width=5)
Put lipstick on the face
D.polygon (face_landmarks ['top_lip'], fill= (150,0,0,128) d.polygon (face_landmarks [' bottom_lip'], fill= (150,0,0,128)) d.line (face_landmarks ['top_lip'], fill= (150,0,0,64), width=8) d.line (face_landmarks [' bottom_lip'], fill= (150,0,0,64), width=8)
Increase eyeliner
D.polygon (face_landmarks ['left_eye'], fill= (255,255,255,30)) d.polygon (face_landmarks [' right_eye'], fill= (255,255,255,30)) d.line (face_landmarks ['left_eye'] + [face_landmarks [' left_eye'] [0]], fill= (0,0,0110), width=6) d.line (face_landmarks ['right_eye'] + [face_landmarks [' right_eye'] [0]], fill= (0) 0,0,110), wid=6)
According to the above code, I use the strength is not good, playing dirty "big mouth" Bogart to do the demonstration!
On the left is the original picture, and on the right is the effect after adding makeup.
Thank you for your reading, the above is the content of "how to use Python to achieve face recognition". After the study of this article, I believe you have a deeper understanding of how to use Python to achieve face recognition, 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.