首页 > 解决方案 > 使用 Pytesseract 从图像中读取文本会导致路径错误

问题描述

我正在尝试使用 pytesseract 从图像中读取文本。我正在使用 mac。我已经用 pip 安装了 pytesseract。

import cv2
import pytesseract

img = cv2.imread('slika1.png')
text = pytesseract.image_to_string(img)
print(text)

它给了我这个错误:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

当我这样做时:

import importlib.util
print(importlib.util.find_spec('pytesseract'))

它打印:

ModuleSpec(name='pytesseract', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7f8a7837c160>, origin='/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract/__init__.py', submodule_search_locations=['/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytesseract'])

那我该怎么办,我做错了什么?有没有其他方法可以从图像中读取文本?

标签: pythonopencvcomputer-visionpython-tesseract

解决方案


尝试打开模块源文件(以管理员身份)并编辑 Tesseract exe 文件的路径 - 如果需要,将其设置为绝对路径。它应该是顶行中的 const 。

像这样的东西(在Win上):

"C:\Program Files\Python36\Lib\site-packages\pytesseract\pytesseract.py" 设置路径:... pytesseract.tesseract_cmd = r"D:\OCR\tesseract.exe"

https://github.com/Twenkid/ComputerVision_Pyimagesearch_OpenCV_Dlib_OCR-Tesseract-DL/blob/master/OCR_Tesseract/ocr.py


推荐阅读