我正在尝试在我的python脚本中使用pytesseract来读出图像中的一串文本,但我不断收到错误。我现在正在尝试这段代码:
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files
(x86)\\Tesseract-OCR\\tesseract'
# Include the above line, if you don't have tesseract executable in
#your PATH
# Example tesseract_cmd: 'C:\\Program Files (x86)\\Tesseract-
#OCR\\tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('IMG_9296.jpg')))
IMG_9296.jpg文件位于我的桌面上:~/Desktop
。我已经安装了tesseract和pytesseract,因为在我的命令行输入tesseract
会返回有关它的信息。当我在命令行输入pip install pytesseract
时,我会回来:
Requirement already satisfied: pytesseract in /Library/Python/2.7/site-packages
Requirement already satisfied: Pillow in /Library/Python/2.7/site-packages (from pytesseract)
所以我猜测/Library/Python/2.7/site-packages
是我的pytesseract之路,所以我尝试将它放入pytesseract.pytesseract.tesseract_cmd
,但这不起作用。无论哪种方式我都得到这个错误(我想从import pytesseract
线):
ValueError: Attempted relative import in non-package
我是否需要进入不同的路径,或在某处移动/复制pytesseract?我不确定发生了什么事。
首先通过输入Code检查命令提示符
!pip view pytesseract
如果它返回一些信息意味着你在系统中有它,你还需要看到PIL模块。
现在来看你的主要问题:
为了pytesseact你需要在pytesseract.pytesseract.tesseract_cmd路径: - 而且你需要在路径上以及图像所在的位置。 所以你可以做的就是如下
进口pytesseract 导入cv2 import os os.chdir(FullPath_where_your_“tesseract.exe”所在位置) image = cv2.imread('full_path_of_your_image') pytext = pytesseract.image_to_string(image) 打印(pytext的)
如果不能正常工作,请告诉我 快乐学习!!