基于python的百度APl批量文字图片识别。
coding=utf-8
import os
import sys
import json
import base64
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.parse import quote_plus
防止https证书校验不正确
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
API_KEY = ‘’# 在这里写入你的API Key
SECRET_KEY = ‘’# 在这里写入你的API私钥
OCR_URL = “https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic”#API类型
“”" TOKEN start “”"
TOKEN_URL = ‘https://aip.baidubce.com/oauth/2.0/token’
“”"
获取token
“”"
def fetch_token():
params = {‘grant_type’: ‘client_credentials’,
‘client_id’: API_KEY,
‘client_secret’: SECRET_KEY}
post_data = urlencode(params)
post_data = post_data.encode(‘utf-8’)
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print(err)
result_str = result_str.decode()
result = json.loads(result_str)
if ('access_token' in result.keys() and 'scope' in result.keys()):
if not 'brain_all_scope' in result['scope'].split(' '):
print ('please ensure has check the ability')
exit()
return result['access_token']
else:
print ('please overwrite the correct API_KEY and SECRET_KEY')
exit()
“”"
读取文件
“”"
def read_file(image_path):
f = None
try:
f = open(image_path, ‘rb’)
return f.read()
except:
print(‘read image file fail’)
return None
finally:
if f:
f.close()
“”"
调用远程服务
“”"
def request(url, data):
req = Request(url, data.encode(‘utf-8’))
has_error = False
try:
f = urlopen(req)
result_str = f.read()
result_str = result_str.decode()
return result_str
except URLError as err:
print(err)
if name == ‘main’:
# 获取access token
path = ""#这里是你的图片所在的文件夹
path1 = ""#这里是你图片文字识别后的文字存放的地方
token = fetch_token()#获取token
# 拼接通用文字识别高精度url
image_url = OCR_URL + "?access_token=" + token
text = ""
# 读取书籍页面图片
files = os.listdir(path)
file_content = []
for i in files:
filesPath = path+i
#print(i)
file_content.append(read_file(filesPath))
#print(file_content)
for i in file_content:
# 调用文字识别服务
result = ""
result = request(image_url, urlencode({'image': base64.b64encode(i)}))
# 解析返回结果
try:
result_json = json.loads(result)
for words_result in result_json["words_result"]:
text = text + words_result["words"]
except:
print("错误!")
continue
# 打印文字
file_handle = open(path1, mode='w')
file_handle.write(text)
file_handle.write("\n")
file_handle.close()
#print(text)