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 未安装在您的路径上。
Google App Engine 标准环境不适合您的使用案例。确实,pytesseract和Pillow库可以通过安装pip。但这些库需要安装tesseract-ocr和libtesseract-dev平台包,而这些包不包含在 App Engine 标准 Python3.7 运行时的基本运行时中。这会产生您收到的错误。
解决方案是使用Cloud Run,它将在 Docker 容器中运行您的应用程序,并且您将能够自定义运行时。我已修改此快速入门指南,以在 Cloud Run 上运行一个示例应用程序,该应用程序使用pytesseract.
我的文件夹结构:
\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\nRun Code Online (Sandbox Code Playgroud)\n\n这里是Dockerfile:
# 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\nRun Code Online (Sandbox Code Playgroud)\n\n内容app.py:
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)))\nRun Code Online (Sandbox Code Playgroud)\n\n这requirements.txt:
Flask==1.1.1\npytesseract==0.3.0\nPillow==6.2.0\nRun Code Online (Sandbox Code Playgroud)\n\n现在要容器化并部署您的应用程序,只需运行:
\n\ngcloud builds submit --tag gcr.io/<PROJECT_ID>/helloworld构建容器并将其提交到Container Registry。
gcloud beta run deploy --image gcr.io/<PROJECT_ID>/helloworld --platform managed将容器部署到 Cloud Run。