今日总结-实现百度云api的调用
获取的json结果如下:
代码如下:
# encoding:utf-8 import base64 import requests def getToken(): 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): params={} request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" params["image"]=base_64_data params["image_type"] = "BASE64" params["face_field"] = "age,beauty,landmark" access_token = token_ request_url = request_url + "?access_token=" + access_token headers = {'content-type': 'application/json'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json()["result"]["face_list"]) print(response.json()["result"]["face_list"][0]["age"]) print(response.json()["result"]["face_list"][0]["beauty"]) print(response.json()["result"]["face_list"][0]["landmark"]) if __name__=="__main__": base_64=img_to_base64("face2.jpg") token=getToken() FaceDetect(token,base_64)