最近在网上闲逛无意中看到百度AI开放平台(http://ai.baidu.com/),产品相当丰富,有文字识别、图像分析、人脸识别... 觉得蛮有意思,所以就玩了下其中的通用文字识别,顺便练习下Python的post请求以及json解析。
语言:Python3.5.2
编译工具:Atom
API文档:http://ai.baidu.com/docs#/OCR-API/e1bd77f3
代码如下:
若想要运行的话请自行替换client_id(即API Key)、client_secret(即Secret Key)以及图像路径名称等;API Key和Secret Key需注册百度云管理中心后创建应用,在应用管理中获取。
import urllib,requests,re,json,base64,io,sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
#调用接口
def invokApi(*parameter,**headers):
response = requests.post(*parameter,**headers).text
return response
#解析json获取access_token
def getAccess_token(response):
token_json = json.loads(response)
access_token = token_json['access_token']
return access_token
#解析json获取words_result(list)
def getWords(response):
word_json = json.loads(response)
words_result = word_json['words_result']
list = []
for i in range(len(words_result)):
list.append(words_result[i]['words'])
return list
#打印words(resList)
def printWords(list):
for y in range(len(list)):
print (list[y])
if __name__=="__main__":
#【接口参数】获取AccessToken,具体参数说明可参看百度AI的API文档
token_url = 'https://aip.baidubce.com/oauth/2.0/token'
grant_type = 'client_credentials'
client_id = '**************'
client_secret = '**************'
token_parameter = {'grant_type':grant_type,'client_id':client_id,'client_secret':client_secret}
#获取access_token
access_token = getAccess_token(invokApi(token_url,token_parameter))
#【接口参数】百度通用文字识别
words_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
headers = {'Content-Type':'application/x-www-form-urlencoded'}
picture = '13.jpg'
#二进制方式打开图文件
f = open('E:/工作/Develop/Python/验证码识别/verificationCode/' + picture, 'rb')
#图像base64编码
img = base64.b64encode(f.read())
img_parameter = {'image':img,'access_token':access_token}
#获取识别结果并打印
recognition_word = invokApi(words_url,img_parameter,headers=headers)
printWords(getWords(recognition_word))
运行结果:
可以看到识别成功率还是很高地。