使用dlib提取图像中人的嘴部

概念

dlib可以检测图像中的人脸,并且可以检测出人脸上的68个关键点,其中后20个点表示了唇部的关键点,因此可以使用dlib检测人脸并通过嘴部关键点得到嘴部图像。

代码

import dlib
import cv2
import numpy as np
from PIL import Image
# 加载面部检测模型
predictor_path = '.\\shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
# 检测面部并选择
faces = detector(img, 1)
face = faces[0]
# 检测关键点并选择唇部
points = predictor(img, face)
mouth_points = [(point.x, point.y) for point in points.parts()[48:]]
# 截取唇部图像
center = np.mean(mouth_points, axis=0).astype(int)
rect = cv2.boundingRect(np.array(mouth_points))
ratio = rect[2] / rect[3]
# 给定输出规模shape=[width, height]
if shape[0] / shape[1] > ratio:
  shape[0], shape[1] = shape[0] / shape[1] * rect[3], rect[3]
else:
  shape[0], shape[1] = rect[2], shape[1] / shape[0] * rect[2]
mouth_img = img[int(center[1] - shape[1] / 2):int(center[1] + shape[1] / 2),
             int(center[0] - shape[0] / 2):int(center[0] + shape[0] / 2)]
# 保存图像
Image.fromarray(mouth_img).save("img_path")

参考:张宏伦的知乎专栏

Lei Yang
Lei Yang
PhD candidate

My research interests include visual speech recognition and semantics segmentation.