图像验证码识别(两种方式)

准备库:

PIL   pytesseract
PIL:用于处理验证码图片
pytesseract:用于识别图片文字

准备工具:

Tesseract Ocr

下载地址

http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.00dev.exe

 

简单来说,三个步骤:

  • 下载验证码图片。
  • 加载(处理)验证码图片。
  • 识别图片。

 

下载验证码图片

这里就拿知网的验证码吧。

如图:

http://my.cnki.net/elibregister/#

找到对应的验证码链接即可下载。

url = 'http://my.cnki.net/elibregister/CheckCode.aspx'    
response = requests.get(url)
with open('CheckCode.png','wb') as fw:
    fw.write(response.content)

 

加载(处理)验证码图片

使用PIL.Image模块进行图片处理。

from PIL import Image
    
img = Image.open('CheckCode.png')

img = img.convert('L')  # 进行灰度处理
                    
# 二值化处理
threshold = 128     # 二值化阈值
t_list = []
for i in range(256):
    if i < threshold:
        t_list.append(0)
    else:
        t_list.append(1)
img = img.point(t_list, '1')

 

识别图片

图片的识别,用到pytesseract模块,只需一行代码即可处理。

import pytesseract
result = pytesseract.image_to_string(img)

完整代码:

# -*- coding: utf-8 -*-

# @Time    : 2019/8/14 14:58
# @Author  : hccfm
# @File    : 图像验证码.py
# @Software: PyCharm

"""
字符图像验证码处理
:使用的tesseract 验证去识别验证码,当图像干扰比较大时,处理时出错大,可进行图像处理。
"""
import requests
from PIL import Image
import pytesseract

url = 'http://my.cnki.net/elibregister/CheckCode.aspx'
# url = 'http://www.yundama.com//index/captcha'

def main():
    response = requests.get(url)
    with open('CheckCode.png','wb') as fw:
        fw.write(response.content)

    img = Image.open('CheckCode.png')
    result = pytesseract.image_to_string(img)
    print("未进行处理,出错机率很大:",result)

    img = img.convert('L')  # 进行灰度处理

    threshold = 128     # 二值化阈值
    t_list = []
    for i in range(256):
        if i < threshold:
            t_list.append(0)
        else:
            t_list.append(1)
    img = img.point(t_list, '1')

    img.show()
    result = pytesseract.image_to_string(img)
    print("处理后:",result)

if __name__ == '__main__':
    main()

处理结果:

 

说明:

tesseract其实在处理复杂验证码时,验证效果不是很好。经常会出错。这时我们可以进行tesseract文字训练。这里就不说了,涉及太多了。参考:https://www.cnblogs.com/wj-1314/p/9454656.html

 

现在说说另一种,使用云打码平台。收费(比较便宜)

http://www.yundama.com/

另外也可以用超级鹰平台:https://www.chaojiying.com/

这里使用云打码平台,因为钱没用完。

价格:

 

使用方式:

下载好对应的开发文档。

http://www.yundama.com/apidoc/YDM_SDK.html#demo

我使用的是python 就下python 的demo

修改YDMHTTPDemo3.x.py 文件中对应的参数即可,(2.x表示python2,3.x表示python3)

 

试试效果

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章