使用发布API过程中 ,一个BUG请教
zp632825 发布于2018-12 浏览:1090 回复:1
0
收藏

训练了一个 demo模型 ,使用接口时,运行是报错 ,请教

执行此句   params = urllib.parse.urlencode(params).encode("utf-8")

提示错误 ;TypeError: not a valid non-string sequence or mapping object

请问如何改? 代码如下

#encoding:utf-8
import base64
import urllib.request, urllib.error, urllib.parse
import json


# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=PjeNmeZGIHRFrPSGwo8WXmkV&client_secret=rAl0K3ZbMNoWPjFVms78tZZS64XybVqx'
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read().decode("utf-8")
if (content):
print(content)

json_data=eval(content)
print("access_token:%s" % json_data["access_token"])


'''
图像主体检测
'''

request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/zybtest1"
file_name='E:\\baidu_AI\\nail_clippers\\20181128-39.jpg'


# 二进制方式打开图片文件
f = open(file_name, 'rb')
img = base64.b64encode(f.read())
#img = base64.encodestring(f.read())
img = str(img,'utf-8')


params = "{\"image\":" + "\"" + img + "\"" + "," + "\"threshold\":0.3}"

params = json.dumps(params)
print("param is:%s" % params)
params = urllib.parse.urlencode(params).encode("utf-8")

access_token = json_data["access_token"]
print("access_token:%s" % access_token)
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
#content = response.read().decode("utf-8")
content = response.read()
if content:
print("resp is:%s" % content)
收藏
点赞
0
个赞
共1条回复 最后由756665228回复于2018-12
#2756665228回复于2018-12

这看的不是接口问题呀。AccessToken最好提前先获取好。

http://ai.baidu.com/forum/topic/show/864043 Python示例代码 

1
TOP
切换版块