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 Python to realize facial recognition

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Python to achieve facial recognition". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

For the development environment, we will use Visual Studio Community Edition.

If you don't have it installed on your computer, you can download it here. And install desktop development using C++.

Now that we have Visual Studio for desktop development using C++, we can start our project.

Use Visual Studio to open a new directory and create a new python environment. We will use venv. Open your integration terminal and write python-m venv venv. Then activate the environment by typing venv/bin/Activate.ps1. This is for PowerShell.

If you use any other terminal, you can find the complete list here

Now that we have finished creating the virtual environment, let's start extracting our dependencies. To do this, we will need opencv and face_recognition. Use pip in your terminal.

Pip install opencv-python face_recognition

Face Recognition is a library that uses the most advanced dlib libraries. We are ready to write some code and recognize some faces.

To create a new python file, we will call the file missingPerson.py, assuming we will use our application to match missing persons. Import our dependencies and write our first few lines.

Import cv2import numpy as npimport face_recognitionimport os from face_recognition.api import face_distance

Assuming that all our photos are stored in our server storage, we need to first pull the images of all the people into our application and read them.

Path = 'MissingPersons'images = [] missingPersons = [] missingPersonsList = os.listdir (path) for missingPerson in missingPersonsList: curImg = cv2.imread (f' {path} / {missingPerson}') images.append (curImg) missingPersons.append (os.path.splitext (missingPerson) [0]) print (missingPersons)

In this section, we will use opencv to read all the images of missing persons and attach them to our missingPerson list.

After we read all the missing face images from the storage, we need to find the face coding so that we can use the CNN face detector to create a two-dimensional array of face bounding boxes in the image.

Def findEncodings (images): encodeList = [] for img in images: img = cv2.cvtColor (img, cv2.COLOR_BGR2RGB) encode = face_recognition.face_encodings (img) [0] encodeList.append (encode) print (encodeList) return encodeList encodeListKnown = findEncodings (images) print ('Encoding Complete')

We store the two-dimensional array in the known face coding list. This will take a few minutes.

Now that we have the facial codes of all the missing persons, all we have to do is match them to our reporter's image. Face_recognition is very convenient to use.

Def findMissingPerson (encodeListKnown, reportedPerson='found1.jpg'): person = face_recognition.load_image_file (f'ReportedPersons/ {reportedPerson}]') person = cv2.cvtColor (person,cv2.COLOR_BGR2RGB) try: encodePerson = face_recognition.face_encodings (person) [0] comparedFace = face_recognition.compare_faces (encodeListKnown,encodePerson) faceDis = face_recognition.face_distance (encodeListKnown EncodePerson) matchIndex = np.argmin (faceDis) if comparedFace [matchIndex]: name = missingPersons[ matchIndex] .upper () print (name) return name else: print ('Not Found') return False except IndexError as e: print (e) return e

First of all, we need to load the image file of the reported person and encode their faces. The rest is to compare the reported face coding with the known face coding. Then a simple logic matches their index and returns whether the person is found in our missingPersons list.

This kind of face recognition is not only used to find missing persons. It can detect and recognize faces, and can operate as needed.

"how to use Python to achieve facial recognition" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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