神经网络算法标识人脸特征值

今天介绍使用dlib库的DNN(深层神经网络)算法识别人脸,并将人脸特征值标注出来。

详细教程-深层神经网络算法标识人脸特征值(完整代码)

 

环境配置

环境拓扑:

操作系统:Windows7 64bit

Python版本:3.7.4

OpenCV版本:4.1.1.26

Dlib版本:19.17.0

配置环境:

安装dlib之前,必须安装cmake(跨平台的编译工具)和boost(是为C++语言标准库提供扩展的一些C++程序库的总称)。利用pip.exe按照顺序先后安装pip install cmake、pip install boost、pip install dlib,安装后pip list命令查看是否成功。

实现流程:

详细教程-深层神经网络算法标识人脸特征值(完整代码)

 

完整代码:

# dlib dnn 识别人脸,并标注人脸关键点
import dlib # 人脸处理的库 Dlib
import cv2 # 图像处理的库 OpenCv

# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()
# Dlib 68 点特征预测器
predictor = dlib.shape_predictor('F:/workspace_Python/Dlib_face_recognition/data_dlib/shape_predictor_68_face_landmarks.dat')

# 人脸图片
faces_path = "F:\\faces.png"

#使用opencv读取图片
img = cv2.imread(faces_path)
#转换为灰度图片
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

#使用detector进行人脸检测 faces 为返回的人脸数
faces = detector(gray, 0)

#dets的元素个数即为脸的个数
print("Number of faces detected: {}".format(len(faces)))

if len(faces) != 0:
 # 检测到人脸

 #使用enumerate 函数遍历序列中的元素以及它们的下标
 #下标idx即为人脸序号
 #left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
 #top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
 for idx, d in enumerate(faces): 
 x, y, w, h = d.left(), d.top(), d.right() - d.left(), d.bottom() - d.top()
 cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2, 8, 0)
 #使用predictor进行人脸关键点识别 shape为返回的结果
 shape = predictor(gray, d)

 print("The face {} features: ".format(idx + 1))
 # 标识人脸68个特征点
 i = 0
 for point in shape.parts(): 
 
 if i >= 0 and i <= 16:
 # 0~16 下巴
 cv2.circle(img, (point.x, point.y), 1, color=(0, 0, 255)) # 红色 
 elif i >= 17 and i <= 21:
 # 17~21 左眉
 cv2.circle(img, (point.x, point.y), 1, color=(0, 255, 0)) # 绿色 
 elif i >= 22 and i <= 26:
 # 22~26 右眉
 cv2.circle(img, (point.x, point.y), 1, color=(255, 0, 0)) # 蓝色
 elif i >= 27 and i <= 30:
 # 27~30 鼻梁
 cv2.circle(img, (point.x, point.y), 1, color=(0, 0, 255)) # 红色
 elif i >= 31 and i <= 35:
 # 31~35 鼻尖
 cv2.circle(img, (point.x, point.y), 1, color=(0, 255, 0)) # 绿色
 elif i >= 36 and i <= 41:
 # 36~41 左眼
 cv2.circle(img, (point.x, point.y), 1, color=(255, 0, 0)) # 蓝色 
 elif i >= 42 and i <= 47:
 # 42~47 右眼
 cv2.circle(img, (point.x, point.y), 1, color=(0, 0, 255)) # 红色 
 elif (i >= 48 and i <= 54) or (i >= 60 and i <= 64):
 # 48~54 60~64 上嘴唇
 cv2.circle(img, (point.x, point.y), 1, color=(255, 0, 0)) # 蓝色
 elif (i == 48) or (i >= 54 and i <= 60) or (i >= 64 and i <= 57):
 # 48 54~60 64~67 下嘴唇
 cv2.circle(img, (point.x, point.y), 1, color=(0, 255, 0)) # 绿色

 # 68 个特征点编码
 #pos = (point.x, point.y)
 #cv2.putText(img, str(i + 1), pos, cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 255, 0), 1, cv2.LINE_AA)
 print("feature {}:, point({}, {})".format(i, point.x, point.y))
 i = i + 1
else:
 # 没有检测到人脸
 cv2.putText(img, "no face", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA)

#显示标注出特征值的图片
cv2.imshow("image", img)
cv2.waitKey(0)

图像显示:

详细教程-深层神经网络算法标识人脸特征值(完整代码)

 

输出结果:

Number of faces detected: 7
The face 1 features: 
feature 0:, point(151, 147)
feature 1:, point(151, 157)
feature 2:, point(152, 166)
feature 3:, point(154, 176)
feature 4:, point(157, 184)
feature 5:, point(164, 192)
feature 6:, point(172, 197)
feature 7:, point(182, 203)
feature 8:, point(191, 204)
feature 9:, point(200, 203)
feature 10:, point(207, 197)
feature 11:, point(212, 191)
feature 12:, point(217, 184)
feature 13:, point(220, 176)
feature 14:, point(222, 168)
feature 15:, point(223, 160)
feature 16:, point(224, 152)
feature 17:, point(162, 137)
feature 18:, point(168, 134)
feature 19:, point(175, 133)
feature 20:, point(182, 134)
feature 21:, point(189, 137)
feature 22:, point(201, 139)
feature 23:, point(207, 138)
feature 24:, point(213, 138)
feature 25:, point(218, 139)
feature 26:, point(222, 143)
feature 27:, point(195, 147)
feature 28:, point(195, 152)
feature 29:, point(195, 158)
feature 30:, point(195, 163)
feature 31:, point(187, 169)
feature 32:, point(191, 170)
feature 33:, point(194, 171)
feature 34:, point(197, 171)
feature 35:, point(200, 170)
feature 36:, point(170, 146)
feature 37:, point(174, 144)
feature 38:, point(179, 144)
feature 39:, point(183, 148)
feature 40:, point(178, 149)
feature 41:, point(173, 148)
feature 42:, point(203, 150)
feature 43:, point(207, 147)
feature 44:, point(211, 148)
feature 45:, point(215, 151)
feature 46:, point(211, 152)
feature 47:, point(207, 152)
feature 48:, point(179, 182)
feature 49:, point(185, 178)
feature 50:, point(191, 176)
feature 51:, point(194, 178)
feature 52:, point(197, 177)
feature 53:, point(201, 179)
feature 54:, point(205, 183)
feature 55:, point(201, 186)
feature 56:, point(197, 188)
feature 57:, point(193, 188)
feature 58:, point(190, 187)
feature 59:, point(184, 186)
feature 60:, point(181, 182)
feature 61:, point(190, 181)
feature 62:, point(194, 181)
feature 63:, point(197, 181)
feature 64:, point(203, 183)
feature 65:, point(197, 182)
feature 66:, point(194, 182)
feature 67:, point(190, 182)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章