Python利用Dlib实现人脸检测和识别

引言
利用 Python 开发,借助 Dlib 库捕获摄像头中的人脸,提取人脸特征,通过计算特征值之间的欧氏距离,来和预存的人脸特征进行对比,判断是否匹配,达到人脸识别的目的;
可以从摄像头中抠取人脸图片存储到本地,然后提取构建预设人脸特征;根据抠取的已有的同一个人多张人脸图片提取 128D 特征值,然后计算该人的 128D 特征均值;
然后和摄像头中实时获取到的人脸提取出的特征值,计算欧氏距离,判定是否为同一张人脸;

系统架构
Windows7: Python3.6 + OpenCv + Dlib ;
1、Dlib的安装(此处是难点,不需安装VS2015)
安装环境:
win7
python3.6 (必须3.6)
第一步:dlib安装:
通过链接:https://pypi.org/simple/dlib/ 下载“dlib-19.7.0-cp36-cp36m-win_amd64.whl“安装包
使用命令:pip install dlib-19.7.0-cp36-cp36m-win_amd64.whl.whl
第二步:face_recognition安装
使用命令:pip install face_recognition
第三步:下载dlib的dat文件
下载地址 http://dlib.net/files/

Python利用Dlib实现人脸检测和识别_第1张图片
2、其他需要pip安装的python模块如下:

Python利用Dlib实现人脸检测和识别_第2张图片
主要python代码
1、get_faces_from_camera.py / 人脸注册录入
代码如下:

#coding=utf-8

# Updated at 2019-06-04

import dlib         # 人脸处理的库 Dlib
import numpy as np  # 数据处理的库 Numpy
import cv2          # 图像处理的库 OpenCv

import os           # 读写文件
import shutil       # 读写文件

# Dlib 正向人脸检测器 / frontal face detector
detector = dlib.get_frontal_face_detector()

# Dlib 68 点特征预测器 / 68 points features predictor
predictor = dlib.shape_predictor('E://DY/Face_AI/data/dlib/shape_predictor_68_face_landmarks.dat')

# OpenCv 调用摄像头 use camera
cap = cv2.VideoCapture(0)

# 设置视频参数 set camera
cap.set(3, 480)

# 人脸截图的计数器 the counter for screen shoot
cnt_ss = 0

# 存储人脸的文件夹 the folder to save faces
current_face_dir = ""

# 保存 faces images 的路径 the directory to save images of faces
path_photos_from_camera = "E://DY/Face_AI/data/data_faces_from_camera/"


# 新建保存人脸图像文件和数据CSV文件夹
# mkdir for saving photos and csv
def pre_work_mkdir():

    # 新建文件夹 / make folders to save faces images and csv
    if os.path.isdir(path_photos_from_camera):
        pass
    else:
        os.mkdir(path_photos_from_camera)


pre_work_mkdir()


##### optional/可选, 默认关闭 #####
# 删除之前存的人脸数据文件夹
# delete the old data of faces
def pre_work_del_old_face_folders():
    # 删除之前存的人脸数据文件夹
    # 删除 "/data_faces_from_camera/person_x/"...
    folders_rd = os.listdir(path_photos_from_camera)
    for i in range(len(folders_rd)):
        shutil.rmtree(path_photos_from_camera+folders_rd[i])

    if os.path.isfile("data/features_all.csv"):
        os.remove("data/features_all.csv")

# 这里在每次程序录入之前, 删掉之前存的人脸数据
# 如果这里打开,每次进行人脸录入的时候都会删掉之前的人脸图像文件夹 person_1/,person_2/,person_3/...
# If enable this function, it will delete all the old data in dir person_1/,person_2/,/person_3/...
# pre_work_del_old_face_folders()
##################################


# 如果有之前录入的人脸 / if the old folders exists
# 在之前 person_x 的序号按照 person_x+1 开始录入 / start from person_x+1
if os.listdir("E://DY/Face_AI/data/data_faces_from_camera/"):
    # 获取已录入的最后一个人脸序号 / get the num of latest person
    person_list = os.listdir("E://DY/Face_AI/data/data_faces_from_camera/")
    person_num_list = []
    for person in person_list:
        person_num_list.append(int(person.split('_')[-1]))
    person_cnt = max(person_num_list)

# 如果第一次存储或者没有之前录入的人脸, 按照 person_1 开始录入
# start from person_1
else:
    person_cnt = 0

# 之后用来控制是否保存图像的 flag / the flag to control if save
save_flag = 1

# 之后用来检查是否先按 'n' 再按 's' / the flag to check if press 'n' before 's'
press_n_flag = 0

while cap.isOpened():
    flag, img_rd = cap.read()
    # print(img_rd.shape)
    # It should be 480 height * 640 width

    kk = cv2.waitKey(1)

    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)
    
    # 人脸数 faces
    faces = detector(img_gray, 0)

    # 待会要写的字体 / font to write
    font = cv2.FONT_HERSHEY_COMPLEX

    # 按下 'n' 新建存储人脸的文件夹 / press 'n' to create the folders for saving faces
    if kk == ord('n'):
        person_cnt += 1
        current_face_dir = path_photos_from_camera + "person_" + str(person_cnt)
        os.makedirs(current_face_dir)
        print('\n')
        print("新建的人脸文件夹 / Create folders: ", current_face_dir)

        cnt_ss = 0              # 将人脸计数器清零 / clear the cnt of faces
        press_n_flag = 1        # 已经按下 'n' / have pressed 'n'

    # 检测到人脸 / if face detected
    if len(faces) != 0:
        # 矩形框 / show the rectangle box
        for k, d in enumerate(faces):
            # 计算矩形大小
            # we need to compute the width and height of the box
            # (x,y), (宽度width, 高度height)
            pos_start = tuple([d.left(), d.top()])
            pos_end = tuple([d.right(), d.bottom()])

            # 计算矩形框大小 / compute the size of rectangle box
            height = (d.bottom() - d.top())
            width = (d.right() - d.left())

            hh = int(height/2)
            ww = int(width/2)

            # 设置颜色 / the color of rectangle of faces detected
            color_rectangle = (255, 255, 255)

            # 判断人脸矩形框是否超出 480x640
            if (d.right()+ww) > 640 or (d.bottom()+hh > 480) or (d.left()-ww < 0) or (d.top()-hh < 0):
                cv2.putText(img_rd, "OUT OF RANGE", (20, 300), font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)
                color_rectangle = (0, 0, 255)
                save_flag = 0
                if kk == ord('s'):
                    print("请调整位置 / Please adjust your position")
            else:
                color_rectangle = (255, 255, 255)
                save_flag = 1

            cv2.rectangle(img_rd,
                          tuple([d.left() - ww, d.top() - hh]),
                          tuple([d.right() + ww, d.bottom() + hh]),
                          color_rectangle, 2)

            # 根据人脸大小生成空的图像 / create blank image according to the size of face detected
            im_blank = np.zeros((int(height*2), width*2, 3), np.uint8)

            if save_flag:
                # 按下 's' 保存摄像头中的人脸到本地 / press 's' to save faces into local images
                if kk == ord('s'):
                    # 检查有没有先按'n'新建文件夹 / check if you have pressed 'n'
                    if press_n_flag:
                        cnt_ss += 1
                        for ii in range(height*2):
                            for jj in range(width*2):
                                im_blank[ii][jj] = img_rd[d.top()-hh + ii][d.left()-ww + jj]
                        cv2.imwrite(current_face_dir + "/img_face_" + str(cnt_ss) + ".jpg", im_blank)
                        print("写入本地 / Save into:", str(current_face_dir) + "/img_face_" + str(cnt_ss) + ".jpg")
                    else:
                        print("请在按 'S' 之前先按 'N' 来建文件夹 / Please press 'N' before 'S'")

    # 显示人脸数 / show the numbers of faces detected
    cv2.putText(img_rd, "Faces: " + str(len(faces)), (20, 100), font, 0.8, (0, 255, 0), 1, cv2.LINE_AA)

    # 添加说明 / add some statements
    cv2.putText(img_rd, "Face Register", (20, 40), font, 1, (0, 0, 0), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "N: New face folder", (20, 350), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "S: Save current face", (20, 400), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "Q: Quit", (20, 450), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)

    # 按下 'q' 键退出 / press 'q' to exit
    if kk == ord('q'):
        break

    # 如果需要摄像头窗口大小可调 / uncomment this line if you want the camera window is resizeable
    # cv2.namedWindow("camera", 0)

    cv2.imshow("camera", img_rd)

# 释放摄像头 / release camera
cap.release()

cv2.destroyAllWindows()


运行效果如下:
Python利用Dlib实现人脸检测和识别_第3张图片

2、features_extraction_to_csv.py 将图像文件中人脸数据提取出来存入CSV

这部分代码实现的功能是将之前捕获到的人脸图像文件,提取出128D特征,然后计算出某人人脸数据的特征均值存入 CSV 中,方便之后识别时候进行比对。

代码如下:

#coding=utf-8
import cv2
import os
import dlib
from skimage import io  #pip安装scipy
import csv
import numpy as np

path_images_from_camera = "E://DY/Face_AI/data/data_faces_from_camera/"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("E://DY/Face_AI/data/dlib/shape_predictor_5_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1("E://DY/Face_AI/data/dlib/dlib_face_recognition_resnet_model_v1.dat")
def return_128d_features(path_img):
    img_rd = io.imread(path_img)
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
    faces = detector(img_gray, 1)

    print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')

    # 因为有可能截下来的人脸再去检测,检测不出来人脸了
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征
    if len(faces) != 0:
        shape = predictor(img_gray, faces[0])
        face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)
    else:
        face_descriptor = 0
        print("no face")

    return face_descriptor

def return_features_mean_personX(path_faces_personX):
    features_list_personX = []
    photos_list = os.listdir(path_faces_personX)
    if photos_list:
        for i in range(len(photos_list)):
            # 调用return_128d_features()得到128d特征
            print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))
            features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])
            #  print(features_128d)
            # 遇到没有检测出人脸的图片跳过
            if features_128d == 0:
                i += 1
            else:
                features_list_personX.append(features_128d)
    else:
        print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/', '\n')

    # 计算 128D 特征的均值
    # personX 的 N 张图像 x 128D -> 1 x 128D
    if features_list_personX:
        features_mean_personX = np.array(features_list_personX).mean(axis=0)
    else:
        features_mean_personX = '0'

    return features_mean_personX

person_list = os.listdir("E://DY/Face_AI/data/data_faces_from_camera/")
person_num_list = []
for person in person_list:
    person_num_list.append(int(person.split('_')[-1]))
person_cnt = max(person_num_list)
with open("E://DY/Face_AI/data/features_all.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    for person in range(person_cnt):
        # Get the mean/average features of face/personX, it will be a list with a length of 128D
        print(path_images_from_camera + "person_"+str(person+1))
        features_mean_personX = return_features_mean_personX(path_images_from_camera + "person_"+str(person+1))
        writer.writerow(features_mean_personX)
        print("特征均值 / The mean of features:", list(features_mean_personX))
        print('\n')
    print("所有录入人脸数据存入 / Save all the features of faces registered into: data/features_all.csv")

运行结果如下:

Python利用Dlib实现人脸检测和识别_第4张图片
3、face_reco_from_camera.py 实时人脸识别对比分析

功能:调用摄像头,捕获摄像头中的人脸,如果检测到人脸,将摄像头中的人脸提取出128D 的特征,然后和之前录入人脸的128D 特征进行计算欧式距离,如果小于设定值(本文设定为0.4),可以判定为一个人,否则不是。
代码如下:

#coding=utf-8
import dlib          # 人脸处理的库 Dlib
import numpy as np   # 数据处理的库 numpy
import cv2           # 图像处理的库 OpenCv
import pandas as pd  # 数据处理的库 Pandas


facerec = dlib.face_recognition_model_v1("E://DY/Face_AI/data/dlib/dlib_face_recognition_resnet_model_v1.dat")

def return_euclidean_distance(feature_1, feature_2):
    feature_1 = np.array(feature_1)
    feature_2 = np.array(feature_2)
    dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))
    return dist

path_features_known_csv = "E://DY/Face_AI/data/features_all.csv"
csv_rd = pd.read_csv(path_features_known_csv, header=None, error_bad_lines=False)
features_known_arr = []

for i in range(csv_rd.shape[0]):
    features_someone_arr = []
    for j in range(0, len(csv_rd.ix[i, :])):
        features_someone_arr.append(csv_rd.ix[i, :][j])
    features_known_arr.append(features_someone_arr)
print("Faces in Database:", len(features_known_arr))

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('E://DY/Face_AI/data/dlib/shape_predictor_68_face_landmarks.dat')

cap = cv2.VideoCapture(0)


cap.set(3, 480)


while cap.isOpened():
    flag, img_rd = cap.read()
    kk = cv2.waitKey(1)
    # 取灰度
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)
    # 人脸数 faces
    faces = detector(img_gray, 0)

    # 待会要写的字体 font to write later
    font = cv2.FONT_HERSHEY_COMPLEX

    # 存储当前摄像头中捕获到的所有人脸的坐标/名字
    # the list to save the positions and names of current faces captured
    pos_namelist = []
    name_namelist = []

    # 按下 q 键退出
    # press 'q' to exit
    if kk == ord('q'):
        break
    else:
        # 检测到人脸 when face detected
        if len(faces) != 0:
            # 获取当前捕获到的图像的所有人脸的特征,存储到 features_cap_arr
            # get the features captured and save into features_cap_arr
            features_cap_arr = []
            for i in range(len(faces)):
                shape = predictor(img_rd, faces[i])
 
             features_cap_arr.append(facerec.compute_face_descriptor(img_rd, shape))

            # 遍历捕获到的图像中所有的人脸
            # traversal all the faces in the database
            for k in range(len(faces)):
                print("##### camera person", k+1, "#####")
                # 让人名跟随在矩形框的下方
                # 确定人名的位置坐标
                # 先默认所有人不认识,是 unknown
                # set the default names of faces with "unknown"
                name_namelist.append("unknown")

                # 每个捕获人脸的名字坐标 the positions of faces captured
                pos_namelist.append(tuple([faces[k].left(), int(faces[k].bottom() + (faces[k].bottom() - faces[k].top())/4)]))

                # 对于某张人脸,遍历所有存储的人脸特征
                # for every faces detected, compare the faces in the database
                e_distance_list = []
                for i in range(len(features_known_arr)):
                    # 如果 person_X 数据不为空
                    if str(features_known_arr[i][0]) != '0.0':
                        print("with person", str(i + 1), "the e distance: ", end='')
                        e_distance_tmp = return_euclidean_distance(features_cap_arr[k], features_known_arr[i])
                        print(e_distance_tmp)
                        e_distance_list.append(e_distance_tmp)
                    else:
                        # 空数据 person_X
                        e_distance_list.append(999999999)
                # Find the one with minimum e distance
                similar_person_num = e_distance_list.index(min(e_distance_list))
                print("Minimum e distance with person", int(similar_person_num)+1)

                if min(e_distance_list) < 0.4:
                    # 在这里修改 person_1, person_2 ... 的名字
                    # 可以在这里改称 Jack, Tom and others
                    # Here you can modify the names shown on the camera
                    name_namelist[k] = "Person "+str(int(similar_person_num)+1)
                    print("May be person "+str(int(similar_person_num)+1))
                else:
                    print("Unknown person")

                # 矩形框
                # draw rectangle
                for kk, d in enumerate(faces):
                    # 绘制矩形框
                    cv2.rectangle(img_rd, tuple([d.left(), d.top()]), tuple([d.right(), d.bottom()]), (0, 255, 255), 2)
                print('\n')

            # 在人脸框下面写人脸名字
            # write names under rectangle
            for i in range(len(faces)):
                cv2.putText(img_rd, name_namelist[i], pos_namelist[i], font, 0.8, (0, 255, 255), 1, cv2.LINE_AA)

    print("Faces in camera now:", name_namelist, "\n")

    cv2.putText(img_rd, "Press 'q': Quit", (20, 450), font, 0.8, (84, 255, 159), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "Face Recognition", (20, 40), font, 1, (0, 0, 0), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "Faces: " + str(len(faces)), (20, 100), font, 1, (0, 0, 255), 1, cv2.LINE_AA)

    # 窗口显示 show with opencv
    cv2.imshow("camera", img_rd)


cap.release()

cv2.destroyAllWindows()

运行log如下:

##### camera person 1 #####
with person 1 the e distance: 0.3633946712891996
Minimum e distance with person 1
May be person 1

##### camera person 1 ###
with person 1 the e distance: 0.26431970935682836
Minimum e distance with person 1
May be person 1

Faces in camera now: ['Person 1'] 
Faces in camera now: [] 
Faces in camera now: [] 


参考资料:
GitHub : https://github.com/coneypo/Dlib_face_recognition_from_camera

你可能感兴趣的:(python+人工智能)