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

标签 python python-3.x google-app-engine gcloud python-tesseract

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

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

Google App Engine 的错误是:

tesseract is not installed on your path.

最佳答案

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

解决方案是使用 Cloud Run ,它将在 Docker 容器中运行您的应用程序,您将能够自定义您的运行时。我修改了这个Quickstart guide在 Cloud 上运行 运行一个示例应用程序,使用 pytesseract 将图像转换为文本。

我的文件夹结构:

├── sample
    ├── requirements.txt
    └── Dockerfile
    └── app.py
    └── test.png

这是 Dockerfile :

# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install production dependencies.
RUN pip install Flask gunicorn
RUN pip install -r requirements.txt

#Install tesseract
RUN apt-get update -qqy && apt-get install -qqy \
        tesseract-ocr \
        libtesseract-dev

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

app.py的内容:

from flask import Flask
from PIL import Image
import pytesseract


# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)

@app.route('/')
def hello():
    return pytesseract.image_to_string(Image.open('test.png'))


if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))

requirements.txt :

Flask==1.1.1
pytesseract==0.3.0
Pillow==6.2.0

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

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

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

关于python - 无法使用 python3 使 tesseract 在 Google 应用程序引擎中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57869385/

相关文章:

python - 如何获得 Jinja2 模板使用的所有文件的列表?

python - 如何使用 REST Framework JWT 测试身份验证?

python-3.x - 如何同时运行两个脚本

python - 将文件上传到 Google Drive 上的公开可写文件夹 - 使用 Python 并且无需通过身份验证?

google-app-engine - gcloud 应用程序部署失败 "cannot import internal package"

java - 在列表中映射 POJO 时,为什么 Cloud Endpoints 会抛出 com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException?

python - TweePy - 如何隐藏 API key

python - C++ 中等效的 opencv python 代码

python - 调用 locale.strxfrm 时 Unicode 字符不在范围内

python-3.x - 为什么 Python 列表推导最后会打印一个 "None"的列表?