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 use face_recognition to realize AI recognition of people in pictures by Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how Python uses face_recognition to identify people in pictures with AI. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Environmental installation

First of all, let's take a look at the official image of face recognition.

Let's take a look at README's information about the installation environment.

The official installable operating systems are Mac and Linux, but I want to install them in windows and move on.

Although windows is not officially supported, it can also be installed. Isn't it just a dlib? Okay, let's start pretending.

Let's install requirements_dev.txt directly, and note here that pip is removed.

Note that there will be an error when installing dlib. You need to install cmake first. The installation command is as follows:

Pip install cmake-I https://pypi.douban.com/simple

In addition, the project also needs to install opencv-python, with the following installation commands:

Pip install opencv-python-I https://pypi.douban.com/simple code uses the

To start with, when running with face_recognition, you can choose the mode in which the face_recognition command is installed to run, or you can use the face_recognition module to build the code to run. For the second development, I would like to try the code first, mainly try the face recognition module.

The official code is as follows:

Import face_recognition# Load the jpg files into numpy arraysbiden_image = face_recognition.load_image_file ("biden.jpg") obama_image = face_recognition.load_image_file ("obama.jpg") unknown_image = face_recognition.load_image_file ("obama2.jpg") # Get the face encodings for each face in each image file# Since there could be more than one face in each image, it returns a list of encodings.# But since I know each image only has one face, I only care about the first encoding in each image So I grab index 0.try: biden_face_encoding = face_recognition.face_encodings (biden_image) [0] obama_face_encoding = face_recognition.face_encodings (obama_image) [0] unknown_face_encoding = face_recognition.face_encodings (unknown_image) [0] except IndexError: print ("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting... ") Quit () known_faces = [biden_face_encoding, obama_face_encoding] # results is an array of True/False telling if the unknown face matched anyone in the known_faces arrayresults = face_recognition.compare_faces (known_faces Unknown_face_encoding) print ("Is the unknown face a picture of Biden? {}" .format (results [0]) print ("Is the unknown face a picture of Obama? {}" .format (results [1]) print ("Is the unknown face a new person that we've never seen before? {}" .format (not True in results))

Code description:

1. First of all, you can see that the data of two faces are added to the known_faces list.

2. Then use the unknown graph data to identify and judge.

Take a look at the photos added to known_faces.

Take a look at the photos that need to be identified

Take a look at the implementation result

We can see prompting false in Biden's recognition and true in Obama's recognition. To note here, let's take a look at the parameters of the compare_faces method.

The best parameter tolerance is 0.6. the lower the parameter is, the stricter it is, so you can adjust it according to your own needs.

Thank you for reading! On "Python how to use face_recognition to identify people in pictures" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

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