人脸识别68特征点的提取,使用python+dlib

#版本python3
#建议使用anconda + pycharm 进行前期配置环境,比较简单
#安装教程请自行百度很简单


import numpy as np
import cv2
import dlib
import os
detector = dlib.get_frontal_face_detector()#创建一个容器
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')#加载一个自带的分类器
img_path = "C:\\Users\\MSI\\Desktop\\my_py_software\\img\\" + str(1) + ".jpg"#我需要识别的图片位置
img = cv2.imread(img_path)#使用python-opencv读取图片
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#使图片转化为灰度图片
rects = detector(img_grey, 0)#返回信息
for i in range(len(rects)):
    landmarks = np.matrix([[p.x, p.y] for p in predictor(img_grey,rects[i]).parts()])#获取点的坐标
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])
        cv2.circle(img, pos, 2, (255, 0, 0), 1)#画圈圈,(255,0,0)是圈圈的颜色
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(idx + 1), pos, font, 0.8, (0, 0, 255), 2, cv2.LINE_AA)#为圈圈标上序号
cv2.namedWindow("img", 2)
cv2.imshow("img", img)#展示
cv2.waitKey(0)

你可能感兴趣的:(人脸识别68特征点的提取,使用python+dlib)