调用百度云api实现人脸库注册代码展示
# 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/faceset/user/add" params["image"]=base_64_data params["image_type"] = "BASE64" params["group_id"] = "face" params["user_id"] = "user_4" params["user_info"] = "20213835 杨凯文" 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()) if __name__=="__main__": base_64=img_to_base64("ykw.jpg") token=getToken() FaceDetect(token,base_64)