时间: 2020-09-3|tag:35次围观|0 条评论

获取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/top

# 人脸库添加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)

返回结果:

# 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/top

# 人脸库搜索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        }]    }}'''

 

文章转载于:https://www.cnblogs.com/52-qq/p/11725119.html

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《百度人脸注册/搜索测试
   

还没有人抢沙发呢~