python3+OpenCv+dlib实现人脸68个关键点检测并标注(windows安装)

首先安装环境:
首先安装CMake:pip install CMake
安装dlib: pip install dlib(安装失败,但是标注版本安装成功pip install dlib==19.6.1)
安装CV2:pip install opencv-python
人脸检测 + 标注
利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68点标定,利用OpenCv进行图像化处理,在人脸上画出68个点,并标明序号;68点标定+opencv绘点
模型:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
网盘链接https://pan.baidu.com/s/1qjp1tNEqm-0fJCKfqeBK-Q 密码:awdf
源码:
import dlib
import cv2

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(‘model/shape_predictor_68_face_landmarks.dat’)
img = cv2.imread(“picture/demo2.JPG”)

dets = detector(img, 1)
for k, d in enumerate(dets):
print(“dets{}”.format(d))
print(“Detection {}: Left: {} Top: {} Right: {} Bottom: {}”.format(
k, d.left(), d.top(), d.right(), d.bottom()))

# 使用predictor进行人脸关键点识别 shape为返回的结果
shape = predictor(img, d)
# 获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))

# 绘制特征点
for index, pt in enumerate(shape.parts()):
    print('Part {}: {}'.format(index, pt))
    pt_pos = (pt.x, pt.y)
    cv2.circle(img, pt_pos, 1, (255, 0, 0), 2)
    # 利用cv2.putText输出1-68
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, str(index + 1), pt_pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)

cv2.imshow(‘img’, img)
cv2.imwrite(‘picture/demo3.JPG’, img)
k = cv2.waitKey()
cv2.destroyAllWindows()

你可能感兴趣的:(工作感悟)