Python 3 利用 Dlib 19.7 进行人脸识别(3)

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"
        "    ")
    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()

for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    print("Processing file: {}".format(f))
    img = io.imread(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)))
    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()

修改:

 绘制两个overlay,矩阵框 和 面部特征

import dlib
from skimage import io

# 使用特征提取器frontal_face_detector
detector = dlib.get_frontal_face_detector()

# dlib的68点模型
path_pre = "F:/code/python/P_dlib_face/"
predictor = dlib.shape_predictor(path_pre+"shape_predictor_68_face_landmarks.dat")

# 图片所在路径
path_pic = "F:/code/python/P_dlib_face/pic/"
img = io.imread(path_pic+"1.jpg")

# 生成dlib的图像窗口
win = dlib.image_window()
win.clear_overlay()
win.set_image(img)

# 特征提取器的实例化
dets = detector(img, 1)
print("人脸数:", len(dets))

for k, d in enumerate(dets):
        print("第", k, "个人脸d的坐标:",
              "left:", d.left(),
              "right:", d.right(),
              "top:", d.top(),
              "bottom:", d.bottom())

# 利用预测器预测
        shape = predictor(img, d)

# 绘制面部轮廓
        win.add_overlay(shape)

# 绘制矩阵轮廓
win.add_overlay(dets)
# 保持图像
dlib.hit_enter_to_continue()

结果

1 人脸数: 1 2 第 0 个人脸d的坐标: left: 79 right: 154 top: 47 bottom: 121

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fc4338304d88a20f3fd60a3ead43936a.html