# 批量人脸裁剪代码
import os
import cv2
import dlib
def FaceClip(fliePath, output_image_path):
predictor_model = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector () # dlib人脸检测器
predictor = dlib.shape_predictor (predictor_model)
imagefile = os.listdir (filePath)
print ('Images need to be processed: '+str(len (imagefile)))
for file in os.listdir (filePath):
print ('Processing file name: '+str(file))
test_image_path = filePath
print ('Processing file path: '+str(test_image_path))
# cv2读取图像
img = cv2.imread (str (test_image_path) + '\\' + str (file))
img = cv2.cvtColor (img, cv2.COLOR_BGR2RGB)
# 人脸数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]))
for i in range (len (rects)):
faces.append (predictor (img, rects[i]))
face_images = dlib.get_face_chips (img, faces, size=320)
for image in face_images:
cv_bgr_img = cv2.cvtColor (image, cv2.COLOR_RGB2BGR)
cv_bgr_img = cv2.resize (cv_bgr_img, (112, 112))
output_path = os.path.join (output_image_path, file)
print ('Output images path: '+str(output_path))
cv2.imwrite (str (output_path), cv_bgr_img)
if __name__ == '__main__':
filePath = 'E:\RealLifeMaskedFaceDataset\split\withoutmasktest'
output_image_path = 'E:\RealLifeMaskedFaceDataset\split\outputtest'
FaceClip (filePath, output_image_path)
批量FaceAlignment using dlib
最新推荐文章于 2025-04-09 14:15:04 发布