Python 简单人脸识别简介

本教程是用 Python 3.6 编写的,但使用的库是 兼容 3.3 及以上版本。 

人脸识别软件很棒。事实上,我们能够写作 准确识别某人眼睛和鼻子所在位置的软件 一张图片仍然让我感到震惊,而且那里有图书馆 这种事情太棒了。这些库有助于降低 适合希望编写自己的人脸识别系统的初学者的入口,以及 允许人们做一些非常酷的事情。

ageitgey/face_recognition 就是其中之一 这样的库,在撰写本文时,它的特点远远超过星星 在 GitHub 上。7,000

建立

为了开始使用该库,您首先将拥有 要安装它,可以使用简单的安装命令来完成,如下所示:face_recognitionpip

pip3 install face_recognition

一个简单的例子

让我们拍摄一张包含多人的库存图像。如果我们想自动查找图像中的所有人脸,我们可以在 4 行代码中轻松做到这一点代码行。此代码将首先接收图像,然后计算使用 的所有人脸的位置。在我们之后将简单地打印出找到的人脸数量。face_recognition.face_locations(image)

import face_recognition

image = face_recognition.load_image_file("My_Image.png")
face_locations = face_recognition.face_locations(image)
print("I found {} face(s) in this photograph.".format(len(face_locations)))

如果我要对上图运行此作,我将得到以下输出:

 $ python3.6 simple.py
I found 5 face(s) in this photograph.

这是人脸检测的一个示例,您可以将其与 类似安全摄像头的东西,并使用它进行实时分析 检测算法,查看是否有人刚刚走进您的房子 例。

识别人脸

一个更复杂的示例是识别每个 找到的人脸并将这些坐标转换为单独的图像。

假设我们想更进一步地使用上面的示例并存储 我们已在新的安全软件中检测到。这可以使用库来完成:face_recognition

from PIL import Image
import face_recognition

## Load the jpg file into a numpy array
image = face_recognition.load_image_file("stock_people.jpg")

## Find all the faces in the image using the default HOG-based model.
## This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
## See also: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:

    ## Print the location of each face in this image
    top, right, bottom, left = face_location
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    ## You can access the actual face itself like this:
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.show()

运行此作将提供以下输出,它将打开 5 个临时图像文件。

 $ python3.6 simple.py
I found 5 face(s) in this photograph.
A face is located at pixel location Top: 72, Left: 394, Bottom: 124, Right: 446
A face is located at pixel location Top: 32, Left: 467, Bottom: 94, Right: 529
A face is located at pixel location Top: 72, Left: 285, Bottom: 124, Right: 337
A face is located at pixel location Top: 72, Left: 170, Bottom: 124, Right: 222
A face is located at pixel location Top: 39, Left: 87, Bottom: 101, Right: 149

很酷,对吧?同样,您可能会在一系列视频中运行此作 流式传输并捕获该视频中出现的所有面孔。

如果您对人脸识别等概念不感兴趣,我们将 在下面介绍,您可能会开始做很酷的事情,例如情绪 分析并尝试衡量人们在您的视频中的快乐或悲伤程度。

如果您想获得有关 景点在博物馆或者游乐园做!

检查图像中是否存在人物

认识到一张图像包含多张脸是很酷的,但我们可以 实际上更进一步,确定图像中存在谁。 例如,假设您有一群人的照片,您可以确定 使用库,无论您的朋友是否存在 那张照片。face_recognitionAlan

但是,为了使其正常工作,您至少需要一个 你试图识别的人。

import face_recognition

## Load in our reference image of Joe Biden
known_image = face_recognition.load_image_file("biden.jpg")
## Load in our image of a group of people
unknown_image = face_recognition.load_image_file("unknown.jpg")

## Create a biden encoding
biden_encoding = face_recognition.face_encodings(known_image)[0]
## create an encoding based off our group photo
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

## Compare the encodings and try to determine if Biden exists within a photo
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
## Print the results
print(results)

大规模人脸识别系统往往会建立庞大的人员数据库 和他们的脸。随着更多人脸被添加到数据库中,检查谁 存在于一张照片中或一系列照片变得更加昂贵。

这意味着我们不能为世界上的每个人拍照并期望我们的 能够实时告诉我们任何给定照片中存在谁的软件。

原文链接: Python 中的人脸识别简介 |TutorialEdge.net

如果想进一步深入了解可以参考这篇文章:

离线识别率高达99%的Python人脸识别系统,开源(附源代码) - 知乎

Python 简单人脸识别简介
YourCompany, Mitchell Admin October 22, 2025
Share this post
Tags
Archive
常用VPN推荐