Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize face recognition with Python Code

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Python code to achieve face recognition". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Python code to achieve face recognition" can help you solve the problem.

Text:

Environmental requirements:

Ubuntu17.10

Python 2.7.14

Environment building:

1. Install Ubuntu17.10 > installation steps are here

two。 Install Python2.7.14 (Ubuntu17.10 default Python version is 2.7.14)

3. Install git, cmake, python-pip

# install git$ sudo apt-get install-y git# install cmake$ sudo apt-get install-y cmake# install python-pip$ sudo apt-get install-y python-pip

4. Install and compile dlib

Before installing face_recognition, you need to install and compile dlib.

# install boost$ sudo apt-get install libboost-all-dev before compiling dlib # start compiling dlib# clone dlib source code $git clone https://github.com/davisking/dlib.git$ cd dlib$ mkdir build$ cd build$ cmake.. -DDLIB_USE_CUDA=0-DUSE_AVX_INSTRUCTIONS=1$ cmake-- build. (notice a space in the middle) $cd.. $python setup.py install-- yes USE_AVX_INSTRUCTIONS-- no DLIB_USE_CUDA

5. Install face_recognition

# installing face_recognition$ pip install face_recognition#, installing face_recognition will automatically install numpy, scipy, etc.

After setting up the environment, enter the face_recognition command in the terminal to see if it is successful.

Realization of face recognition: example 1 (1 line of code to achieve face recognition)

1. First of all, you need to provide a folder with pictures of all the people you want the system to know. Each of them has a picture, which is named after the person:

There are photos of babe, Jackie Chan and Joey Yung under the known_people folder.

two。 Next, you need to prepare another folder, which contains the pictures you want to identify: under the unknown_pic folder are the pictures to be recognized, of which Han Hong is unknown to the machine.

3. Then you can run the face_recognition command, passing in the two folders you just prepared as parameters, and the command will return who appears in the images you need to identify:

Recognition successful!

Example 2 (identify all the faces in the picture and display them) # filename: find_faces_in_picture.py #-*-coding: utf-8-*-# Import pil module, you can install apt-get install python-Imaging from PIL import Image # import face_recogntion module with command You can install pip install face_recognition import face_recognition # to load the jpg file into the numpy array image = face_recognition.load_image_file ("/ opt/face/unknown_pic/all_star.jpg") # use the default given HOG model to find all the faces in the image # this method is quite accurate, but it is still not as accurate as the CNN model Because GPU acceleration is not used # see also: find_faces_in_picture_cnn.pyface_locations = face_recognition.face_locations (image) # use CNN model # face_locations = face_recognition.face_locations (image, number_of_times_to_upsample=0) Model= "cnn") # print: how many faces have I found in the picture print ("I found {} face (s) in this photograph." .format (len (face_locations) # all faces found in the loop for face_location in face_locations: # print the location information of each face top, right, bottom, left = face_location print ("A face is located at pixel location Top: {}, Left: {} Bottom: {}, Right: {} ".format (top, left, bottom, right) # specify the location information of the face Then display the face picture face_image = image [top:bottom, left:right] pil_image = Image.fromarray (face_image) pil_image.show ()

The picture below is a picture for identification.

# execute the python file $python find_faces_in_picture.py

Seven faces are identified from the picture and displayed, as shown in the following picture

Example 3 (automatically recognize other people's facial features) # filename: find_facial_features_in_picture.py #-*-coding: utf-8-*-# Import pil module. You can install apt-get install python-Imagingfrom PIL import Image and ImageDraw # import face_recogntion module by command. You can install pip install face_recognitionimport face_recognition # to load the jpg file into the numpy array image = face_recognition.load_image_file ("biden.jpg") # find all facial features of all faces in the image face_landmarks_list = face_recognition.face_landmarks (image) print ("I found {} face (s) in this photograph." .format (len (face_landmarks_list) for face_landmarks in face_landmarks_list: # print the position of each facial feature in this image facial_features = ['chin' 'left_eyebrow',' right_eyebrow', 'nose_bridge',' nose_tip', 'left_eye',' right_eye', 'top_lip',' bottom_lip'] for facial_feature in facial_features: print ("The {} in this face has the following points: {}" .format (facial_feature Face_ landmarks [faces _ feature]) # Let's depict the features of everyone's face in the image! Pil_image = Image.fromarray (image) d = ImageDraw.Draw (pil_image) for facial_feature in facial_features: d.line (face_ landmarks [feature _ feature], width=5) pil_image.show ()

Automatically recognize facial features (contours)

Example 4 (identify someone's face and identify the person) # filename: recognize_faces_in_pictures.py #-*-conding: utf-8-*-# Import the face_recogntion module You can install pip install face_recognitionimport face_recognition # to load the jpg file into the numpy array babe_image = face_recognition.load_image_file ("/ opt/face/known_people/babe.jpeg") Rong_zhu_er_image = face_recognition.load_image_file ("/ opt/face/known_people/Rong zhu er.jpg") unknown_image = face_recognition.load_image_file ("/ opt/face/unknown_pic/babe2.jpg") # get the facial coding of each face in each image file # because there may be multiple faces in each image So return a list of codes. # but since I know that there is only one face in each image, I only care about the first encoding in each image, so I take the index 0. Babe_face_encoding = face_recognition.face_encodings (babe_image) [0] Rong_zhu_er_face_encoding = face_recognition.face_encodings (Rong_zhu_er_image) [0] unknown_face_encoding = face_recognition.face_encodings (unknown_image) [0] known_faces = [babe_face_encoding, Rong_zhu_er_face_encoding] # the result is an array of True/false The result of a match for anyone in the unknown face known_faces array results = face_recognition.compare_faces (known_faces, unknown_face_encoding) print ("is this unknown face Babe? {}" .format (results [0])) print ("is this unknown face Joey Yung? {}" .format (results [1]) print ("is this unknown face a new face we have never seen before? {}" .format (not True in results))

The display result is shown in the figure

Example 5 (recognize other people's facial features and beauty) # filename: digital_makeup.py #-*-coding: utf-8-*-# Import pil module, you can install apt-get install python-Imagingfrom PIL import Image and ImageDraw # import face_recogntion module by command You can install pip install face_recognitionimport face_recognition # to load the jpg file into the numpy array image = face_recognition.load_image_file ("biden.jpg") # find all facial features of all faces in the image face_landmarks_list = face_recognition.face_landmarks (image) for face_landmarks in face_landmarks_list: pil_image = Image.fromarray (image) d = ImageDraw.Draw (pil_image 'RGBA') # turned eyebrows into a nightmare 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,39) Width=5) # glossy lips 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) # sparkling eyes d.polygon (face_landmarks ['left_eye'], fill= (255,255,255,30)) d.polygon (face_landmarks [' right_eye'], fill= (255,255,255,30)) # apply some eyeliner 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), width=6) pil_image.show ()

The comparison of beauty before and after beauty is as follows:

This is the end of the content about "how to use Python code to achieve face recognition". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report