基于python 2.7
import urllib, urllib2, sys, base64
import ssl
import json
'''
获取access_token
'''
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
print(content)
# 提取其中"access_token":"************",下面会用到
'''
通用文字识别(含位置高精度版)
'''
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=' + access_token
url_general = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
# 二进制方式打开图文件
f = open(r'本地图片', 'rb')
# 参数image:图像base64编码
img = base64.b64encode(f.read())
params = {"image": img}
params = urllib.urlencode(params)
request = urllib2.Request(url, params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
if (content):
print(content)