python - 在 Heroku 上使用 Tesseract 和 Django

标签 python django heroku tesseract

我想将 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/

相关文章:

python - 不同阶段有不同的 initial_data fixture(测试与生产)

heroku - 更新 Heroku CLI 时出错 : write lock exists: update

javascript - Heroku 上未知的代币传播运算符,在本地工作

python - 使用 numpy.reshape() 反转 skimage view_as_blocks()

python - 如何从数据库中同时检索对象?

python - 如何使用 App Engine 获取 Jinja2 中继承模板的特定回溯?

python - 强制setuptools使用dependency_links来安装mysqlclient

python - Django 模型权限与组权限

ssl - 如果没有 "www",Namecheap 域将不会重定向

python - 表单返回 <django.db.models.query_utils.DeferredAttribute object at 0x10e6ee898>