**
一、dlib.get_frontal_face_detector()函数返回值
**
dlib.get_frontal_face_datector(PythonFunction,in Classes)
返回值是<class ‘dlib.dlib.rectangle’>,就是一个矩形
坐标为[(x1, y1) (x2, y2)]
可以通过函数的left,right,top,bottom方法分别获取对应的x1, x2, y1, y2值:
import cv2
import dlib
img = cv2.imread('my_image.jpg')
detector = dlib.get_frontal_face_detector()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_img, 1)
for i, d in enumerate(dets):
print(type(d))
y1 = d.top() if d.top() > 0 else 0
y2 = d.bottom() if d.bottom() > 0 else 0
x1 = d.left() if d.left() > 0 else 0
x2 = d.right() if d.right() > 0 else 0
enumerate函数的返回值是迭代对象的索引和对应值
i:矩形索引
d:矩形坐标值
最后打印一下就会得到相对应的值。这里要说明一下,当使用d.area()函数计算面积的时候,数值是包含起始点的坐标的:
x = x2 - x1 + 1
y = y2 - y1 + 1
d.area() = x * y
**
二、predictor = dlib.shape_predictor()关键点预测器用法
**
predictor = dlib.shape_predictor(‘data/data_dlib/shape_predictor_68_face_landmarks.dat’)
功能:标记人脸关键点
参数:‘data/data_dlib/shape_predictor_68_face_landmarks.dat’:68个关键点模型地址
返回值:人脸关键点预测器
shape = predictor(img, box)
功能:定位人脸关键点
参数:img:一个numpy ndarray,包含8位灰度或RGB图像
box:开始内部形状预测的边界框
返回值:68个关键点的位置