我制作了一个脚本,在 tesseract 和 pyocr 的帮助下将 pdf 扫描成文本批处理。代码如下。问题是,在处理大量文件(例如 20+)时,有时脚本会耗尽内存并因 OSError 而失败。我目前做了它,以便它可以在手动重启后顺利赶上崩溃的地方,但是这些手动重启很乏味。
由于 pyocr 对我来说基本上是一个黑匣子,我尝试将脚本包装到其他 Python 脚本中,以便在崩溃时重新启动它,但它们似乎都出现了该错误,只有在每个相关脚本终止时才释放内存。
我能想到的唯一其他解决方案是制作一个完全外部的包装器,它将检查脚本是否正在运行,如果没有,则重新启动它并且仍然有未处理的文件。
但也许有更好的解决方案?或者也许我编写了可以改进的蹩脚代码以避免这些内存崩溃?(除此之外,我知道它很蹩脚,但效果很好:))。
from io import BytesIO
from wand.image import Image
from PIL import Image as PI
import pyocr
import pyocr.builders
import io
import os
import os.path
import ast
def daemon_ocr(tool, img, lang):
txt = tool.image_to_string(
PI.open(BytesIO(img)),
lang=lang,
builder=pyocr.builders.TextBuilder()
)
return txt
def daemon_wrap(image_pdf, tool, lang, iteration):
print(iteration)
req_image = []
final_text = ''
image_pdf_bckp = image_pdf
image_jpeg = image_pdf.convert('jpeg')
for img in image_jpeg.sequence:
img_page = Image(image=img)
req_image.append(img_page.make_blob('jpeg'))
for img in req_image:
txt = daemon_ocr(tool, img, lang)
final_text += txt + '\n '
if 'работ' not in final_text and 'фактура' not in final_text and 'Аренда' not in final_text and 'Сумма' not in final_text\
and 'аренде' not in final_text and 'товара' not in final_text:
if iteration < 5:
iteration += 1
image_pdf = image_pdf.rotate(90)
final_text = daemon_wrap(image_pdf_bckp, tool, lang, iteration)
return final_text
def daemon_pyocr(food):
tool = pyocr.get_available_tools()[0]
lang = tool.get_available_languages()[0]
iteration = 1
image_pdf = Image(filename='{doc_name}'.format(doc_name=food), resolution=300)
final_text = daemon_wrap(image_pdf, tool, lang, iteration)
return final_text
files = [f for f in os.listdir('.') if os.path.isfile(f)]
output = {}
print(files)
path = os.path.dirname(os.path.abspath(__file__))
if os.path.exists('{p}/output'.format(p=path)):
text_file = open("output", "a")
first = False
else:
text_file = open("output", "w")
first = True
for f in files:
if f != 'ocr.py' and f != 'output':
try:
output[f] = daemon_pyocr(f)
print('{f} done'.format(f=f))
if first:
text_file.write(str(output)[1:-1])
first = False
else:
text_file.write(', {d}'.format(d=str(output)[1:-1]))
output = {}
os.rename('{p}/{f}'.format(p=path, f=f), "{p}/done/{f}".format(p=path, f=f))
except OSError:
print('{f} failed: not enough memory.'.format(f=f))