python计算机视觉

【OPENCV/Dlib/Firebase】人脸关键点定位及配准

2018-09-16  本文已影响743人  ItchyHiker

Tags: ComputerVision DIP

人脸检测和关键点定位是人脸识别中和其他人脸相关应用中的基础信息和先决条件。比如检测五官位置在相机中加入配饰,使用关键点信息进行人脸配准等等... 这篇文章讨论几种常见的人脸检测和关键点定位模型。
人脸卡通拼接:


image

人脸配准:


image.png

OPENCV 人脸检测

OpenCV是目前最为强大的计算机视觉库,可以在这个基础上进行很多二次开发。OpenCV的人脸检测是基于Haar特征的Adaboost级联分类器。内置了两个模型: frontal_face, eye。
opencv提供的人脸检测模型只能检测出大概的矩形位置,功能较弱。

import cv2
import os
import time
filepath = "/Users/yuhua.cheng/PycharmProjects/FaceDetector/data/"

img = cv2.imread(filepath + "face.png")
height, width, channels = img.shape
rescale = 0.5
grayImg = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)

faceClassifier = cv2.CascadeClassifier(filepath + "haarcascade_frontalface_default.xml")
eyeClassifier = cv2.CascadeClassifier(filepath + "haarcascade_eye_tree_eyeglasses.xml")
color = (0,255,0)

faceRects = faceClassifier.detectMultiScale(grayImg, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))


time_start = time.time()
if len(faceRects):
    for faceRect in faceRects:
        x, y, w, h = faceRect
        # face
        cv2.rectangle(img, (x,y), (x+h, y+w), color, 2)
        roi_gray = grayImg[y:y+h, x:x+w]
        roi_img = img[y:y+h, x:x+w]
        # eyes
        eyes = eyeClassifier.detectMultiScale(roi_gray)
        for eye in eyes:
            ex, ey, ew, eh = eye
            cv2.rectangle(roi_img, (ex, ey), (ex+ew, ey+eh), color, 2)
time_end = time.time()

print("It tooks {} to detect with opencv model!".format(time_end - time_start))
img = cv2.resize(img, (int(width*rescale), int(height*rescale)))
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Screen Shot 2018-12-25 at 2.13.10 PM.png

Dlib关键点定位及人脸配准

Dlib是一个C++库,除了图像处理的功能也提供了很多其他功能,比如机器学习等。也可以进行二次开发。
Dlib人脸关键点检测模型的点位信息:


wYrNO.jpg

Dlib点位比较全,利用这些点位信息可以框选出脸部轮廓,眼镜,嘴巴等五官。很多现有的人脸形状分类模型就是利用dlib的点位信息进一步构造特征做的。此外这些信息还可以用于2D人脸校正,具体就是将倾斜的人脸变为正脸,使得人脸处于图像中心位置。

import cv2
import dlib
import time
import numpy as np

filepath = "/Users/yuhua.cheng/PycharmProjects/FaceDetector/data/"

img = cv2.imread(filepath + "face.png")
height, width, channels = img.shape
rescale = 0.5
grayImg = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)

# get face detectors
faceDetector = dlib.get_frontal_face_detector()
# get landmark detectors
landmarkPredictor = dlib.shape_predictor(filepath + "shape_predictor_68_face_landmarks.dat")

faces = faceDetector(grayImg, 1)
time_start = time.time()
for face in faces:
    landmarks = landmarkPredictor(img, face)  # get 68 landmarks for each face
    print(len(landmarks.parts()))
    # 遍历所有点,打印出其坐标,并圈出来
    for pt in landmarks.parts():
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)

# add poly
"""
coordinates = landmarks.parts()

pts = np.zeros((9,2), np.int32)
mask = np.zeros((height, width), np.uint8)

points = [coordinates[15], coordinates[14], coordinates[13], coordinates[12], coordinates[11], coordinates[10], coordinates[54], coordinates[35], coordinates[42]]
for i, point in enumerate(points):
    pts[i][0] = point.x
    pts[i][1] = point.y
    print(pts)

cv2.fillConvexPoly(mask, pts, color=(255,255,255))
cv2.imshow("mask", mask)
cv2.imwrite("../result/mask.jpg", mask)


cv2.fillConvexPoly(img, pts, color=(0, 255, 0))
"""
time_end = time.time()
print("It tooks {} to detect with dlib model!".format(time_end - time_start))
img = cv2.resize(img, (int(width*rescale), int(height*rescale)))

cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()
Screen Shot 2018-12-25 at 2.19.29 PM.png
简单介绍一下人脸校正:
有了上面的关键点信息之后,我们可以使用这些关键点信息来判断人脸处于图像中的位置和倾斜的角度。比较简单的一种方法是根据两个眼镜的相对位置和大小来校正。
一般而言校正之后左眼中心在图像0.2-0.4处,相对应右眼应该在0.6-0.8处,值越大则最后得到的人脸在图像中的占比越小。(脑补下左眼在0.2位置处时,左眼最左侧就已经是图像边缘了,相当于脸部占满图像)
同时所有左右眼部关键点位置可以用来判断脸在平面内的倾斜程度。
image
知道倾斜角度,又知道校正前后的点位位置关系,我们就可以求解一个仿射变换矩阵,最后将这个仿射变换矩阵用于整个图像就可以得到校正后的人脸图像。
具体代码可以参考这篇文章:https://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/

Firebase 点位信息

Firebase 是google 提供的免费sdk,功能很强大。其中的ML Kit Vision提供人脸识别功能,其中关键点检测如下:


Screen Shot 2018-12-25 at 11.05.14 AM.png

得到这些关键点同样可以和dlib一样进行人脸校正。
我用Firebase实现的人脸校正效果如下:


Screen Shot 2018-12-25 at 3.23.58 PM.png

我自己仔细对比了下发现Firebase的关键点虽然多,但是精度不如dlib的关键点精度

Reference

  1. https://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/
上一篇下一篇

猜你喜欢

热点阅读