python tesserocr 包的安装

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

tesserocr 是python的一个ocr 识别库,但其实是对tesseract 做的一层Python API封装,所以它的核心是tesseract.

因此安装tesserocr 之前,我们需要安装tesseract.

 

windows 下安装:

地址列表:

下载地址 :http://digi.bib.uni-mannheim.de/tesseract  

官方语言包下载地址:https://github.com/tesseract-ocr/tessdata

语言包地址: https://github.com/tesseract-ocr/tesseract/wiki/Data-Files

简体字识别包:https://raw.githubusercontent.com/tesseract-ocr/tessdata/4.00/chi_sim.traineddata

繁体字识别包:https://github.com/tesseract-ocr/tessdata/raw/4.0/chi_tra.traineddata

 

1.下载安装

安装tesseract

其中文件名中带有dev 的为开发版本,不带dev 的为稳定版本,可以选择不带dev 的版本。

选中 Additional language data ,可以支持多国语言。

安装tesserocr

pip3 install tesserocr pillow

发现安装失败:

Command "c:\users\gezongyang\appdata\local\programs\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\GEZONG~1\\AppData\\Local
\\Temp\\pip-install-hu3gfkjz\\tesserocr\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code,
__file__, 'exec'))" install --record C:\Users\GEZONG~1\AppData\Local\Temp\pip-record-p1__walc\install-record.txt --single-version-externally-managed --compile
" failed with error code 1 in C:\Users\GEZONG~1\AppData\Local\Temp\pip-install-hu3gfkjz\tesserocr\

这时pip安装就不是正确选择,还可以使用 whl文件 进行安装

下载地址:https://github.com/simonflueckiger/tesserocr-windows_build/releases

 

查看Python对应的whl文件版本

cmd中输入:python

>>>import pip
>>>import pip._internal
>>>print(pip._internal.pep425tags.get_supported())

可以看到支持的版本:

[('cp36', 'cp36m', 'win_amd64'), ('cp36', 'none', 'win_amd64'), ('py3', 'none', 'win_amd64'), ('cp36', 'none', 'any'), ('cp3', 'none', 'any'), ('py36', 'none', 'any'), ('py3', 'none', 'any'), ('py35', 'none', 'any'), ('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')]
>>>

支持的版本:‘cp36', 'cp36m', 'win_amd64'

如果安装的版本不对:

会报如下错误:

Requirement 'tesserocr-2.3.1-cp37-cp37m-win_amd64.whl' looks like a filename, but the file does not exist
tesserocr-2.3.1-cp37-cp37m-win_amd64.whl is not a supported wheel on this platform.

选择对应的版本并安装:

把下载的文件拷贝到Script 文件夹下并来到此目录执行安装命令:

cd C:\Users\gezongyang\AppData\Local\Programs\Python\Python36\Scripts

pip3 install tesserocr-2.4.0-cp36-cp36m-win_amd64.whl

(查看python 的安装目录:

import sys
sys.path
结果:
['', 'C:\\Python27\\Lib\\idlelib', 'C:\\Python27\\lib\\site-packages\\robotframework_selenium2library-1.5.0-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\docutils-0.14-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\decorator-4.2.1-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']

)

 

还需要安装:

pip install pillow

测试是否安装成功:

import tesserocr
from PIL import Image

image = Image.open('code.jpg')
result = tesserocr.image_to_text(image)
print(result)

报错:

Traceback (most recent call last):
  File "D:/mymodule/checkcode/checktest.py", line 5, in <module>
    result = tesserocr.image_to_text(image)
  File "tesserocr.pyx", line 2443, in tesserocr._tesserocr.image_to_text
RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\gezongyang\AppData\Local\Programs\Python\Python36\/tessdata/

解决方案: 

拷贝tessdata/ 到报错的目录下:

 

linux 下安装:

 

Ubuntu:

sudo apt-get install -y tesseract-ocr libtesseract-dev libleptonica-dev

CentOS, Red Hat

 

yum install -y tesseract

 

查看支持的语言:

tesseract --list-langs

 

利用git 命令将其下载下来,并迁移到相关目录

 

Ubuntu

git clone https://github.com/tesseract-ocr/tessdata.git

sudo mv tessdata/* /usr/share/tesseract-ocr/tessdata

 

Centos

git clone https://github.com/tesseract-ocr/tessdata.git

sudo mv tessdata/* /usr/share/tesseract/tessdata

这样就可以将下载好的语言包全部安装:

tesseract --list-langs

结果如下:

 

接下来安装tesserocr 即可,这里直接使用 pip 安装:

pip3 install tesserocr pillow

 

安装完成!!!!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独行客-编码爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值