驗證碼探究 如果你是一個數據挖掘愛好者,那麼驗證碼是你避免不過去的一個天坑,和各種驗證碼鬥爭,必然是你成長的一條道路,接下來的幾篇文章,我會儘量的找到各種驗證碼,並且去嘗試解決掉它,中間有些技術甚至我都沒有見過,來吧,一起Coding吧 數字+字母的驗證碼 我隨便在百度圖片搜索了一個驗證碼,如下 今 ...
驗證碼探究
如果你是一個數據挖掘愛好者,那麼驗證碼是你避免不過去的一個天坑,和各種驗證碼鬥爭,必然是你成長的一條道路,接下來的幾篇文章,我會儘量的找到各種驗證碼,並且去嘗試解決掉它,中間有些技術甚至我都沒有見過,來吧,一起Coding吧
數字+字母的驗證碼
我隨便在百度圖片搜索了一個驗證碼,如下
今天要做的是驗證碼識別中最簡單的一種辦法,採用pytesseract
解決,它屬於Python當中比較簡單的OCR識別庫
庫的安裝
使用pytesseract
之前,你需要通過pip 安裝一下對應的模塊 ,需要兩個
pytesseract庫還有圖像處理的pillow庫了
pip install pytesseract
pip install pillow
如果你安裝了這兩個庫之後,編寫一個識別代碼,一般情況下會報下麵這個錯誤
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
這是由於你還缺少一部分內容
安裝一個Tesseract-OCR軟體。這個軟體是由Google維護的開源的OCR軟體。
下載地址 > https://github.com/tesseract-ocr/tesseract/wiki
中文包的下載地址 > https://github.com/tesseract-ocr/tessdata
選擇你需要的版本進行下載即可
pillow庫的基本操作
命令 | 釋義 |
---|---|
open() | 打開一個圖片 from PIL import Image im = Image.open("1.png") im.show() |
save() | 保存文件 |
convert() | convert() 是圖像實例對象的一個方法,接受一個 mode 參數,用以指定一種色彩模式,mode 的取值可以是如下幾種: · 1 (1-bit pixels, black and white, stored with one pixel per byte) · L (8-bit pixels, black and white) · P (8-bit pixels, mapped to any other mode using a colour palette) · RGB (3x8-bit pixels, true colour) · RGBA (4x8-bit pixels, true colour with transparency mask) · CMYK (4x8-bit pixels, colour separation) · YCbCr (3x8-bit pixels, colour video format) · I (32-bit signed integer pixels) · F (32-bit floating point pixels) |
Filter
from PIL import Image, ImageFilter
im = Image.open(‘1.png’)
# 高斯模糊
im.filter(ImageFilter.GaussianBlur)
# 普通模糊
im.filter(ImageFilter.BLUR)
# 邊緣增強
im.filter(ImageFilter.EDGE_ENHANCE)
# 找到邊緣
im.filter(ImageFilter.FIND_EDGES)
# 浮雕
im.filter(ImageFilter.EMBOSS)
# 輪廓
im.filter(ImageFilter.CONTOUR)
# 銳化
im.filter(ImageFilter.SHARPEN)
# 平滑
im.filter(ImageFilter.SMOOTH)
# 細節
im.filter(ImageFilter.DETAIL)
Format
format屬性定義了圖像的格式,如果圖像不是從文件打開的,那麼該屬性值為None;
size屬性是一個tuple,表示圖像的寬和高(單位為像素);
mode屬性為表示圖像的模式,常用的模式為:L為灰度圖,RGB為真彩色,CMYK為pre-press圖像。如果文件不能打開,則拋出IOError異常。
這個地方可以參照一篇博客,寫的不錯 > https://www.cnblogs.com/mapu/p/8341108.html
驗證碼識別
註意安裝完畢,如果還是報錯,請找到模塊 pytesseract.py 這個文件,對這個文件進行編輯
一般這個文件在 C:\Program Files\Python36\Lib\site-packages\pytesseract\pytesseract.py
位置
文件中 tesseract_cmd = 'tesseract' 改為自己的地址
例如: tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
如果報下麵的BUG,請註意
Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable
解決辦法也比較容易,按照它的提示,表示缺失了 TESSDATA_PREFIX 這個環境變數。你只需要在系統環境變數中添加一條即可
將 TESSDATA_PREFIX=C:\Program Files (x86)\Tesseract-OCR 添加環境變數
重啟IDE或者重新CMD,然後繼續運行代碼,這個地方註意需要用管理員運行你的py腳本
步驟分為
- 打開圖片 Image.open()
- pytesseract識別圖片
import pytesseract
from PIL import Image
def main():
image = Image.open("1.jpg")
text = pytesseract.image_to_string(image,lang="chi_sim")
print(text)
if __name__ == '__main__':
main()
測試英文,數字什麼的基本沒有問題,中文簡直慘不忍睹。空白比較大的可以識別出來。唉~不好用
當然剛纔那個7364
十分輕鬆的就識別出來了。
帶干擾的驗證碼識別
接下來識別如下的驗證碼,我們首先依舊先嘗試一下。運行代碼發現沒有任何顯示。接下來需要對這個圖片進行處理
基本原理都是完全一樣的
- 彩色轉灰度
- 灰度轉二值
- 二值圖像識別
彩色轉灰度
im = im.convert('L')
灰度轉二值,解決方案比較成套路,採用閾值分割法,threshold為分割點
def initTable(threshold=140):
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table
調用
binaryImage = im.point(initTable(), '1')
binaryImage.show()
調整之後
我們還需要對干擾線進行處理。在往下研究去,是圖片深入處理的任務,對付小網站的簡單驗證碼,這個辦法足夠了,本篇博文OVER,下一篇我們繼續研究驗證碼。
參考鏈接
tesserocr GitHub:https://github.com/sirfz/tesserocr
tesserocr PyPI:https://pypi.python.org/pypi/tesserocr
pytesserocr GitHub:https://github.com/madmaze/pytesseract
pytesserocr PyPI:https://pypi.org/project/pytesseract/
tesseract下載地址:http://digi.bib.uni-mannheim.de/tesseract
tesseract GitHub:https://github.com/tesseract-ocr/tesseract
tesseract 語言包:https://github.com/tesseract-ocr/tessdata
tesseract文檔:https://github.com/tesseract-ocr/tesseract/wiki/Documentation
掃碼關註微信公眾賬號,領取2T學習資源