平时大家遇到文字识别进行提取文本特征是不是没有头绪,总结一下文字识别的方面的技术.
一.百度ai文字识别:
申请API Key和Secret Key,这种调用方法简单,但是平台限制,一天紧紧可以申请的条数特别的有限。速度是可以的,就是免费的量有限。
逻辑:
1.获取token:
# 获取access_token
def getAccessToken(client_id, client_secret):
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
response = requests.get(host)
if response:
tokenResult = response.json()
access_token = tokenResult['access_token']
return access_token
return ""
2.图片下载并且转换成base64格式:
def get_img_base64_from_url(url, max_length=1000000):
"""
从互联网获取图片,并限定宽度最大为max_width,最后转换为base64
url:互联网上的图片url
max_length:图片缩放的最大宽度。大于该长度的图片将会被缩放。
"""
try:
origin_file = cStringIO.StringIO(urllib2.urlopen(url).read())
img = Image.open(origin_file)
w, h = img.size
larger_side = max(w, h)
if larger_side > max_length:
img = img.resize((int(float(max_length) * w / larger_side),
int(float(max_length) * h / larger_side)), Image.ANTIALIAS)
jpeg_image_buffer = cStringIO.StringIO()
img.save(jpeg_image_buffer, format="JPEG")
base64_str = base64.b64encode(jpeg_image_buffer.getvalue())
return base64_str
except Exception, e:
return ""
3.post发送图片的请求返回结果:
# 调用百度API完成人脸识别
def baidu_face(client_id, client_secret, path):
try:
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_b"
params ={"image":path}
access_token = getAccessToken(client_id, client_secret)
if (access_token == ""):
print("获取access_token失败")
return
request_url = request_url + "?access_token=" + access_token
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
json = response.json()
return json
except Exception as e:
print(e.message)
return -1