效果圖
Dlib Python 檢測人臉特徵點 Face Landmark Detection
首先安裝Dlib,Opencv庫 安裝Dlib
1.到這裡去下載你需要的的dlib輪子:Links for dlib
2.cmd進入你剛下好whl檔案的位置,然後輸入
pip install 檔案名稱.whl
注意尾碼是whl,並且選擇安裝的版本要和python版本一致,如果一次安裝不成功選擇不同的dlib安裝試試,總有一款會成功。 下載特徵檢測器
設定特徵檢測器,dlib有已經訓練的好的需要下載,也可以自己根據需要訓練
下載連結:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
下載完之後解壓,將路徑送到dlib.shape_predictor()裡面
import cv2import dlibdetector = dlib.get_frontal_face_detector()landmark_predictor = dlib.shape_predictor('D:/python/shape/shape_predictor_68_face_landmarks.dat')img = cv2.imread('D:/python/demos/d.jpg')faces = detector(img,1)if (len(faces) > 0): for k,d in enumerate(faces): cv2.rectangle(img,(d.left(),d.top()),(d.right(),d.bottom()),(255,255,255)) shape = landmark_predictor(img,d) for i in range(68): cv2.circle(img, (shape.part(i).x, shape.part(i).y),5,(0,255,0), -1, 8) cv2.putText(img,str(i),(shape.part(i).x,shape.part(i).y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,2555,255))cv2.imwrite("D:/python/demos/fdres/hh.jpg", img)//將實驗的映像儲存在fdres檔案夾下 cv2.imshow('Frame',img)cv2.waitKey(0)
detector是dlib訓練好的臉部偵測器,是基於HOG特徵的
shape = landmark_predictor(img,d)
我們想要的特徵點全部儲存在了shape裡面,d是dlib.rectangle(),裡面儲存著臉部偵測矩形的左上和右下座標,shape.part(i)是第i個特徵點landmark_predictor也是dlib訓練好的人臉特徵檢測器,是基於Ensemble of Regression Trees的,在CVPR2014的論文有過,單人臉的特徵點檢測速度極快,Dlib就是實現了這種演算法的,想要研究一下的可以看一下。
論文連結https://pdfs.semanticscholar.org/d78b/6a5b0dcaa81b1faea5fb0000045a62513567.pdf