人脸关键点检测

开源库有dlib

安装:https://pypi.org/simple/dlib/ ,下载后pip安装

模型下载:Index of /files

1、dlib5点

左右眼角及人中5点。

import dlib
import numpy as np
import cv2
import time

predictor_path = '../model/shape_predictor_5_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

start_time = time.time()

frame = cv2.imread("../image/ab.jpg")
img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
dets = detector(img, 1)

print(dets)

for index, det in enumerate(dets):
    right_top = (det.right(), det.top())
    left_bottom = (det.left(), det.bottom())
    # 画面部检测框
    # cv2.rectangle(frame, right_top, left_bottom, (255, 0, 0), 2)
    print(right_top,left_bottom)

    # 面部特征点检测
    shape = predictor(img, det)
    landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])
        cv2.circle(frame, pos, 5, (0, 0, 255), -1)
        cv2.putText(frame,str(idx),pos,cv2.FONT_HERSHEY_COMPLEX,0.6,(0,0,255),1)

cv2.imwrite('kp2.jpg',frame)
cv2.imshow('res',frame)
cv2.waitKey()

2、dlib68点

基本包含脸部区域关键点,但是额头区域没有关键点以及只有眉毛的上边缘点。

import dlib
import numpy as np
import cv2
import time

predictor_path = '../model/shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

start_time = time.time()

frame = cv2.imread("../image/ab.jpg")
img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
dets = detector(img, 1)

print(dets)

for index, det in enumerate(dets):
    right_top = (det.right(), det.top())
    left_bottom = (det.left(), det.bottom())
    # 画面部检测框
    # cv2.rectangle(frame, right_top, left_bottom, (255, 0, 0), 2)
    print(right_top,left_bottom)

    # 面部特征点检测
    shape = predictor(img, det)
    landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])
        cv2.circle(frame, pos, 5, (0, 0, 255), -1)
        cv2.putText(frame,str(idx),pos,cv2.FONT_HERSHEY_COMPLEX,0.6,(0,0,255),1)

cv2.imwrite('kp2.jpg',frame)
cv2.imshow('res',frame)
cv2.waitKey()

3、 dlib81点

相比较68点,多出13点分布在额头区域,但是点的精度不是太高。

import dlib
import numpy as np
import cv2
import time

predictor_path = '../model/shape_predictor_81_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

start_time = time.time()

frame = cv2.imread("../image/ab.jpg")
img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
dets = detector(img, 1)

print(dets)

for index, det in enumerate(dets):
    right_top = (det.right(), det.top())
    left_bottom = (det.left(), det.bottom())
    # 画面部检测框
    # cv2.rectangle(frame, right_top, left_bottom, (255, 0, 0), 2)
    print(right_top,left_bottom)

    # 面部特征点检测
    shape = predictor(img, det)
    landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])
        cv2.circle(frame, pos, 5, (0, 0, 255), -1)
        cv2.putText(frame,str(idx),pos,cv2.FONT_HERSHEY_COMPLEX,0.6,(0,0,255),1)

cv2.imwrite('kp2.jpg',frame)
cv2.imshow('res',frame)
cv2.waitKey()

  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HySmiley

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值