Tesseract 3.x多处理奇怪的行为

Lai*_*kus 5 python tesseract gunicorn python-tesseract

我不确定这种奇怪的东西或者tesseract-ocr本身是不是我的基础设施.

每当我在单进程环境中使用image_to_stirng时 - tesseract-ocr工作正常.但是当我用gunicorn产生多个工作人员并且所有人都开始使用ocr读取工作时 - tesseract-ocr开始阅读非常差(而不是来自性能老虎钳,但不是精确的老虎钳).即使在负载完成后 - tesseract也没有相同的准确性.我需要重新启动所有工人才能让tesseract再次运转良好.

这太超级怪了.也许有人已经过期或听说过这个问题?

Leo*_*o K 2

(注意,下面的信息基于对 pytesseract.py 代码的审查,我没有尝试设置多进程测试来检查)

有几个与tesseract-ocr. 您可能正在使用pytesseract(通过image_to_string函数猜测)。

该库将 tesseract-ocr 二进制文件调用为子进程,并使用临时文件与其连接。它使用过时的 tempfile.mktemp(),不能保证唯一的文件名 - 此外,它甚至不按原样使用返回的文件名,因此第二次调用tempfile.mktemp()可以轻松返回相同的文件名。

考虑为 tesseract 使用不同的 python 接口库:例如,pip install tesseract-ocrpython-tesseract来自 Google ( https://code.google.com/archive/p/python-tesseract/ )。

(如果问题实际上出在临时文件上,正如我怀疑的那样)您可以通过为每个生成的工作进程设置不同的临时目录来解决此问题:

td = tempfile.mkdtemp()
tempfile.tempdir = td
try:
    # your-code-calling pytesseract.image_to_string() or similar
finally:
    os.rmdir(td)
    tempfile.tempdir = None
Run Code Online (Sandbox Code Playgroud)