我想将 OCR 功能添加到在 Heroku 上运行的 Django 应用程序。我怀疑最简单的方法是使用 Tesseract 。我注意到 Tesseract 的 API 有许多 python 包装器,但是在 Heroku 上安装和运行 Tesseract 的最佳方法是什么?通过像 heroku-buildpack-tesseract 这样的自定义构建包也许?
最佳答案
我将尝试记录一些关于我在这里找到的解决方案的注释。
我的.buildpacks
文件:
https://github.com/heroku/heroku-buildpack-python
https://github.com/clearideas/heroku-buildpack-ghostscript
https://github.com/marcolinux/heroku-buildpack-libraries
我的.buildpacks_bin_download
文件:
tesseract-ocr https://s3.amazonaws.com/tesseract-ocr/heroku/tesseract-ocr-3.02.02.tar.gz 3.02 eng,spa
这是Python中对pdf文件进行OCRing的关键部分:
# Additional processing
document_path = Path(str(document.attachment_file))
if document_path.ext == '.pdf':
working_path = Path('temp', document.directory)
working_path.mkdir(parents=True)
input_path = Path(working_path, name)
input_path.write_file(document.attachment_file.read(), 'w')
rb = ReadBot()
args = [
'VBEZ',
# '-sDEVICE=tiffg4',
'-sDEVICE=pnggray',
'-dNOPAUSE',
'-r600x600',
'-sOutputFile=' + str(working_path) + '/page-%00d.png',
str(input_path)
]
ghostscript.Ghostscript(*args)
image_paths = working_path.listdir(pattern='*.png')
txt = ''
for image_path in image_paths:
ocrtext = rb.interpret(str(image_path))
txt = txt + ocrtext
document.notes = txt
document.save()
working_path.rmtree()
关于python - 在 Heroku 上使用 Tesseract 和 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19521976/