7.图形验证码识别技术

图形验证码识别技术:

阻碍我们爬虫的。有时候正是在登录或者请求一些数据时候的图形验证码。因此这里我们讲解一种能将图片翻译成文字的技术。将图片翻译成文字一般被成为光学文字识别(Optical Character Recognition),简写为OCR。实现OCR的库不是很多,特别是开源的。因为这块存在一定的技术壁垒(需要大量的数据、算法、机器学习、深度学习知识等),并且如果做好了具有很高的商业价值。因此开源的比较少。这里介绍一个比较优秀的图像识别开源库:Tesseract。

Tesseract:

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

安装:

Windows系统:

在以下链接下载可执行文件,然后一顿点击下一步安装即可(放在不需要权限的纯英文路径下):
https://github.com/tesseract-ocr/

Linux系统:

可以在以下链接下载源码自行编译。
https://github.com/tesseract-ocr/tesseract/wiki/Compiling
或者在ubuntu下通过以下命令进行安装:

sudo apt install tesseract-ocr

Mac系统:

Homebrew即可方便安装:

brew install tesseract

设置环境变量:

安装完成后,如果想要在命令行中使用Tesseract,那么应该设置环境变量。MacLinux在安装的时候就默认已经设置好了。在Windows下把tesseract.exe所在的路径添加到PATH环境变量中。

还有一个环境变量需要设置的是,要把训练的数据文件路径也放到环境变量中。
在环境变量中,添加一个TESSDATA_PREFIX=C:\path_to_tesseractdata\teseractdata

在命令行中使用tesseract识别图像:

如果想要在cmd下能够使用tesseract命令,那么需要把tesseract.exe所在的目录放到PATH环境变量中。然后使用命令:tesseract 图片路径 文件路径
示例:

tesseract a.png a

那么就会识别出a.png中的图片,并且把文字写入到a.txt中。如果不想写入文件直接想显示在终端,那么不要加文件名就可以了。

在代码中使用tesseract识别图像:

Python代码中操作tesseract。需要安装一个库,叫做pytesseract。通过pip的方式即可安装:

pip install pytesseract

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

pip install PIL

使用pytesseract将图片上的文字转换为文本文字的示例代码如下:

# 导入pytesseract库
import pytesseract
# 导入Image库
from PIL import Image

# 指定tesseract.exe所在的路径
pytesseract.pytesseract.tesseract_cmd = r'D:\ProgramApp\TesseractOCR\tesseract.exe'

# 打开图片
image = Image.open("a.png")
# 调用image_to_string将图片转换为文字
text = pytesseract.image_to_string(image)
print(text)

pytesseract处理拉勾网图形验证码:

import pytesseract
from urllib import request
from PIL import Image
import time


pytesseract.pytesseract.tesseract_cmd = r"D:\ProgramApp\TesseractOCR\tesseract.exe"


while True:
    captchaUrl = "https://passport.lagou.com/vcode/create?from=register&refresh=1513081451891"
    request.urlretrieve(captchaUrl,'captcha.png')
    image = Image.open('captcha.png')
    text = pytesseract.image_to_string(image,lang='eng')
    print(text)
    time.sleep(2)
 
posted @ 2018-08-15 16:52  等待の喵  阅读(1136)  评论(0编辑  收藏  举报