face_recognition人脸识别

简介

face_recognition项目是世界上最简洁的人脸识别库,可以使用Python和命令行工具提取、识别、操作人脸。 本项目的人脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。 Labeled Faces in the Wild是美国麻省大学安姆斯特分校(University of Massachusetts Amherst)制作的人脸数据集,该数据集包含了从网络收集的13,000多张面部图像。 本项目提供了简易的face_recognition命令行工具,你可以用它处理整个文件夹里的图片。

搭建环境

  • 我的系统是window10, 基于anconda的python3.6

安装: pip install face_recognition

注: 安装时可能会提示缺失依赖包dlib,而dlib依赖Boost和cmake, 依次安装 pip install CMake pip install dlib pip install face_recognition


也可以通过直接下载安装包的方式

安装dlib:

下载地址:

https://files.pythonhosted.org/packages/0e/ce/f8a3cff33ac03a8219768f0694c5d703c8e037e6aba2e865f9bae22ed63c/dlib-19.8.1-cp36-cp36m-win_amd64.whl#md5=9a704406fbb4036f70b5f174fec9db1f

pip install dlib-19.8.1-cp36-cp36m-win_amd64.whl

验证是否安装成功: pip show dlib

安装face_recognition_models

下载地址:

https://files.pythonhosted.org/packages/cf/3b/4fd8c534f6c0d1b80ce0973d01331525538045084c73c153ee6df20224cf/face_recognition_models-0.3.0.tar.gz#md5=53f60e9165958b2bb8841d66dde3f02d

解压压缩包,进入face_recognition_models-0.3.0目录

python setup.py install

pip show face_recognition_models

验证是否安装成功: pip show face_recognition_models

安装face_recognition

下载地址:

https://files.pythonhosted.org/packages/03/f9/c025d14d73dc7811ae9f25fa0bfd03323bc6a4023a433f829ce03f1a74d2/face_recognition-1.0.0.tar.gz#md5=c2e39015df7d1dfb46592c9be0fea83e

解压压缩包,进入face_recognition目录

python setup.py install

验证是否安装成功: pip show face_recognition


基本使用

import face_recognition
import cv2

img_path = r"F:\work\my_lesson\python\face_mod\img\3.jpg"
image = face_recognition.load_image_file(img_path)
face_locations = face_recognition.face_locations(image)
print(face_locations)
img = cv2.imread(img_path)
for face in face_locations:
    top, right, bottom, left = face
    cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('img', img)
cv2.waitKey(0)

首先face_recognition.load_image_file(img_path)读取图片

face_recognition.face_locations(image)获取脸部的坐标

此时打印face_locations获得: [(553, 660, 627, 585)] 坐标信息, 以此对应的是上右下左的脸部坐标点

然后cv2.imread(img_path)使用cv2再次读取图片

遍历坐标点利用cv2来画线框

cv2.imshow('img', img)在窗口展示图片

cv2.waitKey(0)为0是默认等待关闭

  • 来看看测试图片:

  • 当然可以看看多人识别会怎么样:

  • 显然还是可以识别成功呢, 但是如果是更多的人呢?

从这两组图片对比看出, 只要不是被挡着的完整的脸都是可以正常识别的


面部特征

from PIL import Image, ImageDraw
import face_recognition


img_path = r"F:\work\my_lesson\python\face_mod\img\4.jpg"
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file(img_path)

#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)

print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
    #打印此图像中每个面部特征的位置
    facial_features = [
        'chin',
        'left_eyebrow',
        'right_eyebrow',
        'nose_bridge',
        'nose_tip',
        'left_eye',
        'right_eye',
        'top_lip',
        'bottom_lip'
    ]
    for facial_feature in facial_features:
        print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
    #在图像中画出每个人脸特征!
    for facial_feature in facial_features:
        d.line(face_landmarks[facial_feature], width=5)

pil_image.show()
"""
The chin in this face has the following points: [(303, 200), (305, 220), (308, 240), (312, 259), (318, 277), (328, 293), (342, 305), (360, 313), (379, 316), (398, 314), (414, 306), (427, 294), (436, 278), (441, 260), (446, 242), (451, 223), (453, 203)]
The left_eyebrow in this face has the following points: [(317, 172), (327, 162), (340, 158), (355, 159), (368, 165)]
The right_eyebrow in this face has the following points: [(393, 166), (407, 161), (422, 161), (436, 166), (445, 178)]
The nose_bridge in this face has the following points: [(380, 186), (379, 198), (379, 210), (378, 222)]
The nose_tip in this face has the following points: [(366, 235), (372, 237), (378, 238), (385, 237), (391, 236)]
The left_eye in this face has the following points: [(333, 190), (341, 184), (352, 184), (361, 193), (351, 195), (340, 195)]
The right_eye in this face has the following points: [(401, 194), (411, 186), (422, 186), (430, 193), (422, 197), (411, 196)]
The top_lip in this face has the following points: [(353, 267), (361, 255), (371, 250), (378, 252), (385, 250), (395, 257), (403, 269), (398, 268), (385, 260), (378, 259), (371, 259), (358, 267)]
The bottom_lip in this face has the following points: [(403, 269), (395, 277), (386, 281), (378, 281), (370, 280), (361, 276), (353, 267), (358, 267), (371, 269), (378, 270), (385, 270), (398, 268)]
"""


结语

实际上几行代码就可以完成快速的人脸识别功能, 当然功能还不止, 如用face_recognition.face_encodings可以获取多个不同人的人脸编码来做对比是否同一个人, 但是这个功能的识别率略低, 毕竟裸库, 需要做机器学习来提高识别率, 除此之外还可以调用摄像头来动态识别镜头的人物, 但是无奈我没摄像头就没法好好测试了, 这个功能还是很好玩的.


如果本文对你有启发,或者对本文有疑问或者功能/方法建议,可以在下方做出评论,或者直接联系我,谢谢您的观看和支持!

添加新评论

本站现已启用评论投票,被点踩过多的评论将自动折叠。与本文无关评论请发留言板。请不要水评论,谢谢。

已有 0条评论