Python安装face_recognition搭建人脸识别系统(超简洁,附使用),windows环境安装face_recognition,windows安装dlib。

在此我使用的是GitHub上开源的一个星数非常高的项目,链接为:链接 

如果读者使用的conda环境直接从TIP处开始观看即可,首选conda环境安装,pip总是会报各种错。

一)windows环境下安装该项目:

1、代开pycharm,找到下面的终端,依次输入如下指令:

在安装dlib时会非常的慢,并且有警告不用管多等一会,博主大概进行了三四分钟才安装完成

pip install cmake
pip install boost
pip install dlib
pip install face_recognition

此时我们所有的准备工作都做完了,导入一下face_recognition包若不报错即安装成功啦;


TIP:如果dlib在pip实在安装不上时请更换为conda环境,亲测秒速安装。

conda install dlib

使用上述命令时可能会包下面的错误:表明下载频道太多了,所以需要你自己选择一下

 执行下面代码:

# 查看
anaconda show conda-forge/dlib
# 配置
conda config --append channels conda-forge
# 安装dlib
conda install dlib

 

以上就安装成功dlib啦。 

之后在进行 face_recognition 的安装:

conda install face_recognition

 

这样就安装成功啦。 

二)开始使用:

        官方使用文档链接:使用文档

1、API解析:

方法说明返回值
load_image_file(图片对象)将图片转化为下面使用的对象对象
face_locations(1对象)查找人脸位置返回人脸所在的区域矩形
face_encodings(1对象)将图片中的人脸转化为特征坐标返回list,将识别到的所有人脸都按坐标返回
compare_faces(encodings数组,待测人脸encodings一个对象)将待测的在总的数组中对比返回一个Boolen类型的数组,判断时用in判断

三)使用代码:

import os # 操作文件
import cv2 # 绘制矩形框
from PIL import Image # 绘制图片
import face_recognition # 人脸识别库

# 下面三个是额外方法,不是库内包括的。
# 将识别到的人脸绘制出来
def print_image(face, image):
    for face_location in face:
        # 坐标的返回顺序是top, right, bottom, left
        top, right, bottom, left = face_location
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()

# 根据坐标在图片中画出框框
def print_image_tru(images, image_list):
    image = cv2.imread(images)
    # (top, right, bottom, left)
    for one in image_list:
        y1 = one[0]
        x1 = one[3]
        y2 = one[2]
        x2 = one[1]
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv2.imshow("fff", image)
    cv2.waitKey()

# 将目录中的图片加载到已知人脸库中
def get_face_data():
    base_path = 'image/face_data'
    name_content = os.listdir(base_path)
    image_encoding_content = []
    for one in name_content:
        img = base_path + "/" + str(one)
        load = face_recognition.load_image_file(img)
        encodings = face_recognition.face_encodings(load)[0]
        image_encoding_content.append(encodings)
    return name_content, image_encoding_content


# 1、查找人脸的个数
def count_face(images):
    image = face_recognition.load_image_file(images)
    face_locations = face_recognition.face_locations(image)
    print(f"该照片中识别出了{len(face_locations)}张人脸")
    print_image_tru(images, face_locations)  # 用矩形框画出人脸


# 2、人脸识别
def face_recognitions(data_base_image, tmp_image):
    # 1、将传来的图片转化为人脸编码:
    picture_of_tmp = face_recognition.load_image_file(tmp_image)
    # 2、识别目标中人脸的编码,由于图中可能没用人脸所以可能会抛出异常
    try:
        # 获取人脸的编码,上面加载的图片会有多个人脸,所以face_recognition.face_encodings返回一个列表
        # 由于示例图片我只选取了有一个人脸的图片,所以直接选取了第一个元素
        tmp_encoding = face_recognition.face_encodings(picture_of_tmp)[0]
    except IndexError:
        print('未识别出人脸')
        return

    # 开始进行人脸比对,compare_faces第一个参数是数据库中的所有已经存在人脸,
    # 返回一个列表,表示与上述数据库中第几个人脸匹配成功
    results = face_recognition.compare_faces(data_base_image[1], tmp_encoding)

    if True in results:
        index = results.index(True)
        names = data_base_image[0][index].split('.')[0]
        print(f"人脸验证成功,身份是{names}")
    else:
        print("验证失败")


tmp_image = r'要测试的人脸图片'
tuple_data = get_face_data()
face_recognitions(tuple_data, tmp_image)

  • 9
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值