【发布时间】:2021-06-22 13:01:25
【问题描述】:
我是 pytesseract 和 OCR 的新手,我在互联网上搜索了这是用于从图像中提取文本的工具。但是,我对这个工具一无所知。现在,我遇到了这个错误: tesseract 没有安装或者它不在你的 PATH 中。有关详细信息,请参阅 README 文件。
我不知道如何解决这个问题,我尝试了在互联网上找到的各种解决方案,但不幸的是没有奏效。
错误代码:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
~/.local/lib/python3.9/site-packages/pytesseract/pytesseract.py in run_tesseract(input_filename, output_filename_base, extension, lang, config, nice, timeout)
254 try:
--> 255 proc = subprocess.Popen(cmd_args, **subprocess_args())
256 except OSError as e:
/opt/conda/lib/python3.9/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
950
--> 951 self._execute_child(args, executable, preexec_fn, close_fds,
952 pass_fds, cwd, env,
/opt/conda/lib/python3.9/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, gid, gids, uid, umask, start_new_session)
1822 err_msg = os.strerror(errno_num)
-> 1823 raise child_exception_type(errno_num, err_msg, err_filename)
1824 raise child_exception_type(err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'tesseract'
During handling of the above exception, another exception occurred:
TesseractNotFoundError Traceback (most recent call last)
<ipython-input-7-96e86f1cd397> in <module>
1 img = cv2.imread("Z++¦hler NSHV KTL-Durchlaufanlage-1.jpg")
----> 2 data = pytesseract.image_to_string(img)
3 print(data)
4 # plt.imshow(img)
~/.local/lib/python3.9/site-packages/pytesseract/pytesseract.py in image_to_string(image, lang, config, nice, output_type, timeout)
407 args = [image, 'txt', lang, config, nice, timeout]
408
--> 409 return {
410 Output.BYTES: lambda: run_and_get_output(*(args + [True])),
411 Output.DICT: lambda: {'text': run_and_get_output(*args)},
~/.local/lib/python3.9/site-packages/pytesseract/pytesseract.py in <lambda>()
410 Output.BYTES: lambda: run_and_get_output(*(args + [True])),
411 Output.DICT: lambda: {'text': run_and_get_output(*args)},
--> 412 Output.STRING: lambda: run_and_get_output(*args),
413 }[output_type]()
414
~/.local/lib/python3.9/site-packages/pytesseract/pytesseract.py in run_and_get_output(image, extension, lang, config, nice, timeout, return_bytes)
285 }
286
--> 287 run_tesseract(**kwargs)
288 filename = kwargs['output_filename_base'] + extsep + extension
289 with open(filename, 'rb') as output_file:
~/.local/lib/python3.9/site-packages/pytesseract/pytesseract.py in run_tesseract(input_filename, output_filename_base, extension, lang, config, nice, timeout)
257 if e.errno != ENOENT:
258 raise e
--> 259 raise TesseractNotFoundError()
260
261 with timeout_manager(proc, timeout) as error_string:
TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
对应代码:
!pip install tesseract
import pytesseract
import cv2
from PIL import Image
import matplotlib.pyplot as plt
img = cv2.imread("meter.jpg")
data = pytesseract.image_to_string(img)
print(data)
# plt.imshow(img)
首先让我告诉你我正在使用 Jupyterhub。实际上,我在我大学的 jupyterhub 上注册了一个帐户。此外,我在网上搜索了可以使用 'cmd' 并解决问题的地方。如果是这样,请告诉我该怎么做,否则我必须联系 Uni 管理员来解决这个问题。任何帮助表示赞赏!
【问题讨论】:
-
您需要 tesseract 二进制文件才能运行 pytesseract,下载并设置路径:pytesseract.pytesseract.tesseract_cmd = '/path/to/tesseract/bin'
-
在哪里可以找到 tesseract 二进制文件?
-
您可以自己编译,也可以使用操作系统存储库中已编译的版本:sudo apt install tesseract-ocr && sudo apt install libtesseract-dev。如果你这样安装,我想你不需要运行 pytesseract.pytesseract.tesseract_cmd
-
我确实添加了 tesseract-ocr && sudo apt install libtesseract-dev,但我被要求输入密码。如何解决这个问题,是的,我不想被问到这个问题?
-
好吧,它要求您输入帐户密码(如果您在 sudoers 文件中),只需在此处输入您的帐户密码。如果你不想要,你需要自己编译它:tesseract-ocr.github.io/tessdoc/Compiling.html。不容易,所以最好使用包管理器(apt)安装它
标签: python tesseract python-tesseract