Python人脸识别和对比

直接上代码

import face_recognition
import numpy as np
import cv2


def face_re(face1, face2):
	'''
	对比两张图片是否属于同一人
	'''
    face_encoding1 = []
    is_face1 = face_recognition.face_encodings(face_recognition.load_image_file(face1))

    if (len(is_face1) <= 0):
        print('未识别人脸1')
        return '未识别人脸1'
    else:
        face_encoding1.append(is_face1[0])
    face_encoding2 = []
    is_face2 = face_recognition.face_encodings(face_recognition.load_image_file(face2))
    if (len(is_face2) <= 0):
        print('未识别人脸2')
        return '未识别人脸2'
    else:
        face_encoding2.append(is_face2[0])
    match = face_recognition.compare_faces(np.array(is_face1), np.array(is_face2), tolerance=0.5)

    print(match)
    if match[0]:
        print('图片识别为同一人')
        return True
    else:
        print('图片识别不是一个人')
        return False


def is_face(face_img):
	'''
	判断是否是人脸
	'''
    is_face1 = face_recognition.face_encodings(face_recognition.load_image_file(face_img))

    if (len(is_face1) <= 0):
        print('未识别人脸1')
        return False
    else:
        print('识别人脸')
        return True

利用face_recognition已经训练好的模型,对于证件照之类的人脸对比效果最好

你可能感兴趣的:(无聊解闷,人脸识别,python)