一、登錄注冊,實名認證,進入開放平台,選擇需要的功能
二、選擇人臉識別,獲取免費資源,免費資源有365天
三、創建應用,應用使用途徑填學習使用
四、應用生成后會獲得APIkey和SecretKey 后面會使用到
五、然后就可以到pycharm中開始編碼了
①復制 百度API 技術文檔 中的代碼
其中 client_id=AK,client_secret=SK
②將函數封裝 傳入參數 client_key client_secret 返回access_token
# encoding:utf-8
import requests
import json #pip install 直接下載json包
import base64
client_id = "換成自己生成的碼"
client_secret = "換成自己生成的碼"
def get_access_tocken(client_id,client_secret):
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(client_id,client_secret)
response = requests.get(host)
if response:
#print(response.json())
#print(response.json()['access_token']) #把json看做一個字典,從字典中取值
return response.json()['access_token']
③調用百度API人臉識別的接口
params 中是需要上傳的圖像數據。
其中 image 圖片信息需要轉換成BASE64:
f1=open('./photo/img1.jpeg','rb')
img1=base64.b64encode(f1.read()).decode()
f2=open('./photo/img2.jpeg','rb')
img2=base64.b64encode(f2.read()).decode()
如果上傳的是靜態的圖片,需要把liveness_control這一參數修改成NONE。
六、API接口返回的都是json格式,所以需要自己獲取json格式中自己需要的字段
response()['result']['score'],具體的需要查看百度提供的API技術文檔
access_token =myaccess_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())
if response.json()['result']['score']>92:
print("相似")