1.安装基础环境
安装python
python-3.6.0-amd64.exe
安装Anaconda3
Anaconda3-2020.11-Windows-x86_64.exe
安装pycharm
pycharm-community-2020.3.5.exe
安装CMake,安装完后将CMake的路径配置到环境变量Path路径中
cmake-3.17.3-win64-x64.exe
安装cuda 10.2
cuda_10.2.89_441.22_win10
cudnn-10.2-windows10-x64-v7.6.5.32
2.dlib下载
下载地址:http://dlib.net/files/
dlib-19.17.zip
dlib_face_recognition_resnet_model_v1.dat.bz2
shape_predictor_68_face_landmarks.dat.bz2
3.安装依赖包
conda create -n dlib python=3.6
conda activate dlib
pip install python-skimage
pip install opencv-python
pip install python-skimage
pip install scikit-image
编译dlib库
cd到D:\Projects\dlib\dlib-19.17
执行python setup.py install
备注:
如果报出floor相关的错误,解决如下:
将所有的floor替换为floorf
4.执行代码
Pycharm打开dlib代码
模型数据配置:
关键点检测:
import cv2
import dlib
from skimage import io
# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
# dlib的68点模型,使用作者训练好的特征预测器
predictor = dlib.shape_predictor("D:\\Projects\\dlib\\dlib-19.17\\data\\shape_predictor_68_face_landmarks.dat")
# 图片所在路径
img = io.imread("D:\\Projects\\dlib\\dlib-19.17\\data\\test.jpg")
cv2.imwrite("D:\\1.png", img)
# 生成dlib的图像窗口
win = dlib.image_window()
win.clear_overlay()
win.set_image(img)
# 特征提取器的实例化
dets = detector(img, 1)
print("人脸数:", len(dets))
for k, d in enumerate(dets):
print("第", k + 1, "个人脸d的坐标:",
"left:", d.left(),
"right:", d.right(),
"top:", d.top(),
"bottom:", d.bottom())
width = d.right() - d.left()
heigth = d.bottom() - d.top()
print('人脸面积为:', (width * heigth))
# 利用预测器预测
shape = predictor(img, d)
# 标出68个点的位置
for i in range(68):
cv2.circle(img, (shape.part(i).x, shape.part(i).y), 4, (0, 255, 0), -1, 8)
cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
# 显示一下处理的图片,然后销毁窗口
cv2.imshow('face', img)
cv2.imwrite("D:\\1.png", img)
cv2.waitKey(0)
人脸识别:
# -*- coding: UTF-8 -*-
import sys, os, dlib, glob, numpy
from skimage import io
# if len(sys.argv) != 5:
# print('请检查参数是否正确')
# exit()
# 1.人脸关键点检测器
predictor_path = "D:\\Projects\\dlib\\dlib-19.17\\data\\shape_predictor_68_face_landmarks.dat"
# 2.人脸识别模型
face_rec_model_path = "D:\\Projects\\dlib\\dlib-19.17\\data\\dlib_face_recognition_resnet_model_v1.dat"
# 3.候选人脸文件夹
faces_folder_path = "D:\\Projects\\dlib\\dlib-19.17\\data\\face"
# 4.需识别的人脸
img_path = "D:\\Projects\\dlib\\dlib-19.17\\data\\1.jpg"
# 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)
# win = dlib.image_window()
# 候选人脸描述子list
descriptors = []
# 对文件夹下的每一个人脸进行:
# 1.人脸检测
# 2.关键点检测
# 3.描述子提取
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
# win.clear_overlay()
# win.set_image(img)
# 1.人脸检测
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
# 2.关键点检测
shape = sp(img, d)
# 画出人脸区域和和关键点
# win.clear_overlay()
# win.add_overlay(d)
# win.add_overlay(shape)
# 3.描述子提取,128D向量
face_descriptor = facerec.compute_face_descriptor(img, shape)
# 转换为numpy array
v = numpy.array(face_descriptor)
descriptors.append(v)
# 对需识别人脸进行同样处理
# 提取描述子,不再注释
img = io.imread(img_path)
dets = detector(img, 1)
dist = []
for k, d in enumerate(dets):
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)
# 计算欧式距离
for i in descriptors:
dist_ = numpy.linalg.norm(i - d_test)
dist.append(dist_)
# 候选人名单
candidate = ['shishi', 'bingbing']
# 候选人和距离组成一个dict
c_d = dict(zip(candidate, dist))
cd_sorted = sorted(c_d.items(), key=lambda d: d[1])
print('\n The person is: %s' % (cd_sorted[0][0]))
dlib.hit_enter_to_continue()
备注
如果模型数据下载较慢,这边提供已经配置好的代码与模型,供下载。
链接:https://pan.baidu.com/s/1jzC5_gixioc6Ssst3DUMQA
提取码:zhgp
参考
https://www.cnblogs.com/wjw1014/p/10288196.html
https://blog.csdn.net/ebzxw/article/details/80441556