基于python结合dlib实现人脸识别【附源码】

文章目录

  • 前言
  • 代码实现
    • 引入库
    • 定义检测器的类
    • 加载本地图片
    • 检测人脸及判断
  • 总结


前言

本文实现基于python调用dlib库,并利用欧氏距离计算人脸的特征分析得出属于哪个人


代码实现

引入库

import os,dlib,glob,numpy,time,cv2,re,json
from PIL import ImageFont, ImageDraw, Image

定义检测器的类

 self.leave = 2
 self.faces_folder_path = faces_folder_path        # 候选人脸文件夹
 self.detectionPath = detection_path
 if detection_path is None: # 加载正脸检测器
     self.detector = dlib.get_frontal_face_detector()
 else:
     self.detector = dlib.cnn_face_detection_model_v1(detection_path)
 self.sp = dlib.shape_predictor(predictor_path)                      # 加载人脸关键点检测器
 self.facerec = dlib.face_recognition_model_v1(face_rec_model_path)  # 加载人脸识别模型

加载本地图片

此过程主要是加载一张图片得到标准人脸的特征,用于在检测过程中区分人脸

if self.PeronFile is None or os.path.exists(self.PeronFile)==False:
   self.dataDBDict={}
    for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
        n = re.findall(r"\\(.*?)\.jpg",f)[0]
        self.dataDBDict[n]=None
        img = cv2.imdecode(numpy.fromfile(f, dtype=numpy.uint8), 1)
        dets = self.detector(img, self.leave)
        if len(dets) == 1:
            for k, d in enumerate(dets):
                if self.detectionPath is not None:
                    d = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom())
                shape = self.sp(img, d)
                face_descriptor = self.facerec.compute_face_descriptor(img, shape)
                v = numpy.array(face_descriptor)
                self.dataDBDict[n] = v.tolist()
                print('加载数据成功:%s'%n)
        else:
            print('加载数据失败:%s'%n)
    with open('./data/Person.json', 'w', encoding='utf-8') as f:
        f.write(json.dumps(self.dataDBDict, sort_keys=True, indent=4, separators=(',', ': ')))
else:
    print('加载数据文件')
    with open(self.PeronFile, 'r', encoding='utf-8') as json_file:
        self.dataDBDict = json.load(json_file)

检测人脸及判断

def predict(self,img):
    res_data = []
    dets = self.detector(img, self.leave)
    if len(dets)<=0:
        return res_data
    for k, d in enumerate(dets):
        if self.detectionPath is not None:
            d = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom())
        each_face = {}
        each_face['rectange'] = (d.left(),d.top(),d.right(),d.bottom())
        shape = self.sp(img, d)
        face_descriptor = self.facerec.compute_face_descriptor(img, shape)
        d_test = numpy.array(face_descriptor)
        dist = []
        for key,value in self.dataDBDict.items():
            dist_ = numpy.linalg.norm(numpy.array(value) - d_test)
            dist.append(dist_)

        c_d = dict(zip(list(self.dataDBDict.keys()), dist))
        cd_sorted = sorted(c_d.items(), key=lambda d: d[1])

        each_face['name'] = "UnKnow" if (1-cd_sorted[0][1]) < self.score else cd_sorted[0][0]
        each_face['score'] = 1-cd_sorted[0][1]
        res_data.append(each_face)
    return res_data

总结

完整代码实现

import os,dlib,glob,numpy,time,cv2,re,json
from PIL import ImageFont, ImageDraw, Image

class FaceCheck:
    def __init__(self,detection_path=None,predictor_path=None,face_rec_model_path=None,faces_folder_path=None,score=0.4,PeronFile=None):
        self.leave = 2
        self.faces_folder_path = faces_folder_path        # 候选人脸文件夹
        self.detectionPath = detection_path
        if detection_path is None: # 加载正脸检测器
            self.detector = dlib.get_frontal_face_detector()
        else:
            self.detector = dlib.cnn_face_detection_model_v1(detection_path)
        self.sp = dlib.shape_predictor(predictor_path)                      # 加载人脸关键点检测器
        self.facerec = dlib.face_recognition_model_v1(face_rec_model_path)  # 加载人脸识别模型

        self.score = score
        self.PeronFile = PeronFile
        if self.PeronFile is None or os.path.exists(self.PeronFile)==False:
            self.dataDBDict={}
            for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
                n = re.findall(r"\\(.*?)\.jpg",f)[0]
                self.dataDBDict[n]=None
                img = cv2.imdecode(numpy.fromfile(f, dtype=numpy.uint8), 1)
                dets = self.detector(img, self.leave)
                if len(dets) == 1:
                    for k, d in enumerate(dets):
                        if self.detectionPath is not None:
                            d = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom())
                        shape = self.sp(img, d)
                        face_descriptor = self.facerec.compute_face_descriptor(img, shape)
                        v = numpy.array(face_descriptor)
                        self.dataDBDict[n] = v.tolist()
                        print('加载数据成功:%s'%n)
                else:
                    print('加载数据失败:%s'%n)
            with open('./data/Person.json', 'w', encoding='utf-8') as f:
                f.write(json.dumps(self.dataDBDict, sort_keys=True, indent=4, separators=(',', ': ')))
        else:
            print('加载数据文件')
            with open(self.PeronFile, 'r', encoding='utf-8') as json_file:
                self.dataDBDict = json.load(json_file)

    def getPicture(self,CameraID:int=0):
        cap = cv2.VideoCapture(CameraID)
        while True:
            ret, img = cap.read()
            cv2.imshow("Camera",img)
            if cv2.waitKey(1) & 0xFF == ord('p'):
                name = input("请输入名字:")
                break
        cap.release()
        cv2.destroyAllWindows()

        dets = self.detector(img, self.leave)
        if len(dets)!=1:
            print("载入数据库失败")
        cv2.imwrite(self.faces_folder_path +"/%s.jpg" % name, img)
        self.dataDBDict[name] = None
        for k, d in enumerate(dets):
            if self.detectionPath is not None:
                d = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom())
            shape = self.sp(img, d)
            face_descriptor = self.facerec.compute_face_descriptor(img, shape)
            v = numpy.array(face_descriptor)
            self.dataDBDict[name] = v.tolist()
            print('加载数据成功:%s'%name)
        with open(self.PeronFile if self.PeronFile is not None else './data/Person.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(self.dataDBDict, sort_keys=True, indent=4, separators=(',', ': ')))

    def predict(self,img):
        res_data = []
        dets = self.detector(img, self.leave)
        if len(dets)<=0:
            return res_data
        for k, d in enumerate(dets):
            if self.detectionPath is not None:
                d = dlib.rectangle(d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom())
            each_face = {}
            each_face['rectange'] = (d.left(),d.top(),d.right(),d.bottom())
            shape = self.sp(img, d)
            face_descriptor = self.facerec.compute_face_descriptor(img, shape)
            d_test = numpy.array(face_descriptor)
            dist = []
            for key,value in self.dataDBDict.items():
                dist_ = numpy.linalg.norm(numpy.array(value) - d_test)
                dist.append(dist_)

            c_d = dict(zip(list(self.dataDBDict.keys()), dist))
            cd_sorted = sorted(c_d.items(), key=lambda d: d[1])

            each_face['name'] = "UnKnow" if (1-cd_sorted[0][1]) < self.score else cd_sorted[0][0]
            each_face['score'] = 1-cd_sorted[0][1]
            res_data.append(each_face)
        return res_data

if __name__ == '__main__':
    ctu = FaceCheck(detection_path='./data/dat/detection.dat', predictor_path='./data/dat/predictor.dat', face_rec_model_path='./data/dat/model.dat', faces_folder_path='./data/database', score=0.4, PeronFile='./data/Person.json')
    cap = cv2.VideoCapture(0)
    while True:
        ret, img = cap.read()
        # img_t = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        # h, s, v = cv2.split(img_t)
        # v1 = numpy.clip(cv2.add(1 * v, 30), 0, 255)
        # img = numpy.uint8(cv2.merge((h, s, v1)))
        # img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
        predict_res = ctu.predict(img)
        for each_Face in predict_res:
            cv2.rectangle(img, (each_Face['rectange'][0], each_Face['rectange'][1]), (each_Face['rectange'][2], each_Face['rectange'][3]), (0, 255, 0), 1)

            label = "{}, {}".format(each_Face['name'], each_Face['score'])
            cv2img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            pilimg = Image.fromarray(cv2img)
            draw = ImageDraw.Draw(pilimg)
            font = ImageFont.truetype('./data/font/simfang.ttf', 18, encoding="utf-8")
            draw.text((each_Face['rectange'][0], each_Face['rectange'][1] - 18), label, (255, 0, 0), font=font)
            img = cv2.cvtColor(numpy.array(pilimg), cv2.COLOR_RGB2BGR)
        print(predict_res)
        cv2.imshow('Camrea', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

你可能感兴趣的:(人工智能-人脸识别,python,numpy,开发语言)