最后编辑于2018-12
# 在python3.7和V3环境下人脸对比示例代码
在原示例基础,主要更改:
1·用urllib.request替代所有的urllib2
# import urllib2
2·在原data=params尾加上了.encode(encoding="utf-8"),同时,在下面的print输出时,再使用decode("utf-8")进行解码 参考https://blog.csdn.net/weixin_41596463/article/details/79184537
request = urllib.request.Request(url=request_url, data=params.encode(encoding="utf-8"))
# request = urllib.request.Request(url=request_url, data=params)
# request = urllib2.Request(url=request_url, data=params)
3·在print输出时加上decode("utf-8")解码
print(content.decode("utf-8"))
#print(content)
# encoding:utf-8
import urllib.request
# import urllib2
import json
'''
人脸对比
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match"
# image后面引号内的编码是用https://tool.css-js.com/base64.html编码工具,转换的本地图片代码,内容很长,这里是简写示意,运行时要代换为自己转换的编码。
# 但是不选“包含头”。
params = json.dumps(
[{"image": "sfasq35sadvsvqwr5q...", "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
{"image": "sfasq35sadvsvqwr5q...", "image_type": "BASE64", "face_type": "IDCARD", "quality_control": "LOW"}])
#这个face_type的值不一样,但没看到对结果的影响。
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
# request = urllib2.Request(url=request_url, data=params)
# 原urllib2替换为urllib.request:
# request = urllib.request.Request(url=request_url, data=params)
# 在原data=params尾加上了.encode(encoding="utf-8"),同时,在下面的print输出时,再使用decode("utf-8")进行解码 参考https://blog.csdn.net/weixin_41596463/article/details/79184537
#形成下面的代码:
request = urllib.request.Request(url=request_url, data=params.encode(encoding="utf-8"))
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
# response = urllib2.urlopen(request)
content = response.read()
if content:
print(content.decode("utf-8"))
#print(content)
运行结果:
python.exe face-duibi.py
{"error_code":0,"error_msg":"SUCCESS","log_id":305486846880221051,"timestamp":1544688022,"cached":0,"result":{"score":91.44816589,"face_list":[{"face_token":"d64ce288d3b9f1d00000f00cf73be8de"},{"face_token":"b67f82aed1babadb3617d68544df3dd1"}]}}
Process finished with exit code 0
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
好的,已修改,谢谢!
帖子可以修改的哈
好的,谢谢!第一次在这里发,想着记录下纠错过程同时分享,,也感谢您给出链接便于我学习,非常感谢!
http://ai.baidu.com/forum/topic/show/864836 这里也有哦
建议发代码最好选择 {;} 代码块 插入代码 这样看的好一些