# encoding:utf-8
import base64
import requests
def getToken():标签:token,api,64,base,params,人脸,data,response,百度 From: https://www.cnblogs.com/pengsuoqun123/p/17299547.html
ak='B7E2OqVuDAyDs7OsuGPuKa4y'
sk='idObOz6jqA2GdU49L2VG4VPVhgmiidvD'
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'
response = requests.get(host)
return response.json().get("access_token")
def img_to_base64(file_path):
with open(file_path,'rb') as f:
base_64_data=base64.b64encode(f.read())
s=base_64_data.decode()
return s
def FaceDetect(token_,base_64_data_1,base_64_data_2):
params=[{},{}]
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match"
params[0]["image"]=base_64_data_1
params[0]["image_type"] = "BASE64"
params[1]["image"] = base_64_data_2
params[1]["image_type"] = "BASE64"
access_token = token_
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, json=params, headers=headers)
if response:
print(response.json())
score=response.json()["result"]["score"]
if(score>85):
print("为同一人")
else:
print("不是同一人")
if __name__=="__main__":
base_64_1=img_to_base64("face02.jpg")
base_64_2= img_to_base64("face2.jpg")
token=getToken()
FaceDetect(token,base_64_1,base_64_2)