windows下使用face_recognition实现人脸识别

环境配置

  • Python 3.6(必须是)
  • Windows10(不是官方支持的,但也能用)

前言:

face_recognition这个项目尤其是dlib更适用于Linux系统。在性能方面,编译同样规格的项目,这个工具在Windows 10 上大约是Ubuntu上的四分之一。但是在这两者之间我没有看到在其他方面有什么差别。

由于windows不是face_recognition官方支持的,需要编译安装dlib,过程过于复杂,本次使用网上编译后的whl文件进行安装,这也是必须本次实验必须是在python3.6下的原因,因为网上只有dlib编译文件whl cp36版的。

一、安装环境

第一步:安装dlib

1、下载dlib-19.7.0-cp36-cp36m-win_amd64.whl 安装包

2、打开cmd切换到dlib-19.7.0-cp36-cp36m-win_amd64.whl 文件所在目录下执行命令安装

注意: 你的电脑上可能已经安装过其他的python版本,切记要切换到python3.6,为了防止已经安装其他版本的python dlib安装包要放到python3.6安装目录下的Scripts文件夹中再执行以下命令。

# pip install dlib-19.7.0-cp36-cp36m-win_amd64.whl

第二步:安装face_recognition库

使用命令:

# pip install face_recognition

二、应用face_recognition库

1、首先我们先从网上下载10张不同人的图片存在images文件夹中,并且每张图片用图片中的人名命名图片。用于与需要对比的图片进行对比。images文件夹内容如下:

 2、另外下载一张图片用于对比,我下载的是马云的另一张图片

3、python代码:

import os
import face_recognition as face


# 列出所有可用的图片,也就是刚才下载的10张图片

images = os.listdir('images')
# 加载需要要对比的图片,也就是下载的另一张马云图片

image_to_be_matched = face.load_image_file('test.jpg')

# 将加载的图像编码为特征向量

image_to_be_matched_encoded = face.face_encodings(image_to_be_matched)[0]
# 遍历images下的每张图像

for image in images:

    # 加载图片

    current_image = face.load_image_file("images/" + image)

    # 将加载的图像编码为特征向量

    current_image_encoded = face.face_encodings(current_image)[0]

    # 将图像与图像匹配,并检查是否匹配

    result = face.compare_faces([image_to_be_matched_encoded], current_image_encoded)

    # 检查是否匹配

    if result[0] == True:
        print("匹配: " + image)

print("end")

运行代码输出以下结果:

很显然结果正确。

文章到此结束。下一篇博文将继续深入研究。

发布了17 篇原创文章 · 获赞 70 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/zzqaaasss/article/details/89021177
今日推荐