# 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/search" params["image"]=base_64_data params["image_type"] = "BASE64" params["group_id_list"] = "face" 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()) print("该同学的学号和姓名为:", response.json()["result"]["user_list"][0]["user_info"]) if __name__=="__main__": base_64=img_to_base64("face1.jpg") token=getToken() FaceDetect(token,base_64)
通过调用搜索的方法,从人脸库中获取人脸的JSON数据,并打印出了user_info该组图的信息数据。
标签:token,api,64,base,params,人脸,data,response,百度 From: https://www.cnblogs.com/pengsuoqun123/p/17300933.html