Python验证码识别 安装Pillow、tesseract-ocr与pytesseract模块的安装以及错误解决

1.安装Pillow

pip install Pillow

2.安装tesseract-ocr

 OCR(Optical Character Recognition, 光学字符识别) 软件

安装包含两个部分:ORC引擎本身以及对应语言的训练数据

 

github地址:   https://github.com/tesseract-ocr/tesseract

You can either Install Tesseract via pre-built binary package or build it from source.

windows:

The latest installer can be downloaded here: tesseract-ocr-setup-3.05.01.exe and tesseract-ocr-setup-4.00.00dev.exe (experimental). 

 

ubuntu 16.04:

默认安装的OCR引擎版本是3.04,因此需要安装4.0版本的,则需要:

sudo add-apt-repository ppa:alex-p/tesseract-ocr
sudo apt-get update

 

复制代码
# 安装OCR引擎
sudo apt-get install tesseract-ocr
# 安装训练数据(equ为数学公式包)
sudo apt-get install tesseract-ocr-eng tesseract-ocr-chi-sim  tesseract-ocr-equ

# 可选安装Leptonica
sudo apt-get install liblept5  libleptonica-dev
复制代码

 

traineddata存放路径:  $TESSDATA_PREFIX/testdata

3.04版本  ./usr/share/tesseract-ocr/tessdata/

4.0版本  /usr/share/tesseract-ocr/4.00/tessdata/

此外,训练数据还可在通过 tessdata repository 进行下载

 

3.安装pytesseract

pip install pytesseract

 

遇到的问题:

1.FileNotFoundError: [WinError 2] 系统找不到指定的文件

解决方法:

方法1[推荐]: 将tesseract.exe添加到环境变量PATH中,

例如: D:\Tesseract-OCR,默认路径为C:\Program Files (x86)\Tesseract-OCR

注意: 为了使环境变量生效,需要关闭cmd窗口或是关闭pycharm等ide重新启动

方法2: 修改pytesseract.py文件,指定tesseract.exe安装路径

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe‘

方法3:  在实际运行代码中指定

pytesseract.pytesseract.tesseract_cmd = 'D:\\Tesseract-OCR\\tesseract.exe'

 

2.pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Tesseract-OCR\\tessdata/eng.traineddata')

 解决方法:

方法1[推荐]: 

将tessdata目录的上级目录所在路径(默认为tesseract-ocr安装目录)添加至TESSDATA_PREFIX环境变量中

例如: C:\Program Files (x86)\Tesseract-OCR

Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory.

 

方法2:  在.py文件配置中指定tessdata-dir

tessdata_dir_config = '--tessdata-dir "D:\\Tesseract-OCR\\tessdata"'
# tessdata_dir_config = '--tessdata-dir "'C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'
pytesseract.image_to_string(image, config=tessdata_dir_config)

 

参考文档:

https://pypi.python.org/pypi/pytesseract

https://github.com/tesseract-ocr/tesseract/wiki

 #####################################

转自:https://www.cnblogs.com/hupeng1234/p/7136442.html

猜你喜欢

转载自blog.csdn.net/qq_41262248/article/details/80966576