【人脸识别——Dlib学习2】Face Landmark Detection

  1. 官网文档翻译

http://dlib.net/face_landmark_detection.py.html

  1. 这个例子展示如何找到人的正脸,并且估计它的姿态。这个姿态由68个标点描述。人脸上会被标记很多点,例如嘴的边角,沿着眉毛,眼睛上等等。
  2. 我们使用的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.
"
        "For example, if you are in the python_examples folder then "
        "execute this program by running:
"
        "    ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces
"
        "You can download a trained facial shape predictor from:
"
        "    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()
原文地址:https://www.cnblogs.com/gfgwxw/p/9622955.html