face_recognition实现人脸相似度比较

face_recognition

face_recognition 是github上一个非常有名气的人脸识别开源工具包,我们可以通过以下指令安装到python环境内

$ pip install face_recognition

加载人脸图片

image = face_recognition.load_image_file(src)

face_recognition实现人脸相似度比较_第1张图片

获取图片人脸定位[(top,right,bottom,left )]

face_locations = face_recognition.face_locations(image)
img  = image[face_locations[0][0]:face_locations[0][2],face_locations[0][3]:face_locations[0][1]]

face_recognition实现人脸相似度比较_第2张图片

对人脸图片进行编码

face_encoding = face_recognition.face_encodings(image, face_locations)[0]

face_recognition实现人脸相似度比较_第3张图片

说明:给定想要识别的人脸的图片并对其进行编码(每个人只需要一张),并将这些不同的人脸编码构建成一个列表。编码其实就是将人脸图片映射成一个128维的特征向量。

简单判断人脸相似度,内置函数

def theSamePerson(one_pic,two_pic):
    '''
    给定两张图片,判断是否是同一个人
    '''
    chenglong = face_recognition.load_image_file(one_pic)
    unknown_image = face_recognition.load_image_file(two_pic)
    biden_encoding = face_recognition.face_encodings(chenglong)[0]
    unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    results = face_recognition.compare_faces([biden_encoding], unknown_encoding,tolerance=0.35)
    print('results: ',results)
    return results[0]

在这里插入图片描述

自定义余弦相似度比较人脸相似度

def getFaceEncoding(src):
    image = face_recognition.load_image_file(src)
    face_locations = face_recognition.face_locations(image)
    img_  = image[face_locations[0][0]:face_locations[0][2],face_locations[0][3]:face_locations[0][1]]#人脸范围
    face_encoding = face_recognition.face_encodings(image, face_locations)[0]
    return face_encoding
def simcos(A,B):
    A=np.array(A)
    B=np.array(B)
    dist = np.linalg.norm(A - B) # 二范数
    sim = 1.0 / (1.0 + dist) #
    return sim
def main():
    src1,src2 = sys.argv[1],sys.argv[2]#两张图片路径
    xl1=getFaceEncoding(src1)
    xl2=getFaceEncoding(src2)
    face_distances = face_recognition.face_distance([xl1], xl2)
    value=simcos(xl1,xl2)
    if value>0.75:
        print(True)
    else:
        print(False)

完整代码

import face_recognition
import numpy as np
import sys
import cv2

def display(img):#查看图片
    cv2.imshow("img",img)
    cv2.waitKey()
    cv2.destroyAllWindows()
def getFaceEncoding(src):
    image = face_recognition.load_image_file(src)
    face_locations = face_recognition.face_locations(image)
    img_  = image[face_locations[0][0]:face_locations[0][2],face_locations[0][3]:face_locations[0][1]]
    img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
    #display(img_)
    face_encoding = face_recognition.face_encodings(image, face_locations)[0]
    return face_encoding
def simcos(A,B):
    A=np.array(A)
    B=np.array(B)
    dist = np.linalg.norm(A - B) # 二范数
    sim = 1.0 / (1.0 + dist) #
    return sim

def theSamePerson(one_pic,two_pic):
    '''
    给定两张图片,判断是否是同一个人
    '''
    chenglong = face_recognition.load_image_file(one_pic)
    unknown_image = face_recognition.load_image_file(two_pic)
    biden_encoding = face_recognition.face_encodings(chenglong)[0]
    unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    results = face_recognition.compare_faces([biden_encoding], unknown_encoding,tolerance=0.35)
    print('results: ',results)
    return results[0]

def main():
    src1,src2 = sys.argv[1],sys.argv[2]
    #theSamePerson(src1,src2)
    xl1=getFaceEncoding(src1)
    xl2=getFaceEncoding(src2)
    face_distances = face_recognition.face_distance([xl1], xl2)
    value=simcos(xl1,xl2)
    if value>0.75:
        print(True)
    else:
        print(False)

main()

face_recognition实现人脸相似度比较_第4张图片
face_recognition实现人脸相似度比较_第5张图片
在这里插入图片描述

说明:以上图片全部为stylegan生成的虚假图片。

延申

可以用作门禁系统或人脸登录方面

import face_recognition
import os
import numpy as np

cache_dir = "output/cache"


def save_file(vector,name):
    root = os.path.dirname(name)
    if(not os.path.exists(root)):
        os.mkdir(root)
    np.save(name,vector)


def getFaceEncoding(src):
    name = "output/cache/" +os.path.basename(src).split(".")[0]
    image = face_recognition.load_image_file(src)
    face_locations = face_recognition.face_locations(image)
    face_encoding = face_recognition.face_encodings(image, face_locations)[0]
    save_file(face_encoding,name)
    return face_encoding

def theSamePerson(pic,cache_dir):
    '''
    给定两张图片,判断是否是同一个人
    '''
    pic1 = face_recognition.load_image_file(pic)
    face_locations = face_recognition.face_locations(pic1)
    face_encoding = face_recognition.face_encodings(pic1, face_locations)[0]

    all_meber = os.listdir(cache_dir)
    for meber in all_meber:
    	current_name = meber.split(".")[0]
    	current_file = cache_dir + "/" + meber
    	current_meber_encoding = np.load(current_file)
    	results = face_recognition.compare_faces([face_encoding],current_meber_encoding,tolerance=0.35)
    	if (results == [True]):
    		return current_name

    return "not exists!"



def main():
    #file = "output/jingjing.png"
    #getFaceEncoding(file)
    file1 = "output/image0000-target.png"
    name = theSamePerson(file1,cache_dir)
    print ("This meber is ",name)

    
main()

face_recognition实现人脸相似度比较_第6张图片
思路:我们将人脸编码储存起来,建立人脸信息库。
随后我们给定一张图片进行测试。

你可能感兴趣的:(图像处理,深度学习,神经网络,计算机视觉,python,opencv)