それはpytesseractが一時出力ファイルを見つけることができなかったことを理由に、彼らは(彼らは.hocrファイルだった).txtファイルまたは■は以外の拡張子で保存されていたことだったことが判明。ソースコードから、これらはpytesseractでサポートされている唯一の種類のtesseract出力ファイルです(または、pytesseractによって「探した」など)。ソースから関連するスニペットは以下の通りです:
if status: errors = get_errors(error_string) raise TesseractError(status, errors) 135 f = open(output_file_name, 'rb')
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
がpytesseractのgithubの
pullsを見ると、それはまだ実装されていない計画されている他の出力タイプのサポート(私が使用したソースコードのように思えます.hocrファイルが見つからないように見える理由を表示するには、pytesseractマスターブランチからコピー/ペーストしてください)。
それまでは、複数のファイルタイプをサポートするために、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)
これは他人に役立つことを望みます。