使用“ face_recognition” API的人脸识别

The world’s simplest facial recognition API for Python and the command line

全球最简单的Python和命令行面部识别API

In this article, we’ll explore the ‘face_recognition’ library and implement a face recognition system.

在本文中,我们将探索“ face_recognition”库并实现人脸识别系统。

大纲: (Outline:)

  1. Step 1: Installation

    步骤1:安装
  1. Step 2: Finding Faces in Image

    第2步:在图像中查找人脸
  2. Step 3: Identifying Facial Features

    第3步:识别面部特征
  3. Step 4: Identifying Known Faces

    步骤4:识别已知面Kong

步骤1:安装: (Step 1: Installation:)

要求 (Requirements)

  • Python 3.3+ or Python 2.7

    Python 3.3+或Python 2.7
  • macOS or Linux (Windows not officially supported, but it will work fine)

    macOS或Linux(不正式支持Windows,但可以正常使用)

For windows, First make sure that you have dlib installed. If you don’t have dlib installed. Follow these steps

对于Windows,首先请确保已 安装 dlib 如果您尚未安装 dlib 跟着这些步骤

  • Clone the code from GitHub:

    从GitHub克隆代码:
git clone https://github.com/davisking/dlib.git
  • Install with Python Extension:

    使用Python Extension安装:
cd ..
python3 setup.py install

After this step, you should be able to run import dlib successfully.

完成此步骤后,您应该能够成功运行import dlib

After successful installation of dlib library, install face_recognition using this pip command

成功安装 dlib 库后, 使用此pip命令 安装 face_recognition

pip3 install face_recognition

Now, you are ready to implement a real-time face recognition system.

现在,您准备实施实时面部识别系统。

第2步:在图像中查找面Kong: (Step 2: Finding Faces in Image:)

we’ll start by importing face_recognition library

我们将从导入face_recognition库开始

import face_recognition

Using the built-in function load_image_file() to load the image. And then using face_locations() which gives an array listing the co-ordinates of each face!

使用内置函数load_image_file()加载图像。 然后使用face_locations()给出一个列出每个面Kong坐标的数组!

image = face_recognition.load_image_file("picture.jpg")
face_locations = face_recognition.face_locations(image)

The output will be something like this!

输出将是这样的!

I found 1 face(s) in this photograph.
A face is located at pixel location Top: 241, Left: 419, Bottom: 562, Right: 740

第3步:识别面部特征: (Step 3: Identifying Facial Features:)

The face_landmarks() function locates all the landmarks in a face and returns a Dictionary containing the landmarks.

face_landmarks()函数可定位人脸中的所有地标,并返回包含地标的Dictionary。

# Find all facial features in all the faces in a image face_landmarks_list = face_recognition.face_landmarks(image)print(face_landmarks_list)

And the output will be something like this!

输出将是这样!

[{'chin': [(429, 328), (426, 368), (424, 408), (425, 447), (437, 484), (460, 515), (490, 538), (524, 556), (562, 564), (600, 566), (630, 554), (655, 533), (672, 507), (684, 476), (694, 445), (702, 413), (707, 382)], 
'left_eyebrow': [(488, 294), (509, 279), (535, 278), (561, 283), (584, 296)],
'right_eyebrow': [(622, 307), (646, 305), (670, 309), (691, 321), (698, 344)],
'nose_bridge': [(601, 328), (599, 352), (598, 375), (596, 400)], 'nose_tip': [(555, 414), (570, 421), (586, 428), (601, 428), (614, 426)],
'left_eye': [(512, 320), (528, 316), (544, 319), (557, 331), (541, 330), (525, 327)],
'right_eye': [(629, 348), (647, 342), (661, 346), (672, 357), (659, 358), (644, 354)],
'top_lip': [(519, 459), (545, 455), (566, 456), (580, 462), (595, 462), (610, 470), (627, 480), (620, 477), (593, 470), (579, 468), (564, 463), (527, 459)],
'bottom_lip': [(627, 480), (606, 482), (589, 479), (575, 477), (560, 473), (540, 468), (519, 459), (527, 459), (563, 461), (577, 466), (592, 468), (620, 477)]}]

步骤4:识别已知面Kong: (Step 4: Identifying Known Faces:)

First, we need pictures of people we want to recognize.

首先,我们需要要认识的人的照片。

  • Make Sure there is one person in each photo

    确保每张照片中只有一个人
Image for post
Image for post
List of Known People
知名人士名单

Now, we’ll use face_encodings() function which returns a list of results of identified faces. Note that we will use [0] to grab the first person’s Face Encoding’s.

现在,我们将使用face_encodings()函数,该函数返回已识别面Kong的结果列表。 请注意,我们将使用[0]来获取第一人称的人脸编码。

kohli = face_recognition.load_image_file("Virat Kohli.jpg")
kohli_face_encoding = face_recognition.face_encodings(kohli)[0]ronaldo = face_recognition.load_image_file("Ronaldo.jpg")
ronaldo_face_encoding= face_recognition.face_encodings(ronaldo)[0]

Now, Create arrays of known face encodings and their names

现在,创建已知面部编码及其名称的数组

known_face_encodings = [kohli_face_encoding, ronaldo_face_encoding]known_face_names = ["Virat Kohli" , "Ronaldo"]

Now, To recognize a face from the Unknown Image. We should load an unknown image and Find face locations and face encodings of the unknown image.

现在,要从未知图像中识别出一张脸。 我们应该加载未知图像并查找未知图像的脸部位置和脸部编码。

unknown_image = face_recognition.load_image_file("Unknown.jpg")face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

Converting the image into a PIL-format image. So that we can draw bounding boxes on the image.

将图像转换为PIL格式的图像。 这样我们就可以在图像上绘制边框。

from PIL import Image, ImageDraw
pil_image = Image.fromarray(unknown_image)# Create a Pillow ImageDraw Draw instance to draw
draw = ImageDraw.Draw(pil_image)

Now, Loop through each face in the Image and draw bounding Boxes:

现在,循环遍历图像中的每个面并绘制边界框:

for (t, r, b, l), in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)# Instead, use face_distance to calculate similarities
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) best_match_index = np.argmin(face_distances)
name = "Unknown"
if matches[best_match_index]:
name = known_face_names[best_match_index]# Draw a box around the face using the Pillow module
draw.rectangle(((l, t), (r, b)), outline=(0, 0, 255))# Draw a label with a name below the face
width, height = draw.textsize(name)
draw.rectangle(((l, b - height - 10), (r, b)), fill=(0, 0, 255), outline=(0, 0, 255))
draw.text((l + 6, b - height - 5), name, fill=(255, 255, 255, 255))display(pil_image)
Image for post
Image for post
Identified and Unidentified Faces
身份和身份不明的面Kong

That’s it!!!

而已!!!

I hope this gives you a more visual, hands on, insight into how to use face_recognition API.

我希望这可以使您对如何使用face_recognition API有更直观的了解。

参考: (Reference:)

翻译自: https://medium.com/swlh/face-recognition-using-face-recognition-api-e7fa4dcabbc3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: face_recognition库是一个基于Python的人脸识别库,它可以识别人脸并将其与已知的人脸进行匹配。它的优势在于它使用了深度学习技术,可以在大规模数据集上进行训练,从而提高了识别的准确性和速度。此外,它还支持多种图像格式和多种编程语言,使得它可以在不同的平台上使用。 ### 回答2: face_recognition库是一个用于人脸识别的Python库,具有以下优势: 1. 简单易用:face_recognition库提供了简洁明了的API,使人脸识别变得简单易用。通过几行代码,可以实现人脸的定位、特征提取和对比等操作。 2. 高效精确:face_recognition库采用了基于深度学习的算法,能够在大规模的人脸数据集上进行高效的人脸识别。它使用人脸关键点(landmarks)和特征描述向量(face embeddings)进行人脸比对,能够准确识别人脸,并具有很强的辨别能力。 3. 跨平台兼容:face_recognition库可以在多个操作系统上运行,包括Windows、Linux和macOS等。无论是在本地开发还是在云端部署,都可以很方便地使用该库进行人脸识别。 4. 开源自由:face_recognition库是开源的,可以免费使用和修改。这意味着用户可以根据自己的需求进行二次开发和定制,同时也能够从庞大的开发者社区中获得支持和帮助。 总之,face_recognition库具有简单易用、高效精确、跨平台兼容和开源自由等优势。它是一个强大的人脸识别工具,可以广泛应用于人脸识别、人脸验证、人脸检测等领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值