计算机处理人脸,先要找到face在哪。怎么找?就是检测landmark。landmark是什么意思呢?就是在脸上绘制的若干标记点,标记点一般画在边、角、轮廓、交叉、等分等关键位置,借助它们就可以描述人脸的形态(shape).
现在常用的开源landmark检测工具是dlib,其开源模型中的landmark包含68个点,按顺序来说: 0-16是下颌线(红),17-21是右眼眉(橙),22-26是左眼眉(黄),27-35是鼻子(浅绿),36-41是右眼(深绿),42-47是左眼(浅蓝),48-60是嘴外轮廓(深蓝),61-67是嘴内轮廓(紫)。
那么dlib是如何做到的呢?算法是基于histogram of oriented gradients (HOG) 和linear classifier,论文是这篇:One Millisecond Face Alignment with an Ensemble of Regression Trees by# Vahid Kazemi and Josephine Sullivan, CVPR 2014, 以后会研究一下。其开源model(shape_predictor_68_face_landmarks)是用300w的图片库训练的( iBUG 300-W face landmark dataset),直接拿来用就是了。
pip install opencv-python
pip install dlib
pip install imutils
dlib有python的封装,所以用起来非常简单。shape_predictor_68_face_landmarks.dat是模型,可以直接下载(http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2),get_facial_landmarks函数返回一个68*2的numpy array就是landmark了。
import dlib
# import face_utils
import cv2
import numpy as np
from imutils import face_utils
from pathlib import Path
import os
here = os.path.dirname(os.path.abspath(__file__))
print(here)
predictor_path = os.path.join(here, 'shape_predictor_68_face_landmarks.dat')
print(predictor_path)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
def get_facial_landmarks(image):
# detect face(s)
dets = detector(image, 1)
shape = np.empty([1,1])
for k, d in enumerate(dets):
# Get the landmarks/parts for the face in box d.
shape = predictor(image, d)
# 遍历所有点
for pt in shape.parts():
# 绘制特征点
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 1, (255,0, 0), 2)
shape = face_utils.shape_to_np(shape)
return shape
img = cv2.imread(os.path.join(here, 'abama.jpg'))
landmarks = get_facial_landmarks(img)
print(landmarks.shape, landmarks)
cv2.imwrite(os.path.join(here, 'abama_landmark.jpg'), img)
# cv2.imshow('opencv_face_laowang',img) # 显示图片
# cv2.waitKey(0) # 等待用户关闭图片窗口
# cv2.destroyAllWindows()# 关闭窗口
参考:https://www.jianshu.com/p/122dbbde0ccf
https://blog.csdn.net/qq_44431690/article/details/106573853