python通过Dlib库实现人脸68点特征点标记
1、使用到的库import cv2# 图像处理库import dlib# 人脸识别库from skimage import io# 图像处理库2、代码片段详解2.1 dlib.get_frontal_face_detector()功能:人脸检测画框参数:无返回值:默认的人脸检测器det...
1、使用到的库
import cv2 # 图像处理库
import dlib # 人脸识别库
from skimage import io # 图像处理库
2、代码片段详解
2.1 dlib.get_frontal_face_detector()
功能:人脸检测画框
参数:无
返回值:默认的人脸检测器
detector = dlib.get_frontal_face_detector()
print(detector)
# <dlib.fhog_object_detector object at 0x0000026536162F70>
2.2 dlib.shape_predictor()
功能:标记人脸关键点
参数:shape_predictor_68_face_landmarks.dat:68个关键点模型地址
返回值:人脸关键点预测器
下载链接:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
print(predictor)
# <dlib.shape_predictor object at 0x00000265361F77B0>
2.3 io.imread()
skimage.io.imread: 直接返回numpy.ndarray 对象,通道顺序为RGB(注意cv2.imread()生成的是BGR),通道值默认范围0-255。
img = cv2.imread("ma_si_ke.jpg") # RGB
print(img)
[[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
...
[[43 37 30]
[42 36 29]
[41 35 28]
...
[ 9 4 5]
[ 9 4 5]
[ 9 4 5]]
[[43 37 30]
[42 36 29]
[41 35 28]
...
[ 9 4 5]
[ 9 4 5]
[ 9 4 5]]
[[43 37 30]
[42 36 29]
[41 35 28]
...
[ 9 4 5]
[ 9 4 5]
[ 9 4 5]]]
2.4 cv2.cvtColor()
cv2.cvtColor(p1,p2) 是颜色空间转换函数,p1是需要转换的图片,p2是转换成何种格式。
cv2.COLOR_BGR2RGB 将BGR格式转换成RGB格式
cv2.COLOR_BGR2GRAY 将BGR格式转换成灰度图片
2.5 detector(img, 0)
dets = detector(img, 0)
功能:对图像画人脸框
参数:img:输入的图片
返回值:人脸检测矩形框4点坐标
# 特征提取器的实例化
dets = detector(img, 0)
print(dets)
print("人脸数:", len(dets))
# rectangles[[(62, 62) (211, 211)]]
# 人脸数: 1
2.6 predictor(img, d)
shape = predictor(img, d)
功能:定位人脸关键点
参数:img:一个numpy ndarray,包含8位灰度或RGB图像
d:开始内部形状预测的边界框
返回值:68个关键点的位置
shape = predictor(img, d)
print(shape)
# <dlib.full_object_detection object at 0x0000025923EB1E30>
2.7 cv2.circle()
cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 1)
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
参数说明
img:输入的图片data
center:圆心位置
radius:圆的半径
color:圆的颜色
thickness:圆形轮廓的粗细(如果为正)。负厚度表示要绘制实心圆。
lineType: 圆边界的类型。
shift:中心坐标和半径值中的小数位数。
2.8 cv2.putText()
cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
文本内容,起点坐标,字体,字体大小,颜色,线宽,线类型(cv2.LINE_AA)
2.9 cv2.imshow()
函数cv2.imshow() 显示图像,窗口会自动调整为图像大小。第一个参数是窗口的名字,其次才是我们的图像。你可以创建多个窗口,只要你喜欢,但是必须给他们不同的名字。
cv2.imshow('face_detector_68', img)
2.10 cv2.waitKey(0)
3、实现代码
import cv2 # 图像处理库
import dlib # 人脸识别库
from skimage import io # 图像处理库
# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
# dlib的68点模型,使用官方训练好的特征预测器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 图片所在路径
img = cv2.imread("ma_si_ke.jpg") # RGB
# img = cv2.imread() # BGR
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# 特征提取器的实例化
dets = detector(img, 0)
print("人脸数:", len(dets))
for k, d in enumerate(dets):
print("第", k+1, "个人脸d的坐标:",
"left:", d.left(),
"right:", d.right(),
"top:", d.top(),
"bottom:", d.bottom())
width = d.right() - d.left()
heigth = d.bottom() - d.top()
print('人脸面积为:',(width*heigth))
# 利用预测器预测
shape = predictor(img, d)
# print(shape)
# 标出68个点的位置
for i in range(68):
cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 1)
cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
# 显示一下处理的图片,然后销毁窗口
cv2.imshow('face_detector_68', img)
cv2.waitKey(0)
4、成果展示
更多推荐
所有评论(0)