獲取access_token:https://ai.baidu.com/docs#/Auth/top數據庫
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={id}&client_secret={secret}'.format(id='【控制檯獲取】', secret='【控制檯獲取】') headers = { 'Content-Type': 'application/json; charset=UTF-8' } res = requests.get(url=host, headers=headers) res = json.loads(res.text) print(res.get('access_token', None))
人臉註冊api文檔:https://ai.baidu.com/docs#/Face-Set-V3/topjson
# 人臉庫添加 face_dir = os.path.join(os.path.dirname(__file__), 'face_test', 'face-picture.jpg') with open(face_dir, 'rb') as f: image = base64.b64encode(f.read()).decode() face_add = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=【從控制檯獲取並生成】' headers = { 'Content-Type': 'application/json' } data = { 'image': image, 'image_type': 'BASE64', 'group_id': 'archive', 'user_id': 'zhang', } res = requests.post(url=face_add, headers=headers, data=data) res = json.loads(res.text) print(res)
返回結果:api
# res: { 'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1368654417985064851, 'timestamp': 1571798506, 'cached': 0, 'result': { 'face_token': '人臉token{全局惟一}', # 在用戶數據庫新增face_token字段,人臉註冊後將惟一的身份id存入以便後邊對人臉搜索的結果進行比對 'location': { 'left': 35.18, 'top': 216.13, 'width': 380, 'height': 388, 'rotation': -3 } } } '''
人臉搜索api文檔:https://ai.baidu.com/docs#/Face-Search-V3/topapp
# 人臉庫搜索 face_dir = os.path.join(os.path.dirname(__file__), 'face_test', 'face-picture.jpg') with open(face_dir, 'rb') as f: image = base64.b64encode(f.read()).decode() headers = { 'Content-Type': 'application/json' } data = { 'image': image, 'image_type': 'BASE64', 'group_id_list': 'archive' } search_url = "https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=【控制檯獲取並生成】" res = requests.post(url=search_url, headers=headers, data=data) res = json.loads(res.text) print(res)
''' # res { 'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1345050717991180571, 'timestamp': 1571799118, 'cached': 0, 'result': { 'face_token': '人臉token{全局惟一}', # 查詢到的人臉token能夠用做數據庫字段比對,查看這我的臉的身份信息 'user_list': [{ 'group_id': 'archive', 'user_id': 'zhang', 'user_info': '', 'score': 100 }] } } '''