face_recognition实现人脸相似度比较

Posted 佐倉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了face_recognition实现人脸相似度比较相关的知识,希望对你有一定的参考价值。

face_recognition

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

$ pip install face_recognition

加载人脸图片

image = face_recognition.load_image_file(src)

获取图片人脸定位[(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_encoding = face_recognition.face_encodings(image, face_locations)[0]

说明:给定想要识别的人脸的图片并对其进行编码(每个人只需要一张),并将这些不同的人脸编码构建成一个列表。编码其实就是将人脸图片映射成一个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()



说明:以上图片全部为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实现人脸相似度比较的主要内容,如果未能解决你的问题,请参考以下文章

使用 opencv 和 node 比较两个人脸的相似度

用Python实现一个简单的——人脸相似度对比

使用 python3 模块 face_recognition 比较两个人脸?

几种相似度的对比

人工智能之基于face_recognition的人脸检测与识别

AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习