Python DLib库进行人脸关键点识别

人脸识别之Python DLib库进行人脸关键点识别

安装dlib模块

pip install dlib -i https://pypi.doubanio.com/simple

人脸检测

首先调用dlib.get_frontal_face_detector() 来加载dlib自带的人脸检测器
dets = detector(img, 1)将检测器应用在输入图片上,结果返回给dets(参数1表示对图片进行上采样一次,有利于检测到更多的人脸);
dets的个数即为检测到的人脸的个数;
遍历dets可以获取到检测到的每个人脸四个坐标极值。

import dlib
import cv2

def rect_to_bb(rect): # 获得人脸矩形的坐标信息
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    return (x, y, w, h)
def detect(imgPath):
    detector = dlib.get_frontal_face_detector() # 加载dlib自带的人脸检测器
    image = cv2.imread(imgPath) # 读取图片
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 将图片转化为灰度值图像
    rects = detector(gray, 1) # 将检测器应用在输入图片
    for (i, rect) in enumerate(rects):#遍历获得坐标
        (x, y, w, h) = rect_to_bb(rect)
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # 绘画矩形框
        cv2.putText(image, "Face", (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 写入文字
    cv2.imshow("Output", image)
    cv2.waitKey(0)
if __name__ == '__main__':
    detect("1.jpg")
原文地址:https://www.cnblogs.com/lyhLive/p/13390286.html