人脸识别切图

import sys
import os
import cv2
import dlib
import PIL.Image as Image


def cut_all_face(input_dir, output_dir):
    '''剪切所有的图片'''

    # 使用dlib自带的frontal_face_detector作为我们的特征提取器
    detector = dlib.get_frontal_face_detector()

    output_dir = output_dir + r'\all'
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    index = 1
    for path, dirnames, filenames in os.walk(input_dir):
        for filename in filenames:
            if filename.endswith('.jpg') or filename.endswith('.png'):
                print('正在处理图片 %s' % index)
            img_path = path + '/' + filename
            # 从文件读取图片
            img = cv2.imread(img_path)
            img1 = Image.open(img_path)
            # 转为灰度图片
            gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 使用detector进行人脸检测 dets为返回的结果
            dets = detector(gray_img, 1)

            # 使用enumerate 函数遍历序列中的元素以及它们的下标
            # 下标i即为人脸序号
            # left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
            # top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
            for i, d in enumerate(dets):
                # print(i, d)
                x1 = int(d.left()) if d.left() > 0 else 0
                y1 = int(d.top()) if d.top() > 0 else 0
                x2 = int(d.right()) if d.right() > 0 else 0
                y2 = int(d.bottom()) if d.bottom() > 0 else 0
                box = (x1 - 20, y1 - 20, x2 + 20, y2 + 20)
                # print(box)
                face = img1.crop(box)
                # 调整图片的尺寸
                face = face.resize((160, 160), Image.ANTIALIAS)
                # 保存图片
                face.save(output_dir + '\\' + str(index) + '.jpg', 'JPEG', quality=95)
                index += 1

            key = cv2.waitKey(30) & 0xff
            if key == 27:
                sys.exit(0)
    return output_dir


def difference(hist1, hist2):
    '''图片间的差异'''

    sum1 = 0
    for i in range(len(hist1)):
        if hist1[i] == hist2[i]:
            sum1 += 1
        else:
            sum1 += 1 - float(abs(hist1[i] - hist2[i])) / max(hist1[i], hist2[i])
    return sum1 / len(hist1)


def similary_calculate(path1, path2):
    '''图片处理'''

    # 预处理
    img1 = Image.open(path1).resize((256, 256)).convert('RGB')
    img2 = Image.open(path2).resize((256, 256)).convert('RGB')
    sum = 0
    for i in range(4):
        for j in range(4):
            hist1 = img1.crop((i * 64, j * 64, i * 64 + 63, j * 64 + 63)).copy().histogram()
            hist2 = img2.crop((i * 64, j * 64, i * 64 + 63, j * 64 + 63)).copy().histogram()
            sum += difference(hist1, hist2)
            # print difference(hist1, hist2)
    return sum / 16


def face_discern(path_test, path_train):
    '''根据图片间的相似度,筛选图片'''

    for root_train, directors_train, files_train in os.walk(path_train):
        j = 0
        for filename in files_train:
            file_path_train = os.path.join(root_train, filename)
            j += 1
            output_dir = r'C:\Users\Desktop\test' + '\\' + str(j)
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            for root, directors, files in os.walk(path_test):
                i = 0
                for filename in files:
                    filepath = os.path.join(root, filename)
                    if filepath.endswith(".png") or filepath.endswith(".jpg"):
                        remember = similary_calculate(file_path_train, filepath)
                        if remember > 0.55:
                            print(filepath)
                            img = Image.open(filepath)
                            img.resize((160, 160), Image.ANTIALIAS)
                            img.save(output_dir + '\\' + str(i) + '.jpg', 'JPEG', quality=95)
                            i += 1


if __name__ == '__main__':
    input_dir = r'C:\Users\Desktop\face'
    output_dir = r'C:\Users\Desktop\test'
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    path_test = cut_all_face(input_dir=input_dir, output_dir=output_dir)

    path_train = r'C:\Users\Desktop\train'
    face_discern(path_test=path_test, path_train=path_train)
l = []
l.a

 

你可能感兴趣的:(python)