百度人脸注册/搜索测试
Posted 52-qq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度人脸注册/搜索测试相关的知识,希望对你有一定的参考价值。
获取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 }] } } ‘‘‘
以上是关于百度人脸注册/搜索测试的主要内容,如果未能解决你的问题,请参考以下文章