import requests
import base64
import json
api1 = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=14937000&client_secret=vcMTUfvU6hyCgKdBSsoiooY10RNUpDq1"
# 1.获取token值 拼接api
def get_token():
response = requests.get(api1)
access_token = eval(response.text)['access_token']
api2 = "https://aip.baidubce.com/rest/2.0/face/v3/match" + "?access_token=" + access_token
return api2
# 2.获取图片数据
def img(img1, img2):
with open(img1, "rb") as f:
pic1 = base64.b64encode(f.read())
with open(img2, "rb") as f:
pic2 = base64.b64encode(f.read())
params = json.dumps([
{"images": str(pic1, "utf-8"), "image_type": "BASE64", "face_type": "LIVE", "quality_control": "perfect"},
{"images": str(pic2, "utf-8"), "image_type": "BASE64", "face_type": "IDCARD", "quality_control": "perfect"}
])
return params
# 3.发送请求拿到对比结果
def than_img(file1, file2):
params = img(file1, file2)
api = get_token()
content = requests.post(api, params).text
score = eval(content)['result']['score']
if score > 80:
print("图片相似度:" + str(score) + ",同一个人")
else:
print("图片相似度:" + str(score) + ",不是同一个人")
than_img("马云.png", "马云.png")
比心