我想向在Heroku上运行的Django应用程序添加OCR功能。我怀疑最简单的方法是使用Tesseract。我注意到Tesseract的API有很多python包装器,但是在Heroku上安装并运行Tesseract的最佳方法是什么?通过诸如heroku-buildpack-tesseract之类的自定义buildpack ?
我将尝试捕获有关到达此处的解决方案的一些注释。
我的.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
这是对pdf文件进行OCR处理的python的关键部分:
# 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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句