环境准备
- 需要安装Visual Studio (C++平台),否则dlib模块无法安装成功。
- pip install dlib
- pip install numpy
- pip install opencv-python
- pip install scikit-image
1. 实现给识别的人脸图片加上红色矩形框
import dlib
from skimage import io
# 使用Dilb的正面人脸检测器frontal_face_detector
detector = dlib.get_frontal_face_detector()
# Dlib 的人脸检测模型
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 图片所在路径
# img = io.imread("x3.jpg")
img = io.imread("img.png")
# 生成Dlib的图像窗口
win = dlib.image_window()
win.set_image(img)
# 使用detector检测器来检测图像中的人脸
faces = detector(img, 1)
print("人脸数:", len(faces))
for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:", "left:", d.left(), "right:", d.right, "top:", d.top(), "bottom:", d.bottom)
# 绘制人脸脸部矩形框
win.add_overlay(faces)
# 保持图像
dlib.hit_enter_to_continue()
img.png:
运行效果:
测试如果一张图片中有多张人脸的效果:
2. 画上人脸轮廓
import dlib
from skimage import io
# 使用Dilb的正面人脸检测器frontal_face_detector
detector = dlib.get_frontal_face_detector()
# Dlib 的人脸检测模型
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 图片所在路径
# img = io.imread("zsm1.jpg")
img = io.imread("img.png")
# 生成Dlib的图像窗口
win = dlib.image_window()
win.set_image(img)
# 使用detector检测器来检测图像中的人脸
faces = detector(img, 1)
print("人脸数:", len(faces))
for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:", "left:", d.left(), "right:", d.right, "top:", d.top(), "bottom:", d.bottom)
# 使用predictor来计算面部轮廓关键点位置
shape = predictor(img, faces[i])
# 绘制面部轮廓矩形框
win.add_overlay(shape)
# 绘制人脸脸部矩形框
win.add_overlay(faces)
# 保持图像
dlib.hit_enter_to_continue()
运行效果:
多张人脸运行效果:
3. 进行利用训练过的模型识别检测
import dlib # pip install dlib
import glob
import numpy # pip install numpy
import os
import sys
import cv2 # pip install opencv-python
from skimage import io # pip install scikit-image
# 编写一个人脸识别程序
if len(sys.argv) != 2: # 命令行参数
print("请检查参数是否正确")
exit()
current_path = os.getcwd() # 获取当前路径
# 1. 人脸关键点检测器
# premod = "\\model\\shape_predictor_68_face_landmarks.dat"
premod = "shape_predictor_68_face_landmarks.dat"
# predictor_path = current_path + premod
predictor_path = premod
# 2. 人脸识别模型
# recmod = "\\model\\dlib_face_recognition_resnet_model_v1.dat"
recmod = "dlib_face_recognition_resnet_model_v1.dat"
# face_rec_model_path = current_path + recmod
face_rec_model_path = recmod
# 3. 备选人脸文件夹
faces_folder_path = "face_data1"
# 4. 需识别的人脸
img_path = sys.argv[1]
# img_path = "face_data1/img2.png"
# 5. 加载正脸检测器
detector = dlib.get_frontal_face_detector()
# 6. 加载人脸关键点检测器
sp = dlib.shape_predictor(predictor_path)
# 7. 加载人脸识别模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
# 8. 加载显示人脸窗体
win = dlib.image_window()
# 候选人脸描述子list
descriptors = []
# 9. 对文件夹下的每一个人脸进行
# (1)人脸检测
# (2)关键点检测
# (3)描述子提取
# for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
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)描述子提取,128维向量
face_descriptor = facerec.compute_face_descriptor(img, shape)
# 转换为numpy array
v = numpy.array(face_descriptor)
descriptors.append(v)
# 10. 对需识别人脸进行同样处理
# 提取描述子,不再注释
img = io.imread(img_path)
dets = detector(img, 1)
adist = []
for k, d in enumerate(dets):
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)
# 计算欧氏距离(什么是欧式距离:https://wiki.mbalib.com/wiki/%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E8%B7%9D%E7%A6%BB)
# 即两点(正数)之间最短距离
for i in descriptors:
dist_ = numpy.linalg.norm(i - d_test)
adist.append(dist_)
# 11. 候选人名单
candidate = ['jiejie', 'jobs', 'meimei', 'zsm1', 'zsm2', 'zsm3']
c_d = []
# 12. 候选人和距离组成一个dict
c_d = dict(zip(candidate, adist))
print(c_d)
cd_sorted = sorted(c_d.items(), key=lambda d:d[1])
print("\n 该照片上的人是:", cd_sorted[0][0])
# note: 模型下载地址:http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
# 运行方法:python 11.6.py img.png
# 运行方法:python 11.6.py img1.png