带有tesseract的pyocr内存不足

时间:2017-03-28 14:17:54

标签: python memory-leaks python-tesseract

我制作了一个脚本,在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))

1 个答案:

答案 0 :(得分:2)

我也有同样的问题,最后整理出来。 真正的问题不在于pyocr,而在于sequence wand.image.Image

您可以使用destroy()对象的Image方法释放内存。在处理魔杖时始终使用with语句。

关于此主题已有问题herehere

这是我的代码,它将pdf转换为图像blob,如果它可以帮助你

def convert_pdf_to_image_blob(pdf):
    req_image = []
    with WI(filename=pdf, resolution=150) as image_jpeg:
        image_jpeg.compression_quality = 99
        image_jpeg = image_jpeg.convert('jpeg')

        for img in image_jpeg.sequence:
            with WI(image=img) as img_page:
                req_image.append(img_page.make_blob('jpeg'))
    image_jpeg.destroy()  # frees memory used by Image object. 
    return req_image

由于