我自己的理解
有可能繞過認(rèn)證碼盡量選擇繞過去,請(qǐng)多嘗試乘综,繞過去爬取效率高不少憎账;
先不考慮那些一天一套認(rèn)證碼的網(wǎng)站,百度“Python 認(rèn)證碼識(shí)別”卡辰,搜索得到的結(jié)果一般都是tersserocr胞皱、pytesser這幾個(gè)邪意;簡(jiǎn)單的說就是在你電腦上安裝tesseract-ocr;然后使用Python中的subprocess.Popen執(zhí)行對(duì)應(yīng)的語(yǔ)句反砌,捕捉終端雾鬼、文件中顯示的結(jié)果;所以不局限于語(yǔ)言宴树;
強(qiáng)烈建議用cnn等策菜,比較明確算法,準(zhǔn)確率也高酒贬;現(xiàn)在的tf要不錯(cuò)比較好用又憨;teaseract識(shí)別規(guī)范的還好,cnn看有時(shí)間再寫吧锭吨;
簡(jiǎn)要步驟:
1蠢莺、安裝tesseract-ocr:
2、安裝pytesser耐齐;
3浪秘、訓(xùn)練tesseract蒋情;
4埠况、使用
詳細(xì)的步驟:
##1、安裝tesseract-ocr:
window
直接網(wǎng)上搜索就有了棵癣,exe很方便辕翰,也可以使用包;
Linux
我的虛擬機(jī)是archlinux狈谊,pacman -S tesseract-ocr喜命;
如果是ubuntu,請(qǐng)安裝下面的一堆東西(這個(gè)我沒有認(rèn)證河劝。壁榕。。):
sudo apt-get install autoconf automake libtool
sudo apt-get install libpng12-dev
sudo apt-get install libjpeg62-dev
sudo apt-get install libtiff4-dev
sudo apt-get install zlib1g-dev
sudo apt-get install libleptonica # install leptonica
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install automake
tar zxvf tesseract-3.00.tar.gz
cd tesseract-3.00 && ./configure && make && sudo make install
總的來說赎瞎,缺什么補(bǔ)什么
##2牌里、安裝pytesser;
pytersser的做法是务甥,你輸入圖片之后牡辽,使用tersseract識(shí)別得到一個(gè)文件,這個(gè)文件名被固定了敞临,再去讀取文件得到識(shí)別結(jié)果态辛;所以對(duì)于多線程需自己動(dòng)手修改下;Python要使用pytesser得安依賴包PIL挺尿;
優(yōu)先使用Python的pip安裝奏黑,若出現(xiàn)問題則挽起袖子手動(dòng)安裝(這折騰也是無奈炊邦,只是自己手賤不知道動(dòng)了py哪里,安裝的時(shí)候出現(xiàn)ssl問題攀涵。铣耘。。)以故;
window:
如果你的機(jī)器是32位蜗细,恭喜,網(wǎng)上一堆32位系統(tǒng)的安裝包怒详;
64位系統(tǒng)的請(qǐng)到這里下載炉媒,都是編譯好的:http://www.lfd.uci.edu/~gohlke/pythonlibs/
Linux:
默認(rèn)安裝路勁:/usr/local/lib 安裝libjpeg:
$ tar zxvf jpegsrc.v7.tar.gz
$ cd jpeg-7
$ ./configure --enable-shared --enable-static
$ make
$ sudo make install
安裝zlib:
$ tar zxvf zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ sudo make install
安裝freetype
$ tar zxf freetype-2.6.1.tar.gz
$ cd freetype-2.6.1
$ ./configure
$ make
$ sudo make install
安裝PIL unzip Imaging-1.1.7.zip cd Imaging-1.1.7 修改setup.py文件,修改配置文件路徑 JPEG_ROOT = "/usr/local/include" ZLIB_ROOT = "/usr/local/include" FREETYPE_ROOT = "/usr/local/include"
bash-3.2$ python setup.py build_ext -i
c)測(cè)試編譯: python selftest.py d)安裝: python setup.py install
安裝pytersser 方法很多:pip昆烁,github下載吊骤;
常見的錯(cuò)誤提示:
AttributeError: 'NoneType' object has no attribute 'bands'
修改nano /usr/lib/python2.7/site-packages/PIL/Image.py
1496 行添加:self.load()
def split(self):
? ? "Split image into bands"
? ? ?self.load()
? ? ?if self.im.bands == 1:
? ? ? ? ? ims = [self.copy()]
? ? else:
? ? ? ? ?ims = []
? ? ?self.load()
? ? ?for i in range(self.im.bands):
? ? ? ? ? ?ims.append(self._new(self.im.getband(i)))
? ? return tuple(ims)
OSError: [Errno 2] No such file or directory
原因可能很多:沒有安裝 tesseract-ocr;Python引用問題静尼;圖片不存在等白粉;
使用Image_to_string時(shí)出現(xiàn)的問題:
Traceback (most recent call last):
File "C:\Users\TF-2016\Desktop\spider\ruijie\ruijie.py", line 33, in
print image_file_to_string('11.png', graceful_errors=True)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 48, in image_file_to_string
call_tesseract(filename, scratch_text_name_root)
File "C:\Python27\lib\site-packages\pytesser\pytesser.py", line 23, in call_tesseract
proc = subprocess.Popen(args)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2]
將需要運(yùn)行的文件,直接放在pytesser的包下 或者
>>> import os
>>> os.chdir('C:\Python27\Lib\site-packages\pytesser')
3鼠渺、訓(xùn)練tesseract:
《Python網(wǎng)絡(luò)數(shù)據(jù)采集》中也有介紹鸭巴;這里是使用jTessBoxEditor完成的,一個(gè)現(xiàn)成的工具拦盹;
如果采用默認(rèn)的eng來識(shí)別認(rèn)證碼鹃祖,基本不靠譜,各種雜數(shù)據(jù)普舆;自己訓(xùn)練下數(shù)據(jù)好很多恬口;
安裝Tesseract-OCR之后就訓(xùn)練自己的識(shí)別庫(kù):
1、創(chuàng)建diff:
jTessBoxEditor--tool---merge tiff--shift select more image
diff 文件命名格式:[lang].[fontname].exp[num].tif
exp:num.font.exp0.tif
2沼侣、創(chuàng)建好tiff之后創(chuàng)建.box
tesseract.exe num.font.exp0.tif num.font.exp0 batch.nochop makebox
3祖能、文字矯正:
jTessBoxEditor--open--num.font.exp0.tif
簡(jiǎn)單描述一下: 打開tif之后默認(rèn)已經(jīng)進(jìn)行了一次識(shí)別,需要做的是調(diào)整識(shí)別結(jié)果蛾洛;可以調(diào)節(jié)識(shí)別框位置大小养铸、識(shí)別結(jié)果;調(diào)整完畢后記得回車或者點(diǎn)擊一下設(shè)置按鈕雅潭;
4揭厚、定義字體特征文件:
創(chuàng)建font_properties文件,文件內(nèi)容為:font 0 0 0 0 0
exp:font 0 0 0 0 0扶供;表示字體不是粗體筛圆、斜體
5、生成traineddata文件
樣本圖片路勁下執(zhí)行這些:也可以創(chuàng)建bat一次性執(zhí)行完
tesseract.exe num.font.exp0.tif num.font.exp0 nobatch box.train
unicharset_extractor.exe num.font.exp0.box
mftraining -F font_properties -U unicharset -O num.unicharset num.font.exp0.tr
cntraining.exe num.font.exp0.tr
rename normproto num.normproto
rename inttemp num.inttemp
rename pffmtable num.pffmtable
rename shapetable num.shapetable
combine_tessdata.exe num.
確認(rèn)打印結(jié)果中的Offset 1椿浓、3太援、4闽晦、5、13這些項(xiàng)不是-1就行了提岔。
6仙蛉、拷貝
最后將traineddata拷貝到:Tesseract-OCR--tessdata
7、測(cè)試
tesseract.exe pin.png result-eng -l eng
tesseract.exe pin.png result-num -l num
4碱蒙、使用
import pytesser
from PIL import Image
im = Image.open('./pin.png')
im.show()
print pytesser.image_to_string(im)
目前識(shí)別率還是不理想荠瘪,但已經(jīng)可以用來跑數(shù)據(jù)了~~(折騰的是微博(weibo.com)的認(rèn)證碼;如果是weibo.cn的認(rèn)證碼赛惩,我表示我自己輸入十幾次沒一次通過哀墓,囧),考慮試試卷積~~