python调用AI百度开放平台API

1.Access Token获取

2.

#coding=utf-8
import urllib3
from urllib import urlencode
import base64
import json
import time

t=time.time()
access_token='【1中获取的token】'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rest/2.0/image-classify/v1/body_num?access_token='+access_token
f = open('./tu/006.png','rb')#图片的路径
img = base64.b64encode(f.read())
imgParam=str(img).decode('utf-8')
param = {'image':imgParam,'show':'true'}
param=urlencode(param)
request=http.request('POST',
                      url,
                      body=param,
                      headers={'Content-Type':'application/x-www-form-urlencoded'})
result = str(request.data).decode('utf-8')
result=result.encode('gbk')
result=json.loads(result)
print u'识别出人数为:'+str(result["person_num"])
t2=time.time()-t
print u'用时:'+str(t2)+u'秒。'
strs = result["image"]#获得base64格式的图片
img = base64.b64decode(strs)#转码准备
file = open('test002.jpg','wb')
file.write(img)#保存图片查看效果图
file.close()

你可能感兴趣的:(python)