dlib实现人脸关键点检测检测方法

4 篇文章 0 订阅

概述

dlib支持检测人脸特征关键点,官方提供了68维度和5维度的人脸关键店检测预训练模型提供下载使用。

关键点检测实现方法

实现步骤

  1. 加载图片,进行人脸区域的检测 (包含人脸检测模型创建等)
  2. 传入图片和人脸检测结果,获取人脸换关键点数据(包含face landmark模型创建等)
  3. 解析人脸关键点数据

相关数据与模型文件下载:地址
CNN人脸检测模型名称 :mmod_human_face_detector.dat.bz2
68维人脸检测模型名称 : shape_predictor_68_face_landmarks.dat.bz2
5维人脸检测模型名称 :shape_predictor_5_face_landmarks.dat.bz2

代码示例

本实例中,使用CNN人脸检测模型和68维人脸关键点检测模型

import dlib
import numpy as np

from cv2 import cv2

# step 1. create the face detector and shape predictor model
face_detector_model_path = '../models/mmod_human_face_detector.dat'
face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path)  # dlib.cnn_face_detection_model_v1
shape_model_path = r'../models/shape_predictor_68_face_landmarks.dat'
face_shape_predictor = dlib.shape_predictor(shape_model_path)  # dlib.shape_predictor

# step 2. process face detection
# note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2
image_path = "sample.jpg"
img = dlib.load_rgb_image(image_path)
# img = cv2.cvtColor(cv2.imread(image_path),cv2.COLOR_BGR2RGB)
detections = face_detector(img, 1)  # dlib.mmod_rectangles

# step 3. get shape of one face for example
detection = detections[0]  # dlib.mmod_rectangle
# the mmod_rectangle contains two parts : confidence and rect

shape = face_shape_predictor(img, detection.rect)  # dlib.full_object_detection

step 4. get all the face landmark points
 landmark_points = shape.parts() # dlib.points

效果实例

将所有的68维度人脸特征关键点连接之后的效果图如下所示

关键点的顺序与对应面部位置:

关键类与接口方法

概述

  1. 人脸检测类 : dlib.fhog_object_detectordlib.cnn_face_detection_model_v1,前者基于HOG模型,后者基于CNN模型,前者检测方法调用为__call(img)__ ->dlib.rectanglesrun(img,upsample_num,threshold)->(dlib.rectangles,List[scores:int],List[int]),后者检测方法调用为__call(img)__->dlib.mmod_rectangles
  2. 关键点检测类: dlib.shape_predictor,预测调用方法__call__(self,image, box: dlib.rectangle)->dlib.full_object_detection
  3. 检测结果类 : dlib.full_object_detection, 常用方法 part(self, idx: int)->dlib.point 单个关键点信息 、parts(self)->dlib.points 所有的关键点信息
  4. 关键点类:dlib.point关键点,成员包含x,y,dlib.points关键点列表

关键点检测类: dlib.shape_predictor

class dlib.shape_predictor
    This object is a tool that takes in an image region containing some object and outputs a set of point locations that define the pose of the object. The classic example of this is human face pose prediction, where you take an image of a human face as input and are expected to identify the locations of important facial landmarks such as the corners of the mouth and eyes, tip of the nose, and so forth.

    __call__(self: dlib.shape_predictor, image: array, box: dlib.rectangle) → dlib.full_object_detection
        requires
            image is a numpy ndarray containing either an 8bit grayscale or RGB image.
            box is the bounding box to begin the shape prediction inside.
        ensures
            This function runs the shape predictor on the input image and returns a single full_object_detection.
    __init__(*args, **kwargs)
        Overloaded function.

    __init__(self: dlib.shape_predictor) -> None
    __init__(self: dlib.shape_predictor, arg0: unicode) -> None
        Loads a shape_predictor from a file that contains the output of the train_shape_predictor() routine.

    save(self: dlib.shape_predictor, predictor_output_filename: unicode) → None
    Save a shape_predictor to the provided path.

检测结果类 : dlib.full_object_detection

class dlib.full_object_detection
    This object represents the location of an object in an image along with the positions of each of its constituent parts.

    __init__(self: dlib.full_object_detection, rect: dlib.rectangle, parts: object) → None
        requires
            rect: dlib rectangle
            parts: list of dlib.point, or a dlib.points object.
    num_parts
        The number of parts of the object.
    
    part(self: dlib.full_object_detection, idx: int) → dlib.point
        A single part of the object as a dlib point.
    
    parts(self: dlib.full_object_detection) → dlib.points
        A vector of dlib points representing all of the parts.
    
    rect
        Bounding box from the underlying detector. Parts can be outside box if appropriate.

关键点类:dlib.point dlib.points

class dlib.point
    This object represents a single point of integer coordinates that maps directly to a dlib::point.
    
    __init__(*args, **kwargs)
        Overloaded function.
    
    __init__(self: dlib.point, x: int, y: int) -> None
    __init__(self: dlib.point, p: dlib::vector<double, 2l>) -> None
    __init__(self: dlib.point, v: numpy.ndarray[int64]) -> None
    __init__(self: dlib.point, v: numpy.ndarray[float32]) -> None
    __init__(self: dlib.point, v: numpy.ndarray[float64]) -> None
    normalize(self: dlib.point) → dlib::vector<double, 2l>
        Returns a unit normalized copy of this vector.
    
    x
        The x-coordinate of the point.
    y
        The y-coordinate of the point.
class dlib.points
    An array of point objects.

    __init__(*args, **kwargs)
        Overloaded function.

    __init__(self: dlib.points) -> None
    __init__(self: dlib.points, arg0: dlib.points) -> None
        Copy constructor

    __init__(self: dlib.points, arg0: iterable) -> None
    __init__(self: dlib.points, initial_size: int) -> None
    append(self: dlib.points, x: dlib.point) → None
        Add an item to the end of the list

    clear(self: dlib.points) → None
    count(self: dlib.points, x: dlib.point) → int
        Return the number of times x appears in the list

    extend(*args, **kwargs)
        Overloaded function.

    extend(self: dlib.points, L: dlib.points) -> None
        Extend the list by appending all the items in the given list

    extend(self: dlib.points, arg0: list) -> None
    insert(self: dlib.points, i: int, x: dlib.point) → None
    Insert an item at a given position.

    pop(*args, **kwargs)
        Overloaded function.

    pop(self: dlib.points) -> dlib.point
        Remove and return the last item

    pop(self: dlib.points, i: int) -> dlib.point
        Remove and return the item at index i

    remove(self: dlib.points, x: dlib.point) → None
        Remove the first item from the list whose value is x. It is an error if there is no such item.

    resize(self: dlib.points, arg0: int) → None
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dlib是一个C++开源库,主要用于机器学习和计算机视觉任务。其中,dlib中的人脸检测模块可以在图像或视频中识别出人脸,而人脸关键点检测功能可以在人脸上定位出一些重要的特征点,例如眼睛、鼻子、嘴巴等,以便进行更深入的人脸分析和处理。 dlib人脸关键点检测的原理基于基于人脸形状模型(Face Shape Model)和级联回归分类器(Cascade Regression Classifier)。 首先,dlib人脸检测模块使用级联分类器从图像或视频中识别出人脸。然后,对于每个检测到的人脸dlib使用形状模型来定位人脸上的关键点。 形状模型是一个基于训练数据的统计模型,它描述了人脸上的关键点相对于人脸的平均形状的变化。通过对大量人脸数据进行训练,可以得到一个形状模型,它可以在新的人脸图像中自动定位关键点。 然而,由于不同人脸之间的差异很大,形状模型在某些情况下可能无法准确地定位关键点。为了解决这个问题,dlib使用了级联回归分类器,该分类器可以对形状模型的输出进行微调。 级联回归分类器是一个多层神经网络,每一层都对前一层的输出进行微调,最终输出关键点的坐标。通过多层级联回归分类器的迭代,可以使得关键点的定位更加准确。 综上所述,dlib人脸关键点检测原理是基于形状模型和级联回归分类器的组合。形状模型用于初步定位关键点,级联回归分类器用于进一步微调关键点的位置,从而实现更准确的关键点检测

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值