上幾篇給大家講了OpenCV的圖檔人臉檢測,而本文給大家帶來的是比OpenCV更加精準的圖檔人臉檢測Dlib庫。
往期目錄
視訊人臉檢測——Dlib版(六)
OpenCV添加中文(五)
圖檔人臉檢測——Dlib版(四)
視訊人臉檢測——OpenCV版(三)
圖檔人臉檢測——OpenCV版(二)
OpenCV環境搭建(一)
更多更新,歡迎通路我的github:https://github.com/vipstone/faceai
dlib與OpenCV對比
識别精準度:Dlib >= OpenCV
Dlib更多的人臉識别模型,可以檢測臉部68甚至更多的特征點
效果展示

人臉的68個特征點
安裝dlib
下載下傳位址:https://pypi.org/simple/dlib/ 選擇适合你的版本,本人配置:
Window 10 + Python 3.6.4
我現在的版本是:dlib-19.8.1-cp36-cp36m-win_amd64.whl
使用指令安裝:
pip3 install D:\soft\py\dlib-19.8.1-cp36-cp36m-win_amd64.whl
顯示結果: Processing d:\soft\py\dlib-19.8.1-cp36-cp36m-win_amd64.whl Installing collected packages: dlib Successfully installed dlib-19.8.1
為安裝成功。
下載下傳訓練模型
訓練模型用于是人臉識别的關鍵,用于查找圖檔的關鍵點。
下載下傳位址:http://dlib.net/files/
下載下傳檔案:shape_predictor_68_face_landmarks.dat.bz2
當然你也可以訓練自己的人臉關鍵點模型,這個功能會放在後面講。
下載下傳好的模型檔案,我的存放位址是:C:\Python36\Lib\site-packages\dlib-data\shape_predictor_68_face_landmarks.dat.bz2
解壓:shape_predictor_68_face_landmarks.dat.bz2得到檔案:shape_predictor_68_face_landmarks.dat
代碼實作
#coding=utf-8
import cv2
import dlib
path = "img/meinv.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人臉分類器
detector = dlib.get_frontal_face_detector()
# 擷取人臉檢測器
predictor = dlib.shape_predictor(
"C:\\Python36\\Lib\\site-packages\\dlib-data\\shape_predictor_68_face_landmarks.dat"
)
dets = detector(gray, 1)
for face in dets:
shape = predictor(img, face) # 尋找人臉的68個标定點
# 周遊所有點,列印出其坐标,并圈出來
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
關注下面二維碼,訂閱更多精彩内容。
關注公衆号(加好友):
作者:
王磊的部落格
出處:
http://vipstone.cnblogs.com/