- 错误:POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
- 出错地方 response =request.urlopen(req)不知道怎么改
# encoding:utf-8 from urllib import request from urllib import parse import json import ssl import sys def get_token(): host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=jPnOkfrida8pFOlRHRo9Sqb7&client_secret=7gYFT3OZMHN0TSrsrkisMeVvle6GhGFZ ' req = request.Request(host) req.add_header('Content-Type', 'application/json; charset=UTF-8') response = request.urlopen(req) content =response.read() #"""结果转化为字符""" content = bytes.decode(content) # """转化为字典""" content = eval(content[:-1]) return content['access_token'] #"""转换图片""" def imgdata(file1path,file2path): import base64 f = open(r'%s'% file1path,'rb') pic1 = base64.b64encode(f.read()) f.close() f = open(r'%s'% file2path,'rb') pic2 = base64.b64encode(f.read()) f.close() print(pic1) params = json.dumps( [ {"image":"%s"%pic1, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW" }, {"image":"%s"%pic2, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW" } ],indent=4 ) return params #人脸检测与属性分析 #''' def img(file1path,file2path): token = get_token() print(token) url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token="+token data = imgdata(file1path,file2path) req = request.Request(url,data = data) req.add_header('Content-Type', 'application/json') response =request.urlopen(req)#这报错 POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. content = response.read() content = bytes.decode(content) content = eval(content) #"""获得分数""" score = content['result'][0]['score'] if score > 80: return '图片相似度:'+str(score)+',同一个人' else: return '图片相似度:'+str(score)+',bu同一个人' if __name__ == '__main__': file1path = 'tupian4.jpg' file2path = 'tupian2.jpg' res = img(file1path,file2path) print(res)
请登录后评论
TOP
切换版块
ok,好的
不好意思,刚刚没注意看到你另一个成功帖子,我也解决了谢谢
我也遇到同样的问题了,请问可以说一下你是怎么解决的吗?
人脸模块的全部不要钱。有QPS限制。QPS是需要付费的。免费用户是5貌似。企业认证貌似是10
离线的也有。5000个以上的设备199一个
好的
那你就编辑你的帖子 说明一下解决方案。以便其他Python朋友遇到和你一样的问题
不要
好的,谢谢你,我做出来了
请问下,这些api要钱吗?
pic2 pic1 转成str试试。我不会python。你这个错误也不是接口返回的。
你可以看我写的http://ai.baidu.com/forum/topic/show/864043
区别在于base64我又做了str处理