python--合影照片中多人脸检测、分离、存储、识别综合应用

一时对人脸识别发生了兴趣,这几天踩了不少坑,主要坑是在dlib的安装和参考网上代码时遇到或多或少代码错误。网上关于人脸检测、识别的代码很多,我采用了其中之一的方法,并综合应用,适合初学者入门学习,欢迎交流。
应用环境:window 7 、python3.5
shape_predictor_68_face_landmarks.dat 需在网上下载
dlib_face_recognition_resnet_model_v1.dat 需在网上下载

import dlib
import matplotlib.pyplot as plt
import numpy as np
import math,cv2
import os, glob,math
from skimage import io

def resize(image, width=1200):  # 将待检测的image进行resize
    r = width * 1.0 / image.shape[1]
    dim = (width, int(image.shape[0] * r))
    resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
    return resized

def shape_to_np(shape, dtype="int"): # 将包含68个特征的的shape转换为numpy array格式
    coords = np.zeros((68, 2), dtype=dtype)
    for i in range(0, 68):
        coords[i] = (shape.part(i).x, shape.part(i).y)
    return coords
"""
人脸特征点检测
"""
def rect_to_bb(rect): # 获得人脸矩形的坐标信息
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    return (x, y, w, h)

"""
人脸对齐
"""
def face_alignment(faces):
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 用来预测关键点
    faces_aligned = []
    for face in faces:
        rec = dlib.rectangle(0,0,face.shape[0],face.shape[1])
        shape = predictor(np.uint8(face),rec) # 注意输入的必须是uint8类型
       
        order = [36,45,30,48,54] # left eye, right eye, nose, left mouth, right mouth  注意关键点的顺序,这个在网上可以找
        for j in order:
            x = shape.part(j).x
            y = shape.part(j).y
            #cv2.circle(face, (x, y), 2, (0, 0, 255), -1)    #测试加还是不加

        eye_center =((shape.part(36).x + shape.part(45).x) * 1./2, # 计算两眼的中心坐标
                      (shape.part(36).y + shape.part(45).y) * 1./2)
        dx = (shape.part(45).x - shape.part(36).x) # note: right - right
        dy = (shape.part(45).y - shape.part(36).y)

        angle = math.atan2(dy,dx) * 180. / math.pi # 计算角度
        RotateMatrix = cv2.getRotationMatrix2D(eye_center, angle, scale=1) # 计算仿射矩阵
        RotImg = cv2.warpAffine(face, RotateMatrix, (face.shape[0], face.shape[1])) # 进行放射变换,即旋转
        faces_aligned.append(RotImg)
    return faces_aligned

def feature(path,foces):
    im_raw =cv2.imread(foces).astype('uint8')   
    detector = dlib.get_frontal_face_detector()
    gray = cv2.cvtColor(im_raw, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)
    src_faces = []
    for (i, rect) in enumerate(rects):
        (x, y, w, h) = rect_to_bb(rect)
        detect_face = im_raw[y:y+h,x:x+w]
        src_faces.append(detect_face)
        cv2.rectangle(im_raw, (x, y), (x + w, y + h), (0, 255, 0), 2)
        cv2.putText(im_raw, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    faces_aligned = face_alignment(src_faces)
    #cv2.imshow("src", im_raw)    
    for j in os.listdir(path):                 #清空拟装合影照片中分离人脸目录中的文件
        os.remove(path+'\\'+j)         
    i = 0
    for face in faces_aligned:
        #cv2.imshow("det_{}".format(i), face)
        i = i + 1        
        io.imsave(path+'\\'+'Face{}.jpg'.format(i),face)
    cv2.imshow("Output", im_raw)
    cv2.waitKey(0)

def predict(descriptors,path):
    # 对需识别的人脸进行同样处理
    # 提取描述子
    img = io.imread(path)
    dets = detector(img, 1)
    dist = []
    for k, d in enumerate(dets):
        shape = sp(img, d)
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        # 转换为numpy array
        d_test = np.array(face_descriptor)
        # 计算欧式距离
        for i in descriptors:
            dist_ = np.linalg.norm(i-d_test)
            dist.append(dist_)
    return dist

def create_face_space( faces_folder_path ):
    # 对文件夹下的每一个人脸进行:
    # 1.人脸检测
    # 2.关键点检测
    # 3.描述子提取
    # 已知人脸描述子list
    descriptors = []
    for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
        #print("Processing file: {}".format(f))
        img = io.imread(f)
        # 1.已知人脸检测
        dets = detector(img, 1)
        #print("Number of faces detected: {}".format(len(dets)))
        for k, d in enumerate(dets):
            # 2.关键点检测
            shape = sp(img, d)
            # 3.提取描述子,128D向量
            face_descriptor = facerec.compute_face_descriptor(img, shape)
            # 转换为numpy array
            v = np.array(face_descriptor)
            descriptors.append(v)
    return descriptors

def demo(path,faces_folder_path):
    global detector, sp, facerec
    # 加载正脸检测器
    detector = dlib.get_frontal_face_detector()
    # 加载人脸关键点检测器
    sp = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    # 3. 加载人脸识别模型
    facerec = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat") 
    descriptors = create_face_space(faces_folder_path)
    # 存放在已知人脸目录中的照片对应的人员名单
    candidate = ['刘德华','巩俐','范冰冰','奥巴马','张文']
    for f in glob.glob(os.path.join(path, "*.jpg")):
        dist = predict(descriptors, f)
        #print(dist)
        # 候选人和距离组成一个dict
        c_d = dict(zip(candidate, dist))
        cd_sorted = sorted(c_d.items(), key=lambda d:d[1])
        #print( cd_sorted )     
        if  len(cd_sorted) !=0 and cd_sorted[0][1]<0.6: #比对值,越小越准确但会有已知人认不出
            print ("此人_{} 是: ".format(f),cd_sorted[0][0])           
        else:
            print ("不知此人_{} 是谁: ".format(f))
if __name__ == "__main__":
    foces=r'C:\Users\Administrator\Desktop\123.jpg'   #合影照片
    path = r'examples\test'          #暂存合影照片中分离人脸的子目录
    faces_folder_path =r'examples\trains' #已知人脸的子目录
    feature(path,foces)
    demo(path, faces_folder_path)

你可能感兴趣的:(网上收集python相关资料,python,计算机等级考试)