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.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

要实现人脸识别,需要使用OpenCVDlib这两个。以下是使用C++实现人脸识别的基本步骤: 1. 安装OpenCVDlib 首先需要安装OpenCVDlib,并将其包到C++项目中。可以使用以下命令在Ubuntu上安装这两个: ``` sudo apt-get install libopencv-dev sudo apt-get install libdlib-dev ``` 2. 加载人脸识别模型 使用Dlib提供的人脸检测器和68个关键点检测器,需要加载人脸识别模型。可使用以下代码: ``` #include <dlib/opencv.h> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing.h> using namespace dlib; frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("shape_predictor_68_face_landmarks.dat") >> sp; ``` 3. 加载人脸数据 将需要识别的人脸图片保存到人脸数据中。可使用以下代码加载人脸数据: ``` std::vector<matrix<rgb_pixel>> faces; std::vector<std::string> labels; // Load faces from a directory path load_image_dataset(faces, labels, "faces"); ``` 4. 人脸检测和关键点检测 使用Dlib提供的人脸检测器和68个关键点检测器,对待识别的人脸图像进行处理,提取人脸特征。可使用以下代码: ``` // Load the input image cv::Mat inputImg = cv::imread("face.jpg"); // Convert the input image to Dlib's format cv_image<rgb_pixel> dlibImg(inputImg); // Detect faces in the image std::vector<rectangle> dets = detector(dlibImg); // Find the pose of each face std::vector<full_object_detection> shapes; for (unsigned long j = 0; j < dets.size(); ++j) { full_object_detection shape = sp(dlibImg, dets[j]); shapes.push_back(shape); } ``` 5. 人脸识别 将待识别的人脸特征与人脸数据中的特征进行比对,找到最相似的人脸。可使用以下代码: ``` // Compute the face descriptor for each face std::vector<matrix<float,0,1>> faceDescriptors; for (unsigned long i = 0; i < shapes.size(); ++i) { matrix<rgb_pixel> faceChip; extract_image_chip(dlibImg, get_face_chip_details(shapes[i],150,0.25), faceChip); faceDescriptors.push_back(net(faceChip)); } // Find the closest match in the database std::vector<double> distances; std::string bestLabel; double bestDistance = 1.0; for (unsigned long i = 0; i < faces.size(); ++i) { double distance = length(faceDescriptors[0] - faceDescriptors[i]); if (distance < bestDistance) { bestDistance = distance; bestLabel = labels[i]; } } ``` 以上是使用C++实现人脸识别的基本步骤。可以根据实际需求对代码进行修改和优化。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值