效果
→
参考资料和资源
GitHub - Onwaier/SegfaceAndAlignByDlib: 用dlib实现脸部分割和人脸对齐
shape_predictor_68_face_landmarks.dat 下载地址_shape_predictor_68_face_landmarks.dat下载-CSDN博客
未运行的参考资料 dlib实现脸部分割与人脸对齐 - 知乎
py代码
"""
代码功能:
1. 用dlib人脸检测器检测出人脸,返回的人脸矩形框
2. 对检测出的人脸进行关键点检测并切割出人脸
"""
import cv2
import dlib
import numpy as np
import matplotlib.pyplot as plt# In[]
imgname='1'
in_path = imgname+".jpg"
outpts = 100
out_path = imgname+'-face.jpg'
colorRGB=cv2.COLOR_BGR2RGB# In[]
# cv2读取图像
img = cv2.imread(in_path)#,cv2.IMREAD_COLOR)BGR阵列是蓝脸,需要转换成正常色
img = cv2.cvtColor(img, colorRGB)
plt.imshow(img);# In[]
predictor_model = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()# dlib人脸检测器
predictor = dlib.shape_predictor(predictor_model)
# In[]
# 人脸数rects
rects = detector(img, 0)
# faces存储full_object_detection对象
faces = dlib.full_object_detections()for i in range(len(rects)):faces.append(predictor(img,rects[i]))face_images = dlib.get_face_chips(img, faces, size=outpts)
for image in face_images:cv_bgr_img = cv2.cvtColor(image, colorRGB)cv2.imwrite(out_path, cv_bgr_img)