dlib在人脸关键点检测中的应用

概述

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

关键点检测实现方法

实现步骤

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

代码示例

本实例中,使用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


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空