|
前言 相信大家利用 Python 寫的爬蟲應該遇到過要輸入驗證碼的尷尬局面,又或者寫了個自動填充表單的小程序,結(jié)果就卡在了驗證碼上。由于我也遇上過上述兩種情況,所以我在網(wǎng)上查閱資料后,打算將我查閱到的結(jié)果整理一下放在這里,順便做一個備份。 工具 Python pytesseract 庫 pytesseract 是對 Tesseract-OCR 的一個封裝,方便我們在 Python 中調(diào)用 Tesseract-OCR 引擎 Pypi Page 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. Github Page Python PIL(2.*)/Pillow(3.*) 庫 這兩個庫是 Python 關(guān)于圖像處理的第三方庫,其中 3.* 的版本要用 Pillow 庫 安裝 Tesseract-OCR 源碼編譯:可參照官方 Wiki windows:安裝包可以在 Sourceforge 上下載,不過只有 3.02 版本的安裝包 Linux:以 Ubuntu 為例,在終端輸入 sudo apt-get tesseract-ocr 即可進行安裝 Mac: MacPorts sudo port install tesseract Homebrew brew install tesseract 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 來搭建虛擬且獨立的python環(huán)境,可以使每個項目環(huán)境與其他項目獨立開來,保持環(huán)境的干凈,解決包沖突問題。 可以通過 pip 和 easy_install 進行安裝: easy_install virtualenv 或 pip install virtualenv 詳細可參照 使用virtualenv搭建獨立的Python環(huán)境 PIL, Pillow, pytesseract 這三個庫都可以通過 pip 直接安裝。 編程 首先,打開命令行或者終端,輸入以下命令: virtualenv venv --no-site-packages --python=X:\xxx\python.exe 各參數(shù)解釋: venv 虛擬環(huán)境所在位置 --no-site-packages 不復制主環(huán)境的庫 --python 指定虛擬環(huán)境的 python 版本 然后在命令行輸入以下命令,激活虛擬環(huán)境 Linux cd venv source ./bin/activate Windows cd venv .\Scripts\activate 如果要退出虛擬環(huán)境的話則輸入 deactivate 或 .\Scripts\deactivate 安裝依賴包 在當前虛擬環(huán)境中輸入 pip install PIL pip install Pillow pip install pytesseract 安裝完成后進入python,import一下看是否安裝成功。 圖片處理 step1. 打開圖片 Captcha.jpg from PIL import Image im = Image.open('Captcha.jpg') step2. 將彩色圖像轉(zhuǎn)化為灰度圖 im = im.convert('L') 轉(zhuǎn)化為灰度圖是為了減少圖片的色彩,處理起來更方便 step3. 降噪,圖片二值化 為了消除背景對文字的影響,可以通過設置一個閾值來將文字與背景分隔開來。而閾值可以參考圖片灰度的直方圖來得出,又或者試出來。 這里將閾值設置為 140,然后將大于閾值的像素置 1,小于閾值的置 0。 def initTable(threshold=140): table = [] for i in range(256): if i <> 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 設置的是 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é)果為 誤差修正
經(jīng)過測試發(fā)現(xiàn),Tesseract-OCR 對于純數(shù)字的驗證碼識別有一定誤差,因為該引擎識別的是英文文本,所以會將數(shù)字識別為字母。這時候就需要建立一個替換表,將識別錯誤的字母替換為數(shù)字,提高識別正確率。 |
|
|