圖像驗證碼識別(兩種方式)

準備庫:

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)

 

試試效果

 

 

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