Python利用百度ai人脸识别库实现人脸比对

#人脸比对

from PIL import Image
from aip import AipFace
import base64,os,time

# 定义常量,初始化AipFace对象
""" 你的 APPID AK SK """
APP_ID = '你的appID'
API_KEY = '你的API_KEY'
SECRET_KEY = '你的SECRET_KEY'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

# 分别获得2个文件夹中的图片文件名称
orginimageList=os.listdir('./old/')   #同目录下有两个文件夹,分别放着需要比对的照片
searchimageList=os.listdir('./new/')  #没有文件夹的可以新建两个同名文件夹 old和new

# 双重遍历2个文件夹的文件
for oimg in orginimageList:
    for simg in searchimageList:
        time.sleep(0.3)
        #调用人脸比对
        result = client.match([
            {
                'image': str(base64.b64encode(open('./old/'+oimg, 'rb').read()),'utf-8'),
                'image_type': 'BASE64',
            },
            {
                'image': str(base64.b64encode(open('./new/'+simg, 'rb').read()),'utf-8'),
                'image_type': 'BASE64',
            }
        ])
        print(result)
        # #相似度较高,输出相应结果
        if result['result']['score']>=80:
            print( oimg+simg,'应该是同一个人,相似得分:',result['result']['score'])
            #展示两张照片
            Image.open('./old/'+oimg).show()
            Image.open('./new/'+simg).show()

结果:当old和new文件夹里的两张照片相似度高,则自动展示这两张照片

你可能感兴趣的:(Python,人脸识别,python,图像识别)