參考自https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/ python
在原有基礎上有一部分的修改(image改成可選參數,若不填則爲拍照後選取),若是有想深刻學習的,能夠去關注這位‘吳克’先生的文章。ide
本文不涉及關於人臉檢測的訓練部分(雖然以後隨着學習深刻我會再發相關的隨筆),只是簡單的用輪子。oop
今天咱們來使用dlib和opencv進行人臉的檢測標註學習
首先安裝opencv和dlib的方法ui
pip install dlib
pip install opencv-python
本程序中還使用了imutils用於resize圖片,安裝方法以下spa
pip install imutils
dlib中爲咱們提供了關於人臉檢測標註訓練好的文件 可在http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2下載(若是
在參考網頁下載到的例程中也包含了這個文件了).net
訓練好的文件可識別人臉的68個關鍵點並標註(關鍵點越少確定越容易致使識別錯誤)3d
本程序運行方法:若.py和shape_predictor_68_face_landmarks.dat以及須要檢測的圖片在同一目錄下,在當前目錄console中輸入code
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat -i guanhai.jpg
或採用拍照識別的方式,輸入orm
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat
在圖片框中按q完成拍照
以後會顯示標註後的照片
例如輸入以下圖片運行截圖
拍照而後識別就不舉例了吧,你們能夠自行嘗試
代碼以下my_facial_landmarks.py
from imutils import face_utilsimport argparseimport imutilsimport dlibimport cv2def takephoto(): cap = cv2.VideoCapture(0) while (1): # get a frame ret, frame = cap.read() # show a frame cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'):#按q鍵完成照相 # cv2.imwrite("./test0.jpg", frame) 保存照片,但在這裏咱們並不須要 return frame#返回圖片 cap.release() cv2.destroyAllWindows()def main(): # construct the argument parser and parse the arguments 使用argparse設置輸入所需的實參 ap = argparse.ArgumentParser() ap.add_argument("-p", "--shape-predictor", required=True, #訓練好的關於檢測的文件 help="path to facial landmark predictor") ap.add_argument("-i", "--image", required=False,default='0', #圖片 help="path to input image") args = vars(ap.parse_args()) # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor #初始化dlib人臉檢測(基於HOG),而後建立面部標誌預測器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) # load the input image, resize it, and convert it to grayscale if args['image'] != '0': image = cv2.imread(args['image'])#輸入圖片實參則讀入圖片 else: image = takephoto()#若未輸入則進行照相操做 image = imutils.resize(image, width=500) # 調整圖片寬度爲500 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#圖片調整爲灰色 # detect faces in the grayscale image 檢測灰度圖像中的面部 rects = detector(gray, 1) # loop over the face detections 循環進行人臉的檢測 for (i, rect) in enumerate(rects): # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array # 肯定面部區域的面部標誌,而後將面部標誌(x,y)座標轉換成NumPy陣列 shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # convert dlib's rectangle to a OpenCV-style bounding box # [i.e., (x, y, w, h)], then draw the face bounding box #將dlib矩形轉換爲OpenCV樣式的邊界框[即(x,y,w,h)],而後繪製邊界框 (x, y, w, h) = face_utils.rect_to_bb(rect) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # show the face number 人臉序號的標記(可識別多張) cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image #循環找到面部地關鍵點的(x,y)座標並在圖像上繪製它們 for (x, y) in shape: cv2.circle(image, (x, y), 1, (0, 0, 255), -1) # show the output image with the face detections + facial landmarks #用臉部檢測+面部標誌顯示輸出圖像 cv2.imshow("Output", image) cv2.waitKey(0)if __name__ == '__main__': main()