【问题标题】:Can not make tesseract work in google app engine with python3无法使用 python3 使 tesseract 在谷歌应用引擎中工作
【发布时间】:2020-03-04 15:20:37
【问题描述】:

我正在尝试在也具有 OCR 功能的 Google App Engine 上部署一个应用程序。我使用自制软件下载了 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 未安装在您的路径上。

【问题讨论】:

  • 您能否检查一下您是否在 requirements.txt 中指定了 tesseract 依赖项?你可以看到一个例子here。这是用于在 Python 中指定依赖项的documentation
  • 您是否尝试在应用启动代码中打印os.getcwd()?它可能不是你所期望的那样。也许试试os.chdir() 你的tesseract 在哪里?
  • 它们都不起作用。有人知道如何在 gcloud 应用引擎中添加路径吗?
  • 我也可以添加路径,还是不行。有没有办法在应用引擎上安装只能通过 brew 获得的包?

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


【解决方案1】:

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

解决方案是使用Cloud Run,它将在 Docker 容器中运行您的应用程序,您将能够自定义您的运行时。我已修改此 Quickstart guide 以在 Cloud Run 上运行一个示例应用程序,该应用程序使用 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2012-03-09
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多