python-OCR图片识别库--tesserocr

1 介绍

OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程。对于图形验证码来说,它们都是一些不规则的字符,这些字符确实是由字符稍加扭曲变换得到的内容。

2. 条件

tesserocr 安装之前必须安装 tesseract

相关链接:

相关学习信息:

  1. Python tesserocr模块使用示例
  2. win10下安装tesserocr失败(问题已解决,见文末)

3. 安装出现的问题

查看安装 tesseract-ocr 和 tesserocr 以及出现的问题

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

[('cp37', 'cp37m', 'win32'), ('cp37', 'none', 'win32'), ('py3', 'none', 'win32'), ('cp37', 'none', 'any'), ('cp
3', 'none', 'any'), ('py37', 'none', 'any'), ('py3', 'none', 'any'), ('py36', 'none', 'any'), ('py35', 'none',
'any'), ('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('p
y30', 'none', 'any')]

原来是py37, win32

通过pip3 install tesserocr-2.2.2-cp36-cp36m-win32.whl, 在window 上 还是报错。

4. 解决tesseract安装问题

  • 官方推荐:
Installation
Windows
The proposed downloads consist of stand-alone packages containing all the Windows libraries needed for execution. This means that no additional installation of tesseract is required on your system.
#### pip

Download the wheel file corresponding to your Windows platform and Python installation from [simonflueckiger/tesserocr-windows_build/releases](https://github.com/simonflueckiger/tesserocr-windows_build/releases) and install them via:
pip install <package_name>.whl

这里的package_name, 是从(https://github.com/sirfz/tesserocr/releases)
下载的,对应自己的版本与环境。

下载之后,安装之后,调试代码还是报错

File "tesserocr.pyx", line 2401, in tesserocr._tesserocr.image_to_textRuntimeError: Failed to init API, possibly an invalid tessdata path: C:\\

NO
原因: :stand-alone packages 虽然包含了 Windows 下所需的所有库,但并是不包含语言数据文件(language data files)。并且数据文件需要被统一放置在 tessdata\ 文件夹中,并置于 C:\Python36 内。

  • 实践
    无需安装 tesseract ,只需克隆 tesseract 仓库的主分支,然后将其中的 tessdata\ 文件夹复制到 Python36\中。接下来,通过 tessdata_fast 仓库下载 eng.traineddata 语言文件,并放置于 tessdata\ 内即可。
2223200-fe5c804395605a36.png
image.png
  • 代码
from PIL import Image
import tesserocr

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

# 有些读取不出,需要二值化去杂

PS: 若你觉得可以、还行、过得去、甚至不太差的话,可以“关注或点赞”一下,就此谢过!

转载于:https://www.jianshu.com/p/e542e532a001

猜你喜欢

转载自blog.csdn.net/weixin_34082789/article/details/91244014