pytesseract临时输出文件“没有这样的文件或目录”错误

问题描述 投票:1回答:2

我正在使用pytesseract行:

text = image_to_string(temp_test_file, 
                       lang='eng', 
                       boxes=False, 
                       config='-c preserve_interword_spaces=1 hocr')

并得到错误

pytesseract.py
135|  f = open(output_file_name, 'rb')

No such file or directory: 
/var/folders/j3/dn60cg6d42bc2jwng_qzzyym0000gp/T/tess_EDOHFP.txt

查看pytesseract here的源代码,似乎无法找到它用于存储tesseract命令输出的临时输出文件。

我在这里已经看到其他答案已经通过检查tesseract已经从命令终端安装和调用而得到解决,对我来说就是这样,所以这不是问题所在。任何想法可能是什么以及如何解决它?谢谢

python-2.7 tesseract python-tesseract
2个回答
1
投票

使用预期的输出格式来启动配置字符串:

config_str = "-l eng --oem 4 --psm 7"
text = pytesseract.image_to_string(img, config=("txt "+config_str))
# or for more meta-info:
data = pytesseract.image_to_data(img, config=("tsv "+config_str))

0
投票

事实证明,pytesseract无法找到临时输出文件的原因是它们存储的扩展名不是.txt或.box(它们是.hocr文件)。从源代码中,这些是pytesseract支持的唯一类型的tesseract输出文件(或更像是pytesseract'查找')。来源的相关摘要如下:

input_file_name = '%s.bmp' % tempnam() output_file_name_base = tempnam() if not boxes: output_file_name = '%s.txt' % output_file_name_base else: 123 output_file_name = '%s.box' % output_file_name_base

if status: errors = get_errors(error_string) raise TesseractError(status, errors) 135 f = open(output_file_name, 'rb')

看看pytesseract的github pulls,似乎计划对其他输出类型的支持但尚未实现(我用来显示为什么.hocr文件看起来无法找到的源代码是从pytesseract master分支复制/粘贴的)。

在那之前,我对pytesseract脚本进行了一些hackish更改,以支持多种文件类型。

此版本未设置输出文件的扩展名(因为tesseract会自动执行此操作)并查看pytesseract存储其临时输出文件的目录,并查找以输出文件名开头的文件(直到第一个')。由pytesseract指定的“字符”(不关心扩展名):

def tempnam():
    ''' returns a temporary file-name and directory '''
    tmpfile = tempfile.NamedTemporaryFile(prefix="tess_")
    return tmpfile.name, tempfile.tempdir


def image_to_string(image, lang=None, boxes=False, config=None, nice=0):
    if len(image.split()) == 4:
        # In case we have 4 channels, lets discard the Alpha.
        # Kind of a hack, should fix in the future some time.
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))
    (input_file_name, _) = tempnam() #'%s.bmp' % tempnam()
    input_file_name += '.bmp'
    (output_file_name_base, output_filename_base_dir) = tempnam()
    if not boxes:
        # Don’t put an extension on the output file name because Tesseract will do it automatically
        output_file_name = '%s' % output_file_name_base
    else:
        output_file_name = '%s.box' % output_file_name_base

    try:
        ########## DEBUGGING
        #print('input file name: %s' % input_file_name)
        #print('temp output name: %s' % output_file_name)
        #print('temp output dir: %s' % output_filename_base_dir)
        ##########

        image.save(input_file_name)
        status, error_string = run_tesseract(input_file_name,
                                             output_file_name_base,
                                             lang=lang,
                                             boxes=boxes,
                                             config=config,
                                             nice=nice)

        if status:
            errors = get_errors(error_string)
            raise TesseractError(status, errors)


        # find the temp output file in temp dir under whatever extension tesseract has assigned
        output_file_name += '.'
        output_file_name_leaf = os.path.basename(output_file_name)
        print('**output file starts with %s, type: %s' % (output_file_name, type(output_file_name)))
        l=os.listdir(output_filename_base_dir)
        for f in l:            
            if f.startswith(output_file_name_leaf):
                output_file_name_leaf = f
                break


        output_file_name_abs = os.path.join(output_filename_base_dir, output_file_name_leaf)
        f = open(output_file_name_abs, 'rb')
        try:
            return f.read().decode('utf-8').strip()
        finally:
            f.close()

    finally:
        cleanup(input_file_name)
        # if successfully created and opened temp output file
        if 'output_file_name_abs' in locals():
            output_file_name = output_file_name_abs
            print('**temp output file %s successfully created and deleted' % output_file_name)
        cleanup(output_file_name)

希望这有助于其他人。

© www.soinside.com 2019 - 2024. All rights reserved.