pytesseract 临时输出文件 "No such file or directory" 错误
pytesseract temporary output files "No such file or directory" error
我在以下行中使用 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 是否已安装并且可以从命令终端调用来解决,对我来说是这样,所以这不是这里的问题。任何想法这可能是什么以及如何解决它?谢谢
事实证明,pytesseract 无法找到临时输出文件的原因是它们以 .txt 或 .box 以外的扩展名存储(它们是 .hocr 文件)。从源代码来看,这些是 pytesseract 支持的唯一类型的 tesseract 输出文件(或更像 pytesseract 的 'looked for')。来源中的相关片段如下:
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 文件似乎不支持的源代码发现 copy/pasted 来自 pytesseract master 分支)。
在那之前,我对 pytesseract 脚本进行了一些骇人听闻的更改以支持多种文件类型。
此版本没有为输出文件设置扩展名(因为 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)
希望这对其他人有帮助。
使用预期的输出格式启动您的配置字符串:
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))
我在以下行中使用 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 是否已安装并且可以从命令终端调用来解决,对我来说是这样,所以这不是这里的问题。任何想法这可能是什么以及如何解决它?谢谢
事实证明,pytesseract 无法找到临时输出文件的原因是它们以 .txt 或 .box 以外的扩展名存储(它们是 .hocr 文件)。从源代码来看,这些是 pytesseract 支持的唯一类型的 tesseract 输出文件(或更像 pytesseract 的 'looked for')。来源中的相关片段如下:
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 文件似乎不支持的源代码发现 copy/pasted 来自 pytesseract master 分支)。
在那之前,我对 pytesseract 脚本进行了一些骇人听闻的更改以支持多种文件类型。
此版本没有为输出文件设置扩展名(因为 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)
希望这对其他人有帮助。
使用预期的输出格式启动您的配置字符串:
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))