python中使用pytesseract,遇到以下错误:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path,本文主要介绍解决此错误的方法。

执行代码:

import cv2
import pytesseract

img = cv2.imread('1d.png')
print(pytesseract.image_to_string(img))

报错信息:

raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

问题原因:

使用pip安装了pytesseract,但忘记安装tesseract二进制文件。

1、Linux上安装命令

sudo apt update
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev

2、Mac安装命令

brew install tesseract

3、Windows上安装命令

https://github.com/UB-Mannheim/tesseract/wiki下载二进制文件。然后添加pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'到脚本中。(如有必要,须替换tesseract二进制文件的路径)

参考文档: https://pypi.org/project/pytesseract/(安装部分)和 https://github.com/tesseract-ocr/tesseract/wiki#installation

推荐文档