19.验证码识别

Tesseract

tesseract是一个ORC(optical charactor recognition)库,目前由谷歌赞助。tesseract是目前公认最优秀,最准确的开源OCR库。tesseract具有很高的识别度,也具有很高的灵活度,他可以通过训练识别任何字体。

安装

  • 下载地址:https://github.com/tesseract-ocr/tesseract/wiki
    设置环境变量:
    安装完成后,如果想要在命令行中使用tesseract,那么应该设置环境变量。Mac和Linux在安装的时候已经设置好了,在window下把tesseract.exe所在的路径添加到path环境变量中。
  • 下载语言训练包:https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
    将下载的语言包放到tessdata目录下(没有的话自己新建,tesseract默认读取这个文件夹下的英文训练包)
    查看已安装的语言训练包
    image.png

tesseract识别图像:

tesseract a.png abc

a.png是要识别的图片,abc为识别后输出的文本。运行完成后,则多出abc.txt,记录着识别出的文字。

python中使用tesseract

在python中操作tesseract,需要安装一个库,叫做pytesseract。通过pip安装即可:

pip install tesseract

并且需要读取图片,需要借助第三方库叫做PIL。通过pip list看下是否安装。如果没有安装,通过:

pip install PIL

使用pytesseract将图片上的文字转换成文本文字:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r"D:\tesseract-Win64\tesseract.exe"

image = Image.open(r'D:\tesseract-Win64\1.png')

text = pytesseract.image_to_string(image)
# 选择其他语言
# text = pytesseract.image_to_string(image,lang='xxx')

print(text)

你可能感兴趣的:(19.验证码识别)