无法使用python3使tesseract在google app engine中工作

2024-07-03 06:59:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Google应用引擎上部署一个同样具有OCR功能的应用程序。我用自制软件下载了tesseract,并用pytesseract包装在Python中。OCR功能在我的本地系统上工作,但是当我把应用程序上传到googleappengine时就不行了。在

我从usr/local/celal/tesseract复制了tesseract文件夹,并粘贴到我的应用程序的工作目录中。我上传了tesseract文件和pytesseract文件到appengine。我用os.getcwd()指定了tesseract的路径,这样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]

Google App Engine的错误是:

tesseract is not installed on your path.


Tags: 文件pathname功能目录app应用程序os
1条回答
网友
1楼 · 发布于 2024-07-03 06:59:16

googleappengine标准环境不适合您的用例。确实,^{}和{a2}库可以通过pip安装。但是要安装这些库require the ^{} and ^{}平台包,它们不在appengine标准Python3.7运行时的基本运行时中提供。这就产生了错误。在

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

我的文件夹结构:

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

这是Dockerfile

^{pr2}$

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将容器部署到云运行。

相关问题 更多 >