gpt4 book ai didi

python - 在 pytesseract 中运行示例代码

转载 作者:行者123 更新时间:2023-12-01 03:37:49 26 4
gpt4 key购买 nike

我正在运行 python 2.6.6 并想要安装 pytesseract包裹。提取并安装后,我可以从命令行调用 pytesseract。不过我想在 python 中运行 tesseract。我有以下代码(ocr.py):

try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('test.png')))
print(pytesseract.image_to_string(Image.open('test-european.jpg'),lang='fra'))

当我通过 python ocr.py 运行代码时,我得到以下输出:

Traceback (most recent call last):
File "ocr.py", line 6, in <module>
print(pytesseract.image_to_string(Image.open('test.png')))
File "/pytesseract-0.1.6/build/lib/pytesseract/pytesseract.py", line 164, in image_to_string
raise TesseractError(status, errors)
pytesseract.TesseractError: (2, 'Usage: python tesseract.py [-l language] input_file')

test.png 和 test-european.jpg 位于工作目录中。有人可以帮我运行这段代码吗?我尝试过以下方法:

  1. 将 tesseract_cmd 调整为“pytesseract”
  2. 已安装 tesseract-ocr

感谢任何帮助,因为我现在正在尝试解决这个问题几个小时。

最佳答案

tesseract_cmd 应指向命令行程序 tesseract ,而不是pytesseract

例如在 Ubuntu 上,您可以使用以下方式安装该程序:

sudo apt install tesseract-ocr

然后将变量设置为 tesseract/usr/bin/tesseract

关于python - 在 pytesseract 中运行示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40131630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com