python package dlib login authentication model face recognition system

1, directly on the dry

# ! / Usr / bin / Python 
# - * - Coding: UTF-8 - * - 
Import Time
 Import dlib
 Import numpy AS NP
 class faceDiscernModel:
     DEF  __init__ (Self):
         # Load pre-trained face detection CNN model 
        self.cnn_face_model = " ./model/mmod_human_face_detector.dat " 
        self.cnn_face_detector = dlib.cnn_face_detection_model_v1 (self.cnn_face_model) 

        # loading facial feature detection model 
        self.predictor_path = " ./model/shape_predictor_5_face_landmarks.dat " 
        self.predictor =dlib.shape_predictor (self.predictor_path) 

        # load the facial feature extraction model 
        self.featureModel = " ./model/dlib_face_recognition_resnet_model_v1.dat " 
        self.feater = dlib.face_recognition_model_v1 (self.featureModel) 

    DEF get_faces_feature (Self, imgpath): 

        # Reading face images taken 
        IMG = dlib.load_rgb_image (imgpath) 

        feat. = [] 

        # detect each face a bounding box 
        Dets = self.cnn_face_detector (IMG,. 1 ) 

        # len (Dets) is the number of detected human faces 
        for I, D in the enumerate (dets):
             # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
            #     i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))

            # 检测 box i 内的人脸关键点
            shape = self.predictor(img, d.rect)

            # 计算特征向量
            face_descriptor = self.feater.compute_face_descriptor(img, shape)

            feat.append(face_descriptor)

        return feat

    def Euclidean_distance_test(self,feature_1,feature_2):
        return np.sqrt(np.sum((np.array(feature_1)-np.array(feature_2))**2))

    def face_compare(self,imgPath_1,imgPath_2,assess=0.6):
        feature_1 = self.get_faces_feature(imgPath_1)
        feature_2 = self.get_faces_feature(imgPath_2)
        score = self.Euclidean_distance_test(feature_1=feature_1,feature_2=feature_2)
        if score > assess:
            return False
        elif 0< score < assess:
            return True
        else:
            return False


if __name__ == '__main__':

    imgPath_1 = "./faceImage/test.jpg"
    imgPath_2 = "./faceImage/test1.jpg"
    faceDiscern = faceDiscernModel()
    st = time.time()
    result = faceDiscern.face_compare(imgPath_1,imgPath_2)

    print(time.time()-st)
    print(result)

2, model Download

  http://dlib.net/files/

Guess you like

Origin www.cnblogs.com/wuzaipei/p/11031760.html