今天介紹使用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)