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

无法使tesseract在使用python3的google应用程序引擎中工作,python,python-3.x,google-app-engine,gcloud,python-tesseract,Python,Python 3.x,Google App Engine,Gcloud,Python Tesseract,我正在尝试在谷歌应用程序引擎上部署一个同样具有OCR功能的应用程序。我使用自制软件下载了tesseract,并使用pytesseract以Python进行包装。OCR功能在我的本地系统上工作,但在我将应用程序上载到Google应用程序引擎时不起作用 我从usr/local/ceral/tesseract复制了tesseract文件夹,并粘贴到我的应用程序的工作目录中。我将tesseract文件和pytesseract文件上传到app engine。我已使用os.getcwd()指定了tesser

我正在尝试在谷歌应用程序引擎上部署一个同样具有OCR功能的应用程序。我使用自制软件下载了tesseract,并使用
pytesseract
以Python进行包装。OCR功能在我的本地系统上工作,但在我将应用程序上载到Google应用程序引擎时不起作用

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

来自pyteseract.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


Google App Engine标准环境不适合您的用例。确实,可以通过
pip
安装和库。但是这些库和平台包需要安装,它们不在应用程序引擎标准Python3.7运行时的基本运行时中。这会产生您正在得到的错误

解决方案是使用,它将在Docker容器中运行您的应用程序,并且您将能够自定义您的运行时。我修改了它,在云上运行一个示例应用程序,该应用程序使用
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
现在要对应用程序进行容器化和部署,只需运行:

  • gcloud builds submit——标记gcr.io//helloworld
    以生成并提交容器

  • gcloud beta run deploy--image gcr.io//helloworld--platform managed
    将容器部署到云运行


  • 能否检查您是否已在requirements.txt中指定了tesseract依赖项?你可以看到一个例子。以下是在Python中指定依赖项的方法。是否尝试在应用程序启动代码中打印
    os.getcwd()
    ?这可能不是你所期望的。也许可以尝试
    os.chdir()
    到您的
    teseract
    所在的位置?它们都不起作用。有人知道如何在gcloud应用程序引擎中添加路径吗?我也能够添加路径,但仍然无法工作。有没有办法在app engine上安装仅通过brew提供的软件包?