Python中的人脸识别&打开简历

时间:2016-02-25 09:52:40

标签: python opencv face-recognition

我可以找到这些面孔并使用python将其保存在我的本地目录中,并按照以下代码从视频中打开cv



  UserFactory.register(username,password)
  .catch(function(err){
      // do something
  })
  .then(function(user) {
       $state.go('app.loggedin',{reload :true});
       console.log('Logged in', user);
  });




但是现在我想要识别那个在视频中面对的人......

我如何定义此人的身份?

喜欢扫描脸部并将其匹配到我的本地脸部数据库中,如果匹配,则给出名称等等

2 个答案:

答案 0 :(得分:1)

区分照片中的人并不是一项微不足道的任务,但有一些例子。正如Derman在之前的评论中所提到的,最好的方法是使用机器学习来教授程序不同的人面对的样子。一种方法是手动查找和提取人脸中的特征,例如眼睛之间的距离与眼睛和嘴之间的距离等。这虽然需要注意镜头失真和透视效果。有多篇研究论文讨论了最好的技术,比如本文使用来自一组面部的特征向量来找到最可能的匹配 Face Recognition Using Eigen Faces

有一个用于Python的机器学习工具箱,称为scikit-learn,它实现了对分类,回归,聚类等的支持。您可以使用它来训练神经网络并支持矢量机等。以下是如何使用带有scikit-learn和python的SVM实现Eigenface方法的完整示例: Complete implementation using Python

答案 1 :(得分:0)

您可以使用EigenFaceRecognizer或FisherFaceRecognizer或LBHP

所有这三种算法都是用python

构建的
# Create a recognizer object  
recognizer = cv2.face.createEigenFaceRecognizer()
# But Remember for EigenFaces all the images whether training or testing has to be of same shape
    #==========================================================================
    # get_images_and_labels function will give us list of images and list of labels to train our recognizer that we created in the first line
    # function requires the path of the directory where all the images is stored
    #===========================================================================
    def get_images_and_labels(path):
        # Append all the absolute image paths in a list image_paths
        image_paths = [os.path.join(path, f) for f in os.listdir(path) if not 
        f.endswith('.sad')]
        # images will contains face images
        images = []
        # labels will contains the label that is assigned to the image
        labels = []
        final_images = []
        largest_image_size = 0
        largest_width = 0
        largest_height = 0

        for image_path in image_paths:
           # Read the image and convert to grayscale
           image_pil = Image.open(image_path).convert('L')
           # Convert the image format into numpy array
           image = np.array(image_pil, 'uint8')
           # Get the label of the image
           nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
           # Detect the face in the image
           faces = faceCascade.detectMultiScale(image)
           # If face is detected, append the face to images and the label to labels

        for (x, y, w, h) in faces:
            images.append(image[y: y + h, x: x + w])
            labels.append(nbr)
            cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
            cv2.waitKey(50)
           # return the images list and labels list

        for image in images:
            if image.size > largest_image_size:
                largest_image_size = image.size
        largest_width, largest_height = image.shape

        for image in images:
            image = cv2.resize(image, (largest_width, largest_height), interpolation=cv2.INTER_CUBIC)
            final_images.append(image)

    return final_images, labels, largest_width, largest_height

#===================================================================
# Perform the tranining
# trainer takes two parameters as input
# first parameter is the list of images
# second parameter is a numpy array of their corresponding labels
#===================================================================
recognizer.train(images, np.array(labels)) # training takes as input the   list 



image_paths = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.sad')]
for image_path in image_paths:
    predict_image_pil = Image.open(image_path).convert('L')
    predict_image = np.array(predict_image_pil, 'uint8')
    faces = faceCascade.detectMultiScale(predict_image)
    for (x, y, w, h) in faces:
        result = cv2.face.MinDistancePredictCollector()
        predict_image = predict_image[y: y + h, x: x + w] 
        predict_image = cv2.resize(predict_image, (max_width, max_heigth), interpolation=cv2.INTER_CUBIC)

        # =========================================================  
        # predict method will give us the prediction
        # we will get the label in the next statement
        # predicted_image is the image that you want to recognize 
        # =========================================================  
        recognizer.predict(predict_image, result, 0) # this statement will give the prediction

        # ========================================== 
        # This statement below will give us label 
        # ========================================== 
        nbr_predicted = result.getLabel() 

        # ========================================== 
        # conf will tell us how much confident our recognizer is in it's prediction
        # ========================================== 
        conf = result.getDist()
        nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
        if nbr_actual == nbr_predicted:
           print("{} is Correctly Recognized with confidence {}".format(nbr_actual, conf))
        else:
            print("{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted))
sys.exit()