Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to recognize people in Image

Shulou Source: shulou.com Published: 2022-06-02 02:39:48 09月10日 Update

Editor to share with you how to use Python to identify people in the image, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Environmental deployment

Just follow the installation and deployment in the previous article.

Code

No nonsense, just go to the code.

#! / user/bin/env python# coding=utf-8 "" @ project: face_recognition@author: swordsman A Liang _ ALiang@file: test.py@ide: PyCharm@time: 2022-01-11 19:50:58 "import face_recognitionknown_faces = [[], []] def add_person (image_path: str Name: str): image = face_recognition.load_image_file (image_path) try: encoding = face_recognition.face_encodings (image) [0] known_faces [0] .append (name) known_faces [1] .append (encoding) except IndexError: print ("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting... ") def compare (new_image: str): new1 = face_recognition.load_image_file (new_image) unknown_face_encoding = face_recognition.face_encodings (new1) [0] results = face_recognition.compare_faces (known_faces [1], unknown_face_encoding,0.5) print (known_faces [0]) print (results) name =''for i in range (0) Len (known_faces [0]): if results [I]: print (I) name = known_faces [0] [I] break if name =': return'I do not who' else: return nameif _ name__ = ='_ main__': add_person ('data/1.jpg',' Yang Mi') add_person ('data/2.jpg'') Add_person ('data/3.jpg',' Song Yi') add_person ('data/4.jpg',' Deng Ziqi') print (compare ('data/121.jpg')) print (compare (' data/123.jpg'))

Code description:

1. First record some faces and specify the names of the characters. The method is add_person.

2. The compare method is used to determine who the photo is.

Take a look at the picture I prepared first.

Take a look at the photos that need to be verified.

Execution result

It can be seen that Yang Mi and Deng Ziqi have been identified.

The above is all the contents of the article "how to use Python to identify people in an image". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: People codes photos articles images content methods not so much faces swordsmen names most nonsense more environment knowledge results industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Huawei Shulou Technology Microsoft