- 官網文檔翻譯
http://dlib.net/face_landmark_detection.py.html
- 這個例子展示如何找到人的正臉,並且估計它的姿態。這個姿態由68個標點描述。人臉上會被標記很多點,例如嘴的邊角,沿着眉毛,眼睛上等等。
- 我們使用的Face detector是使用經典的HOG特征,結合線性分類器、圖像金字塔和滑動窗口檢測的算法。姿態估計器的建立是基於下文:One Millisecond Face Alignment with an Ensemble of Regression Trees by Vahid Kazemi and Josephine Sullivan, 並且在iBUG 300-W face landmark dataset進行訓練。
# -*-coding:utf-8-*- #author: lyp time: 2018/9/10 import sys import os import dlib import glob # 本例子要求你在cmd中輸入兩個參數 # 參數一是68點文件的路徑,傳給predictor_path # 參數二是要檢測的圖片的路徑,傳給face_folder_path # Windows這個方式不太友好,一直提醒沒有dlib模塊。 if len(sys.argv) != 3: print( "Give the path to the trained shape predictor model as the first " "argument and then the directory containing the facial images.\n" "For example, if you are in the python_examples folder then " "execute this program by running:\n" " ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n" "You can download a trained facial shape predictor from:\n" " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2") exit() # 輸入的路徑傳給對應參數 predictor_path = sys.argv[1] faces_folder_path = sys.argv[2] detector = dlib.get_frontal_face_detector() # 人臉檢測器的生成 predictor = dlib.shape_predictor(predictor_path) # 特征點提取器的生成 win = dlib.image_window() # dlib提供的圖片窗口 # 獲取指定文件路徑下的所有.jpg文件,'*'是通配符 for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")): print("Processing file: {}".format(f)) img = dlib.load_rgb_image(f) win.clear_overlay() win.set_image(img) # Ask the detector to find the bounding boxes of each face. The 1 in the # second argument indicates that we should upsample the image 1 time. This # will make everything bigger and allow us to detect more faces. # 將圖像進行向上采樣一倍 dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) # 使用enumerate函數遍歷dets中元素 for k, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) # Get the landmarks/parts for the face in box d. shape = predictor(img, d) print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1))) # Draw the face landmarks on the screen. win.add_overlay(shape) win.add_overlay(dets) dlib.hit_enter_to_continue()