最后编辑于2022-10
Python3-urllib3-API定制化图像接口示例代码
- AccessToken获取可以参考:http://ai.baidu.com/forum/topic/show/497663(Python3-urllib3示例)
- Python安装什么的。大家百度经验即可
-----------------------------------------------------下面开始代码----------------------------------------------------
- Python3-API示例代码(定制化图像平台接口)
'''
Created on 2018-1-26
定制化图像接口-Python3 -API示例代码
@author: 小帅丶
'''
import urllib3,base64
import json
access_token='24.745a2689abb8f19ffd58b5ede94ba6e6.2592000.1519880763.282335-10131029'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/yourselfapiname?access_token='+access_token
f = open('G:/mnist_png/training/8/94.png','rb')
#参数image:图像base64编码 以及top_num参数
img = base64.b64encode(f.read())
#img参数进行一下str转换
params={'image':''+str(img,'utf-8')+'','top_num':5}
#对参数数据进行json格式处理
encoded_data = json.dumps(params).encode('utf-8')
print(encoded_data)
request=http.request('POST',
url,
body=encoded_data,
headers={'Content-Type':'application/json'})
#对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data,'utf-8')
print(result)
- 返回的识别结果内容
{
"log_id": 16307189784052794363,
"results": [
{
"name": "8",
"score": 0.9999861717224121
},
{
"name": "9",
"score": 5.0299408940190915e-06
},
{
"name": "zero",
"score": 4.277262632967904e-06
},
{
"name": "5",
"score": 1.8296633470527013e-06
},
{
"name": "6",
"score": 9.7080430805363e-07
}
]
}
- 测试的图片:没发上传。图片原本内容是个手写的数字8
-----------------------------------------------------代码结束-----------------------------------------------------
确实发现Python简单。而且写的代码也很少。代码仅供参考。
小帅丶的QQ:783021975
请登录后评论
TOP
切换版块
这个目前看官网的一些。并不能。你可以提交工单咨询百度技术人员后续是否考虑增加
返回的结果能否包含 物体在图片中的位置信息