Ubuntu+dlib+opencv摄像头实时人脸识别(含训练人脸库)

Face_Recognition_dlib

环境

  1. Ubuntu 16.04
  2. opencv 3.0 for python3.6 pip install opencv-python
  3. dlib 19.16

模型下载

人脸关键点检测器 predictor_path="shape_predictor_68_face_landmarks.dat
人脸识别模型 face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat
含人脸库candidate-face中人脸不同表情的测试数据集 test-face.zip 解压后与上述文件均置于根目录下
下载地址 : 百度云盘 https://pan.baidu.com/s/1h01sfvf5KWU6_7c2-i5HTQ

运行

运行python candidate_train.py 获得人脸库特征信息,存储在candidates.npycandidates.txt 中 。

candidate_train.py文件:

# -*- coding: UTF-8 -*-

import os,dlib,numpy
import cv2

# 1.人脸关键点检测器
predictor_path = "shape_predictor_68_face_landmarks.dat"

# 2.人脸识别模型
face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"

# 3.候选人脸文件夹
faces_folder_path = "candidate-face"

# 4.需识别的人脸
img_path = "test-face/0001_IR_allleft.jpg"

# 5.识别结果存放文件夹
faceRect_path = "faceRec"


# 1.加载正脸检测器
detector = dlib.get_frontal_face_detector()

# 2.加载人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)

# 3. 加载人脸识别模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)



# 候选人脸描述子list

candidates = []

filelist = os.listdir(faces_folder_path)
count = 0
for fn in filelist:
        count = count+1
descriptors = numpy.zeros(shape=(count,128))
n = 0
for file in filelist:
    f = os.path.join(faces_folder_path,file)
    #if os.path.splitext(file)[1] == ".jpg" #文件扩展名
    print("Processing file: {}".format(f))
    img = cv2.imread(f)
    # 1.人脸检测
    dets = detector(img, 1)

    for k, d in enumerate(dets):
        # 2.关键点检测
        shape = sp(img, d)

        # 3.描述子提取,128D向量
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        # 转换为numpy array
        v = numpy.array(face_descriptor)

        descriptors[n] = v

        # descriptors.append(v)
        candidates.append(os.path.splitext(file)[0])

    n += 1

    for d in dets:
        # print("faceRec locate:",d)
        # print(type(d))
        # 使用opencv在原图上画出人脸位置
        left_top = (dlib.rectangle.left(d), dlib.rectangle.top(d))
        right_bottom = (dlib.rectangle.right(d), dlib.rectangle.bottom(d))
        cv2.rectangle(img, left_top, right_bottom, (0, 255, 0), 2, cv2.LINE_AA)

    # cv2.imwrite(os.path.join(faceRect_path,file), img)

numpy.save('candidates.npy',descriptors)
file= open('candidates.txt', 'w')
for candidate in candidates:
    file.write(candidate)
    file.write('\n')
file.close()

运行 python facerec_68point.py 得到识别结果all-face-result.jpg。

facerec_68point.py文件:

# -*- coding: UTF-8 -*-

import dlib
import cv2
import numpy

# 待检测图片
img_path = "all-face.jpg"
# 人脸关键点检测器
predictor_path="shape_predictor_68_face_landmarks.dat"
# 人脸识别模型
face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"
# 候选人文件
candidate_npydata_path = "candidates.npy"
candidate_path = "candidates.txt"

# 加载正脸检测器
detector = dlib.get_frontal_face_detector()
# 加载人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
# 加载人脸识别模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)


# 候选人脸描述子list

# 读取候选人数据
npy_data=numpy.load(candidate_npydata_path)
descriptors=npy_data.tolist()

# 候选人名单
candidate = []
file=open(candidate_path, 'r')
list_read = file.readlines()
for name in list_read:
    name = name.strip('\n')
    candidate.append(name)

print("Processing file: {}".format(img_path))
img = cv2.imread(img_path)

# 1.人脸检测
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))



for k, d in enumerate(dets):
    # 2.关键点检测
    shape = sp(img, d)
    face_descriptor = facerec.compute_face_descriptor(img, shape)
    d_test2 = numpy.array(face_descriptor)
    # 计算欧式距离
    dist = []
    for i in descriptors:
        dist_ = numpy.linalg.norm(i - d_test2)
        dist.append(dist_)
    num = dist.index(min(dist))  # 返回最小值

    left_top = (dlib.rectangle.left(d), dlib.rectangle.top(d))
    right_bottom = (dlib.rectangle.right(d), dlib.rectangle.bottom(d))
    cv2.rectangle(img, left_top, right_bottom, (0, 255, 0), 2, cv2.LINE_AA)
    text_point = (dlib.rectangle.left(d), dlib.rectangle.top(d) - 5)
    cv2.putText(img, candidate[num], text_point, cv2.FONT_HERSHEY_PLAIN, 2.0, (255, 255, 255), 2, 1)  # 标出face

cv2.imwrite('all-face-result.jpg', img)

# cv2.imshow("img",img) # 转成BGR显示
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()

运行 this_is_who_camera.py 打开摄像头进行实时的人脸识别

this_is_who_camera.py文件:

# -*- coding: UTF-8 -*-

import dlib,numpy 
import cv2          
import time

# 1.人脸关键点检测器
predictor_path = "shape_predictor_68_face_landmarks.dat"
# 2.人脸识别模型
face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"
# 3.候选人文件
candidate_npydata_path = "candidates.npy"
candidate_path = "candidates.txt"
# 4.储存截图目录
path_screenshots = "screenShots/"


# 加载正脸检测器
detector = dlib.get_frontal_face_detector()
# 加载人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
# 加载人脸识别模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)


# 候选人脸描述子list
# 读取候选人数据
npy_data=numpy.load(candidate_npydata_path)
descriptors=npy_data.tolist()
# 候选人名单
candidate = []
file=open(candidate_path, 'r')
list_read = file.readlines()
for name in list_read:
    name = name.strip('\n')
    candidate.append(name)

# 创建 cv2 摄像头对象
cv2.namedWindow("camera", 1)
cap = cv2.VideoCapture(0)
cap.set(3, 480)
# 截图 screenshots 的计数器
cnt = 0
while (cap.isOpened()):  #isOpened()  检测摄像头是否处于打开状态
    ret, img = cap.read()  #把摄像头获取的图像信息保存之img变量
    if ret == True:       #如果摄像头读取图像成功
        # 添加提示
        cv2.putText(img, "press 'S': screenshot", (20, 400), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA)
        cv2.putText(img, "press 'Q': quit", (20, 450), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA)

        # img_gray = cv2.cvtColor(im_rd, cv2.COLOR_RGB2GRAY)
        dets = detector(img, 1)
        if len(dets) != 0:
            # 检测到人脸
            for k, d in enumerate(dets):
                # 关键点检测
                shape = sp(img, d)
                # 遍历所有点圈出来
                for pt in shape.parts():
                    pt_pos = (pt.x, pt.y)
                    cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
                face_descriptor = facerec.compute_face_descriptor(img, shape)
                d_test2 = numpy.array(face_descriptor)
                # 计算欧式距离
                dist = []
                for i in descriptors:
                    dist_ = numpy.linalg.norm(i - d_test2)
                    dist.append(dist_)
                num = dist.index(min(dist))  # 返回最小值

                left_top = (dlib.rectangle.left(d), dlib.rectangle.top(d))
                right_bottom = (dlib.rectangle.right(d), dlib.rectangle.bottom(d))
                cv2.rectangle(img, left_top, right_bottom, (0, 255, 0), 2, cv2.LINE_AA)
                text_point = (dlib.rectangle.left(d), dlib.rectangle.top(d) - 5)
                cv2.putText(img, candidate[num][0:4], text_point, cv2.FONT_HERSHEY_PLAIN, 2.0, (255, 255, 255), 1, 1)  # 标出face

            cv2.putText(img, "facesNum: " + str(len(dets)), (20, 50),  cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 2, cv2.LINE_AA)
        else:
            # 没有检测到人脸
            cv2.putText(img, "facesNum:0", (20, 50),  cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 2, cv2.LINE_AA)

        k = cv2.waitKey(1)
        # 按下 's' 键保存
        if k == ord('s'):
            cnt += 1
            print(path_screenshots + "screenshot" + "_" + str(cnt) + "_" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + ".jpg")
            cv2.imwrite(path_screenshots + "screenshot" + "_" + str(cnt) + "_" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + ".jpg", img)

        # 按下 'q' 键退出
        if k == ord('q'):
            break
        cv2.imshow("camera", img)

# 释放摄像头
cap.release()
cv2.destroyAllWindows()

补充

  1. 每次人脸库candidate-face中加入新的人脸数据,均需运行python candidate_train.py
  2. python facerec_68point.py检测的是与人脸库中最相似的
  3. 提供 this_is_who.py 进行在test-face文件夹中的批量测试,测试结果存于faceRec文件夹,识别错误结果存于faceRec_ERROR
  4. 最近的项目是在红外人脸图像上进行的,人脸不太清晰,如果是正常摄像头效果应该会更好

运行结果

python facerec_68point.py在单张上的测试结果:
在这里插入图片描述
this_is_who.py在test-face文件夹中的批量测试结果:
在这里插入图片描述
this_is_who_camera.py实时检测效果.py
在这里插入图片描述
摄像头截图:
在这里插入图片描述
项目地址 :https://github.com/zj19941113/Face_Recognition_dlib

猜你喜欢

转载自blog.csdn.net/ffcjjhv/article/details/84637986
今日推荐