Python3-urllib3-API定制化图像接口示例代码
AccessToken获取可以参考:http://ai.baidu.com/forum/topic/show/497663(Python3-urllib3示例)
python编写程序输出以下图形,Python安装什么的。大家百度经验即可
-----------------------------------------------------下面开始代码----------------------------------------------------
Python3-API示例代码(定制化图像平台接口)
python代码画、'''
Created on 2018-1-26
定制化图像接口-Python3 -API示例代码
python代码,@author: 小帅丶
'''
import urllib3,base64
python图形界面哪个好,import json
access_token='24.745a2689abb8f19ffd58b5ede94ba6e6.2592000.1519880763.282335-10131029'
http=urllib3.PoolManager()
python图形化、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}
python基本图形绘制,#对参数params数据进行json处理
encoded_data = json.dumps(params).encode('utf-8')
print(encoded_data)
python怎么写、request=http.request('POST',
url,
body=encoded_data,
headers={'Content-Type':'application/json'})
#对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data,'utf-8')
print(result)
示例代码2
import urllib3, json, base64
def image_classification():
#access_token获取方法请详见API使用说明,请注意access_token有效期为30天,这里仅为了简化编码每一次请求都去获取access_token
access_token='【access_token】'
http=urllib3.PoolManager()
#请将API地址替换为EasyDL所提供的API地址
url='【API地址】?access_token='+access_token
#修改图片地址,测试不同图片
f = open('【本地图片地址】','rb')
#参数image:图像base64编码以及返回分类数量top_num设置为5
img = base64.b64encode(f.read())
#img参数进行一下str转换
params={'image':''+str(img,'utf-8')+'','top_num':5}
#对参数params数据进行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)
#输出结果
print(image_classification())
返回的识别结果内容
{
"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
2018年8月9日增加物体检测示例代码
示例代码
'''
Created on 2018-08-09
定制化图像接口物体检测-Python3 -API示例代码
@author: 小帅丶
'''
import urllib3,base64
import json
access_token='自己应用获取的access_token'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/yourselfname?access_token='+access_token
f = open('G:/mnist_png/training/8/94.png','rb')
#参数image:图像base64数据不需要urlencode
img = base64.b64encode(f.read())
print(img)
params={'image':''+str(img,'utf-8')+'','threshold':0.3}
#对数据进行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)
-----------------------------------------------------代码结束-----------------------------------------------------
确实发现Python简单。而且写的代码也很少。代码仅供参考。
小帅丶的QQ:783021975
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态