无法使用 python3 使 tesseract 在 Google 应用程序引擎中工作

hur*_*ane 7 python google-app-engine python-3.x python-tesseract gcloud

我正在尝试在 Google App Engine 上部署一个也具有 OCR 功能的应用程序。我使用 homebrew 下载了 tesseract,并使用pytesseractPython 进行包装。OCR 功能在我的本地系统上有效,但当我将应用程序上传到 Google App Engine 时却不起作用。

tesseract从 usr/local/cellar/tesseract 复制文件夹并粘贴到我的应用程序的工作目录中。我将超正方文件和pytesseract文件上传到应用程序引擎。我已经指定了 tesseract 的路径,os.getcwd()以便pytesseract可以找到它。然而,这是行不通的。App Engine 找不到要执行的文件,因为它们不在同一目录 ( os.getcwd()) 中。

代码来自 pytesseract.py

cmda = os.getcwd()
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY


def find_all(name, path):
    result = []
    for root, dirs, files in os.walk(path):
        if name in files:
            result.append(os.path.join(root, name))
    return result

founds = find_all("tesseract",cmda)

tesseract_cmd = founds[0]
Run Code Online (Sandbox Code Playgroud)

Google App Engine 的错误是:

tesseract 未安装在您的路径上。

llo*_*les 9

Google App Engine 标准环境不适合您的使用案例。确实,pytesseractPillow库可以通过安装pip。但这些库需要安装tesseract-ocrlibtesseract-dev平台包,而这些包不包含在 App Engine 标准 Python3.7 运行时的基本运行时中。这会产生您收到的错误。

\n\n

解决方案是使用Cloud Run,它将在 Docker 容器中运行您的应用程序,并且您将能够自定义运行时。我已修改此快速入门指南,以在 Cloud Run 上运行一个示例应用程序,该应用程序使用pytesseract.

\n\n

我的文件夹结构:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sample\n \xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 requirements.txt\n \xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Dockerfile\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 app.py\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.png\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里是Dockerfile

\n\n
# Use the official Python image.\n# https://hub.docker.com/_/python\nFROM python:3.7\n\n# Copy local code to the container image.\nENV APP_HOME /app\nWORKDIR $APP_HOME\nCOPY . ./\n\n# Install production dependencies.\nRUN pip install Flask gunicorn\nRUN pip install -r requirements.txt\n\n#Install tesseract\nRUN apt-get update -qqy && apt-get install -qqy \\\n        tesseract-ocr \\\n        libtesseract-dev\n\n# Run the web service on container startup. Here we use the gunicorn\n# webserver, with one worker process and 8 threads.\n# For environments with multiple CPU cores, increase the number of workers\n# to be equal to the cores available.\nCMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app\n
Run Code Online (Sandbox Code Playgroud)\n\n

内容app.py

\n\n
from flask import Flask\nfrom PIL import Image\nimport pytesseract\n\n\n# If `entrypoint` is not defined in app.yaml, App Engine will look for an app\n# called `app` in `main.py`.\napp = Flask(__name__)\n\n@app.route(\'/\')\ndef hello():\n    return pytesseract.image_to_string(Image.open(\'test.png\'))\n\n\nif __name__ == "__main__":\n    app.run(debug=True,host=\'0.0.0.0\',port=int(os.environ.get(\'PORT\', 8080)))\n
Run Code Online (Sandbox Code Playgroud)\n\n

requirements.txt

\n\n
Flask==1.1.1\npytesseract==0.3.0\nPillow==6.2.0\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在要容器化并部署您的应用程序,只需运行:

\n\n
    \n
  1. gcloud builds submit --tag gcr.io/<PROJECT_ID>/helloworld构建容器并将其提交到Container Registry

  2. \n
  3. gcloud beta run deploy --image gcr.io/<PROJECT_ID>/helloworld --platform managed将容器部署到 Cloud Run。

  4. \n
\n