前言
相信大家利用 Python 寫的爬蟲應(yīng)該遇到過要輸入驗(yàn)證碼的尷尬局面斩个,又或者寫了個自動填充表單的小程序胯杭,結(jié)果就卡在了驗(yàn)證碼上。由于我也遇上過上述兩種情況受啥,所以我在網(wǎng)上查閱資料后做个,打算將我查閱到的結(jié)果整理一下放在這里鸽心,順便做一個備份。
工具
- Python pytesseract 庫
pytesseract 是對 Tesseract-OCR 的一個封裝居暖,方便我們在 Python 中調(diào)用 Tesseract-OCR 引擎
- Tesseract-OCR 開源識別引擎
Tesseract was originally developed at Hewlett-Packard Laboratories Bristol and at Hewlett-Packard Co, Greeley Colorado between 1985 and 1994, with some more changes made in 1996 to port to Windows, and some C++izing in 1998.
In 2005 Tesseract was open sourced by HP. Since 2006 it is developed by Google.
- Python PIL(2.*)/Pillow(3.*) 庫
這兩個庫是 Python 關(guān)于圖像處理的第三方庫顽频,其中 3.* 的版本要用 Pillow 庫
安裝
-
Tesseract-OCR
- 源碼編譯:可參照官方Wiki
- windows:安裝包可以在 Sourceforge 上下載,不過只有 3.02 版本的安裝包
- Linux:以 Ubuntu 為例太闺,在終端輸入
sudo apt-get tesseract-ocr
即可進(jìn)行安裝 - Mac:
- MacPorts
sudo port install tesseract
- Homebrew
brew install tesseract
- MacPorts
** P.S.**
在windows上安裝時糯景,在 Target appended to the Path 這一步耗時較久,請耐心等候省骂。
安裝完成后莺奸,在命令行界面輸入 tesseract 會出現(xiàn)以下提示:
Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...] pagesegmode values are: 0 = Orientation and script detection (OSD) only. 1 = Automatic page segmentation with OSD. 2 = Automatic page segmentation, but no OSD, or OCR 3 = Fully automatic page segmentation, but no OSD. (Default) 4 = Assume a single column of text of variable sizes. 5 = Assume a single uniform block of vertically aligned text. 6 = Assume a single uniform block of text. 7 = Treat the image as a single text line. 8 = Treat the image as a single word. 9 = Treat the image as a single word in a circle. 10 = Treat the image as a single character. -l lang and/or -psm pagesegmode must occur before anyconfigfile. Single options: -v --version: version info --list-langs: list available languages for tesseract engine
則說明引擎安裝成功。
-
virtualenv
為了將 Python 主環(huán)境隔離開來冀宴,不影響第三方庫之間的兼容性灭贷,我們可以利用 virtualenv 來搭建虛擬且獨(dú)立的python環(huán)境,可以使每個項目環(huán)境與其他項目獨(dú)立開來略贮,保持環(huán)境的干凈甚疟,解決包沖突問題。可以通過 pip 和 easy_install 進(jìn)行安裝:
easy_install virtualenv 或 pip install virtualenv
詳細(xì)可參照使用virtualenv搭建獨(dú)立的Python環(huán)境
PIL, Pillow, pytesseract
這三個庫都可以通過 pip 直接安裝逃延。
編程
- 首先政冻,打開命令行或者終端万哪,輸入以下命令:
virtualenv venv --no-site-packages --python=X:\xxx\python.exe
各參數(shù)解釋:
- venv 虛擬環(huán)境所在位置
- --no-site-packages 不復(fù)制主環(huán)境的庫
- --python 指定虛擬環(huán)境的 python 版本
然后在命令行輸入以下命令,激活虛擬環(huán)境
- Linux
cd venv
source ./bin/activate
- Windows
cd venv
.\Scripts\activate
如果要退出虛擬環(huán)境的話則輸入
deactivate
或
.\Scripts\deactivate
- 安裝依賴包
在當(dāng)前虛擬環(huán)境中輸入
pip install PIL
pip install Pillow
pip install pytesseract
安裝完成后進(jìn)入python,import一下看是否安裝成功拉队。
- 圖片處理
-
step1. 打開圖片
Captcha.jpgfrom PIL import Image im = Image.open('Captcha.jpg')
-
step2. 將彩色圖像轉(zhuǎn)化為灰度圖
im = im.convert('L')
轉(zhuǎn)化為灰度圖是為了減少圖片的色彩备闲,處理起來更方便
-
step3. 降噪蝇棉,圖片二值化
為了消除背景對文字的影響喜最,可以通過設(shè)置一個閾值來將文字與背景分隔開來。而閾值可以參考圖片灰度的直方圖來得出料按,又或者試出來奄侠。
這里將閾值設(shè)置為 140,然后將大于閾值的像素置 1载矿,小于閾值的置 0垄潮。
def initTable(threshold=140): table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) return table
再使用 im.point() 可以將灰度圖二值化,結(jié)果如下:
binaryImage = im.point(initTable(), '1') binaryImage.show()
Captcha1.jpg
- 識別文本
可以通過 pytesseract 的 image_to_string() 函數(shù)將圖片轉(zhuǎn)化為文本闷盔,該函數(shù)還可以接受參數(shù) config弯洗,config 設(shè)置的是 Tesseract-OCR 引擎的參數(shù),可自行查閱引擎的幫助文本逢勾。不過我們只需要用到 psm 參數(shù)牡整,具體的 psm 參數(shù)值如下:
-psm N
Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR.
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
識別圖片的代碼如下:
print(image_to_string(binaryImage, config='-psm 7')
識別結(jié)果為
7226
- 誤差修正
經(jīng)過測試發(fā)現(xiàn),Tesseract-OCR 對于純數(shù)字的驗(yàn)證碼識別有一定誤差敏沉,因?yàn)樵撘孀R別的是英文文本果正,所以會將數(shù)字識別為字母。這時候就需要建立一個替換表盟迟,將識別錯誤的字母替換為數(shù)字秋泳,提高識別正確率。