實作使用tesseract實作自動識别驗證碼,然後使用python搭建守護程序進行監聽。
一、首先要安裝Python
centos下預設的最新版本是2.6.6,為了更好的相容性,我這裡安裝python2.7.11.
1),先安裝GCC,用如下指令yum install gcc gcc-c++ openssl-devel 2)下載下傳Python-2.7.11.tar.xz檔案,wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz 3)下載下傳後放到/usr/local/src/下,進行解壓
tar -Jxvf Python-2.7.11.tar.xz
cd Python-2.7.11
mkdir /usr/local/python2.7
./configure --prefix=/usr/local/python2.7
make all
make install
清除之前編譯的可執行檔案及配置檔案。
make clean
清除所有生成的檔案 make distclean 由于系統預設給是2.6,是以先清除/usr/bin/python
rm -rf /usr/bin/python
建立軟連接配接
ln -s /usr/local/python2.7/bin/python2.7 /usr/bin/python
可以用python -V檢視,
安裝python擴充包
ln -s /usr/local/python2.7/bin/python2.7 /usr/bin/python
然後執行
python ez_setup.py 發現此方法安裝不上,出現問題,setup_tool安裝不上,去官網檢視,把ez_setup包下載下傳下來。
wget https://pypi.python.org/packages/ba/2c/743df41bd6b3298706dfe91b0c7ecdc47f2dc1a3104abeb6e9aa4a45fa5d/ez_setup-0.9.tar.gz
tar zxvf ez_setup-0.9.tar.gz
cd ez_setup-0.9
python ez_setup.py
雖然現在python已經安裝完成,但是使用yum指令會有問題——yum不能正常工作:
# yum list
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python
這是因為yum預設使用的python版本是2.6.6,到哪是現在的python版本是2.7.5,故會出現上述問題,隻需要該一下yum的預設python配置版本就行了:
#vi /usr/bin/yum
将檔案頭部的#!/usr/bin/python改為
#/usr/bin/python2.6
二、安裝tesseract
yum install -y tesseract
既可安裝,安裝的版本是3.04 安裝好後既可以使用其來使用進行圖檔的識别,
tesseract phototest.tif phototest -l eng
他會生成一個phototest的檔案,裡面就是所識别得内容
三、安裝PIL
1)安裝PIL所需的系統庫檔案,參考PIL的REDEME檔案,需要如下依賴庫 yum install zlib zlib-devel libjpeg libjpeg-devel freetype freetype-devel 2)删除Python下安裝的PIL cd /usr/local/python2.7/lib/python2.7/site-packages
3)下載下傳PIL軟體包 wget http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz tar -zxvf Imaging-1.1.7.tar.gz cd Imaging-1.1.7 python setup.py build_ext -i #用來進行安裝前的檢查
#修改setup.py,通過指令 vim setup.py 其中yum安裝的依賴庫需要用rpm -ql 包名檢視安裝位置。
TCL_ROOT = "/usr/lib64/"
JPEG_ROOT = "/usr/lib64/"
ZLIB_ROOT = "/usr/lib64/"
TIFF_ROOT = "/usr/lib64/"
FREETYPE_ROOT = "/usr/lib64/"
LCMS_ROOT = "/usr/lib64/"
安裝
$ python setup.py install
四、安裝pytesser
pytesser項目目前為止隻有0.0.1版本,并且代碼托管在谷歌,無法擷取,我是在百度網盤下載下傳, 下載下傳完成後,放到python安裝子產品下 /usr/local/python2.7/lib/python2.7/site-packages/下 在其目錄下,可以進行測試 寫一個test.py腳本 環境變量pythonpath 在/etc/profile.d/ vim pythonpath.sh 輸入: export PYTHONPATH=/usr/local/python2.7/lib/python2.7/site-packages/:/usr/local/python2.7/lib/python2.7/site-packages/pytesser_v0.0.1 儲存退出 source /etc/profile.d/pythonpath.sh 即可。
五、安裝web.py
官網:webpy.org 下載下傳web.py
wget http://webpy.org/static/web.py-0.37.tar.gz
tar zxvf web.py-0.37.tar.gz
cd /web.py-0.37
python setup.py install
或者使用
/usr/local/python2.7/bin/easy_install web.py
把寫好的Python腳本加入到系統啟動當中,并系統重新開機自動啟動,背景運作 在/etc/init.d/目錄下建立一個啟動腳本名字叫做chimgtoword 在其中加入如下内容:
#!/bin/bash
#add for chkconfig
#chkconfig: 2345 70 30
#description: the description of the shell
#processname: chimgtoword
#author:
#date:
執行 nohup /usr/bin/python /usr/local/bin/changeImgToWords.py 2>>/dev/null & 儲存退出。 增加可執行權限, [[email protected] ~]# chmod a+x /etc/init.d/chimgtoword [[email protected] ~]# chkconfig --add chimgtoword [[email protected] ~]# chkconfig --list|grep chimgtoword chimgtoword 0:off 1:off 2:on 3:on 4:on 5:on 6:off [[email protected] ~]# chkconfig chimgtoword on 啟動服務: [[email protected] ~]# /etc/init.d/chimgtoword 檢視服務是否啟動
檢視端口是否打開: