python人脸试别——照片对比(百度ai)

首先获取access_token

点击 百度ai平台 进入控制台,创建一个应用,如图

创建成功打开应用把 api key 和secret key复制下来

代码 

import urllib
from urllib import request , parse
import base64
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= 这是你的api key &client_secret= 这是你的secret key '
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 把字符转换为字节
    content = bytes.decode(content)
    # 把字节存入字典中  eval功能:将字符串str当成有效的表达式来求值并返回计算结果
    content = eval(content[:-1])
    return content['access_token']

if __name__ == '__main__':
    print(get_token())

运行结果

把文件中的照片以BASE64的形式读出

代码

import urllib
from urllib import request , parse
import base64
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= 这是你的api key &client_secret= 这是你的secret key '
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 把字符转换为字节
    content = bytes.decode(content)
    # 把字节存入字典中  eval功能:将字符串str当成有效的表达式来求值并返回计算结果
    content = eval(content[:-1])
    return content['access_token']

def img_data(img1Path,img2Path):
    # 把图片转换成base64编码
    f = open(r'%s' % img1Path,'rb')
    pic1 = base64.b64encode(f.read())
    f.close()
    f = open(r'%s' % img2Path,'rb')
    pic2 = base64.b64encode(f.read())
    f.close()
    # 将文件转化为可提交信息
    params = json.dumps(
        [{"image": str(pic1, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
         {"image": str(pic2, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"}]
    )
    return params.encode(encoding='UTF8')

if __name__ == '__main__':
    img1Path = '这个是你照片1的位置'
    img2Path = '这个是你照片2的位置'
    print(img_data(img1Path,img2Path))

这个结果就不截图了,他就是一大坨字符流

把照片进行比较

import urllib
from urllib import request , parse
import base64
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= 这是你的api key &client_secret= 这是你的secret key '
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 把字符转换为字节
    content = bytes.decode(content)
    # 把字节存入字典中  eval功能:将字符串str当成有效的表达式来求值并返回计算结果
    content = eval(content[:-1])
    return content['access_token']

def img_data(img1Path,img2Path):
    # 把图片转换成base64编码
    f = open(r'%s' % img1Path,'rb')
    pic1 = base64.b64encode(f.read())
    f.close()
    f = open(r'%s' % img2Path,'rb')
    pic2 = base64.b64encode(f.read())
    f.close()
    # 将文件转化为可提交信息
    params = json.dumps(
        [{"image": str(pic1, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
         {"image": str(pic2, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"}]
    )
    return params.encode(encoding='UTF8')

def faceTest(img1Path,img2Path):
    token = get_token()
    params = img_data(img1Path,img2Path)
    # 使用百度官方api接口
    request_url = 'https://aip.baidubce.com/rest/2.0/face/v3/match'
    access_token = get_token()

    # 提交请求
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 评分
    content = eval(content)
    score = content['result']['score']
    if score>80:
        return "照片相似度为:" + str(score) +"基本确认为本人"
    else:
        return "照片相似度为:" + str(score) +"基本确认不是本人"

if __name__ == '__main__':
    img1Path = '你照片1的地址'
    img2Path = '你照片2的地址'
    print(img_data(img1Path,img2Path))

例子比较下面两组照片

左为hu1右为hu2

右为peng

代码

import urllib
from urllib import request , parse
import base64
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= 这是你的api key &client_secret= 这是你的secret key '
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 把字符转换为字节
    content = bytes.decode(content)
    # 把字节存入字典中  eval功能:将字符串str当成有效的表达式来求值并返回计算结果
    content = eval(content[:-1])
    return content['access_token']

def img_data(img1Path,img2Path):
    # 把图片转换成base64编码
    f = open(r'%s' % img1Path,'rb')
    pic1 = base64.b64encode(f.read())
    f.close()
    f = open(r'%s' % img2Path,'rb')
    pic2 = base64.b64encode(f.read())
    f.close()
    # 将文件转化为可提交信息
    params = json.dumps(
        [{"image": str(pic1, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
         {"image": str(pic2, 'utf-8'), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"}]
    )
    return params.encode(encoding='UTF8')

def faceTest(img1Path,img2Path):
    token = get_token()
    params = img_data(img1Path,img2Path)
    request_url = 'https://aip.baidubce.com/rest/2.0/face/v3/match'
    access_token = get_token()

    # 对比照片
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(request)
    content = response.read()
    # 评分
    content = eval(content)
    score = content['result']['score']
    if score>80:
        return "照片相似度为:" + str(score) +"基本确认为本人"
    else:
        return "照片相似度为:" + str(score) +"基本确认不是本人"

if __name__ == '__main__':
    img1Path = './images/hu2.jpg'
    img2Path = './images/hu1.jpg'
    # img2Path = './images/peng.jpg'
    print(faceTest(img1Path, img2Path))

结果分别为

原文请点击

猜你喜欢

转载自blog.csdn.net/qq_39248122/article/details/87983414
今日推荐