大家好,我是天空之城,今天给大家带来小福利,通过python利用百度ai实现图片识别。有兴趣加qq群,纯学习,1098016198。
至于api接口的APP_ID ,API_KEY ,SECRECT_KEY 参见下面链接。
利用百度ai打造自己语音合成小程序
import json
import requests
import base64
import urllib.parse
APP_ID = '你的id'
API_KEY ='你的key'
SECRECT_KEY = '你的secret-key'
# invoke
url = 'https://aip.baidubce.com/oauth/2.0/token'
data1 = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRECT_KEY
}
req = requests.post(url=url, data=data1)
token = json.loads(req.content)['access_token']
# baidu translate content
ocr_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=%s' % token
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
o = open(r'pic\img1.png', 'rb')#r后面是你的图片地址
content = o.read()
o.close()
body = base64.b64encode(content)
data2 = urllib.parse.urlencode({'image': body})
r = requests.post(url=ocr_url, headers=headers, data=data2)
res_words = json.loads(r.content)['words_result']
for i in res_words:
print(i["words"])