Dlib库教程(2):联合python的人脸检测、标记、识别

1 说明
=====
1.1 准备篇《Dlib库教程(1):介绍、linux下的安装与避坑》 。
1.2 本次讲解Dlib库的强大的联合python的人脸检测、标记和识别功能 。
1.3 熟悉Dlib库的python的API功能函数 。
1.4 熟悉python编程相关知识 , 讲解思路清晰 , 注释仔细 , 干货满满 , 由浅入深 , 一秒入门 。
1.5 图片来自今日头条正版免费图库 , 表示对女神的喜爱 , 致敬 , 仅供学习 。
1.6 环境:python3.8+dlib19.21.99+opencv4.4.0+deepin-linux操作系统 。
Dlib库教程(2):联合python的人脸检测、标记、识别文章插图
1.jpeg
Dlib库教程(2):联合python的人脸检测、标记、识别文章插图
2.jpeg
2 dlib+opencv+python的人脸检测、识别和标示
====================================
2.1 代码:
#人脸检测 , 画框和画68点特征显示#第1步:导入模块import dlibimport cv2#第2步:模型加载#这是人脸68点特征检测器模型 , 提前官网下载好shape_predictor_path = '/home/xgj/Desktop/dlib/shape_predictor_68_face_landmarks.dat'#获取人脸分类器detector = dlib.get_frontal_face_detector()#获取人脸 68 点特征检测器 , 进行人脸面部轮廓特征提取:predictor = dlib.shape_predictor(shape_predictor_path)#第3步:被检测图片#载入被检测图片路径imgpath ='/home/xgj/Desktop/dlib/pic/2.jpeg'#读图片 , cv.imread()共两个参数 , 第二个为如何读取图片 , #包括cv2.IMREAD_COLOR: 读入一个彩色图片#img = cv2.imread(imgpath, cv2.IMREAD_COLOR)img = cv2.imread(imgpath)#默认 , 等同于上面读取彩色图片#这个是cv2特有的图片读取 , 需要转换 , 否则不是原图b, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])#侦测人脸dets = detector(img2, 1)#第4步:for index, face in enumerate(dets):left = face.left()top = face.top()right = face.right()bottom = face.bottom()#在人脸上标示绿色方框cv2.rectangle(img,(left, top),(right, bottom),(0, 255, 0), 1)shape = predictor(img, face)for index, pt in enumerate(shape.parts()):pt_pos = (pt.x, pt.y)#标示人脸68个特征圆点cv2.circle(img, pt_pos, 1, (255, 0, 0), 1)#第5步:#展示已经人脸识别后的带有标示图片cv2.imshow('pic',img)cv2.namedWindow('pic',cv2.WINDOW_AUTOSIZE)#窗口关闭设置k = cv2.waitKey(0)cv2.destroyAllWindows()2.2 图
Dlib库教程(2):联合python的人脸检测、标记、识别文章插图
3 dlib+cv2+numpy法人脸检测和识别
===========================
3.1 代码
# encoding:utf-8#代码来源#;depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param#人脸检测 , 画框import dlibimport numpy as npimport cv2# 获得人脸矩形的坐标信息函数def rect_to_bb(rect):x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)# 图片大小调整函数def resize(image, width=1200):# 将待检测的image进行resizer = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resized# 人脸检测函数def detect():#加载待检测的图片image_file = "/home/xgj/Desktop/dlib/pic/1.jpeg"image = cv2.imread(image_file)#调整大小image = resize(image, width=1200)#灰度转换gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#dlib人脸检测器和人脸检测画框detector = dlib.get_frontal_face_detector()#dlib人脸检测矩形框4点坐标和对图像画人脸框rects = detector(gray, 1)#画框和输出文字for (i, rect) in enumerate(rects):(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face:{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)#图片展示cv2.imshow("Output", image)cv2.waitKey(0)if __name__ == "__main__":detect()3.2 图
Dlib库教程(2):联合python的人脸检测、标记、识别文章插图
3.3 增加功能 , 标注68人脸特征点
=========================
3.3.1 代码
# encoding:utf-8import dlibimport numpy as npimport cv2# 获得人脸矩形的坐标信息def rect_to_bb(rect):x = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - yreturn (x, y, w, h)# 将包含68个特征的的shape转换为numpy array格式def shape_to_np(shape, dtype="int"):coords = np.zeros((68, 2), dtype=dtype)for i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)return coords# 将待检测的image进行resizedef resize(image, width=1200):r = width * 1.0 / image.shape[1]dim = (width, int(image.shape[0] * r))resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)return resized#人脸检测、识别、标注68特征点def feature():#加载被检测图片image_file = "/home/xgj/Desktop/dlib/pic/2.jpeg"image = cv2.imread(image_file)image = resize(image, width=1200)gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#加载dlib功能函数detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor("/home/xgj/Desktop/dlib/shape_predictor_68_face_landmarks.dat")rects = detector(gray, 1)shapes = []#人脸检测、识别和画框for (i, rect) in enumerate(rects):shape = predictor(gray, rect)#获取特征点shape = shape_to_np(shape)#存入列表中shapes.append(shape)(x, y, w, h) = rect_to_bb(rect)cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)#获取68关键点并画圈标注for shape in shapes:for (x, y) in shape:cv2.circle(image, (x, y), 2, (0, 0, 255), -1)cv2.imshow("Output", image)cv2.waitKey(0)if __name__ == "__main__":feature()