批量FaceAlignment using dlib

# 批量人脸裁剪代码
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)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值