前言

最近碰到了照片识别的场景,正好使用了face_recognition项目,给大家分享分享。face_recognition项目能做的很多,人脸检测功能也是有的,是一个比较成熟的项目。该项目的github地址:github仓库

本文主要是对该项目的安装使用,后面会更新一篇我自己写的实现人脸检测的代码,可以直接使用。

环境安装

首先我们看看官方给出的人脸识别效果图

我们看一下README关于安装环境的信息

file

官方给出的可安装操作系统是Mac和Linux,但是我想在windows安装,继续往下看。

file

windows虽然不是官方支持,但是也能装,不就是个dlib吗?好的,那就开始装。

我们直接安装requirements_dev.txt,这里要注意,把pip去掉。

file

注意一点安装dlib的时候会报错,需要先安装cmake,安装命令如下:

pip install cmake -i https://pypi.douban.com/simple

除此之外,项目还需要安装opencv-python,安装命令如下:

pip install opencv-python -i https://pypi.douban.com/simple

代码使用

先做一下说明,在使用face_recognition运行的时候,可以选择安装face_recognition命令进行运行的模式,也可以使用face_recognition模块构建代码运行。为了二次开发,我还是先试试代码的方式,主要试试人脸识别模块。

官方代码如下:

import face_recognition

# Load the jpg files into numpy arrays
biden_image = face_recognition.load_image_file("biden.jpg")
obama_image = face_recognition.load_image_file("obama.jpg")
unknown_image = face_recognition.load_image_file("obama2.jpg")

# Get the face encodings for each face in each image file
# Since there could be more than one face in each image, it returns a list of encodings.
# But since I know each image only has one face, I only care about the first encoding in each image, so I grab index 0.
try:
    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
except IndexError:
    print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")
    quit()

known_faces = [
    biden_face_encoding,
    obama_face_encoding
]

# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("Is the unknown face a picture of Biden? {}".format(results[0]))
print("Is the unknown face a picture of Obama? {}".format(results[1]))
print("Is the unknown face a new person that we've never seen before? {}".format(not True in results))

代码说明:

1、首先可以看到将两个人脸的数据加到了known_faces列表内。

2、然后用未知图数据进行识别判断。

看一下加入到known_faces的照片

看一下需要识别的照片

看一下执行结果

file

我们可以看到在拜登的识别中提示false,在奥巴马识别中提示true。这里要注意一点,我们看一下compare_faces方法参数。

file

参数tolerance最佳为0.6,越低越严格,所以可以按照自己的需求调整。

总结

经过我多次测试,在脸型比较接近的情况下,还是会有误差,需要按照自己的情况进行调整。

分享:生活最沉重的负担不是工作,而是无聊。——《罗曼·罗兰》

如果本文对你有帮助的话,点个赞吧,谢谢!

file

本人CSDN主页地址:剑客阿良_ALiang的主页

一起学习,一起进步。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/jk-aliang/p/15792428.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!