99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python驗證碼識別處理實例
Python驗證碼識別處理實例
2018-04-01
收藏

Python驗證碼識別處理實例

一、準(zhǔn)備工作與代碼實例
(1)安裝PIL:下載后是一個exe,直接雙擊安裝,它會自動安裝到C:\Python27\Lib\site-packages中去,
(2)pytesser:下載解壓后直接放C:\Python27\Lib\site-packages(根據(jù)你安裝的Python路徑而不同),同時,新建一個pytheeer.pth,內(nèi)容就寫pytesser,注意這里的內(nèi)容一定要和pytesser這個文件夾同名,意思就是pytesser文件夾,pytesser.pth,及內(nèi)容都要一樣!
(3)Tesseract OCR engine下載:下載后解壓,tessdata文件夾,用其替換掉pytesser解壓后的tessdata文件夾即可。

二、驗證
(1)原理:
驗證碼圖像處理

驗證碼圖像識別技術(shù)主要是操作圖片內(nèi)的像素點,通過對圖片的像素點進行一系列的操作,最后輸出驗證碼圖像內(nèi)的每個字符的文本矩陣。
    1、讀取圖片
    2、圖片降噪
    3、圖片切割
    4、圖像文本輸出
(2)驗證字符識別
驗證碼內(nèi)的字符識別主要以機器學(xué)習(xí)的分類算法來完成,目前我所利用的字符識別的算法為KNN(K鄰近算法)和SVM (支持向量機算法),后面我 會對這兩個算法的適用場景進行詳細描述。
    1、獲取字符矩陣
    2、矩陣進入分類算法
    3、輸出結(jié)果
要驗證的圖片如下:

(3)、簡單的命令:

from pytesser import *
image = Image.open('1.jpg') # Open image object using PIL
print image_to_string(image)  # Run tesseract.exe on image

然后運行:

或者直接:

print image_file_to_string('fnord.tif')

同樣能輸出結(jié)果!
(4)、復(fù)雜一點的
上面的只能對一些比較簡單的做處理,一
原理:彩色轉(zhuǎn)灰度,灰度轉(zhuǎn)二值,二值圖像識別

# 驗證碼識別,此程序只能識別數(shù)據(jù)驗證碼
import Image
import ImageEnhance
import ImageFilter
import sys
from pytesser import *
# 二值化
threshold = 140
table = []
for i in range(256):
 if i < threshold:
  table.append(0)
 else:
  table.append(1)
 
#由于都是數(shù)字
#對于識別成字母的 采用該表進行修正
rep={'O':'0',
 'I':'1','L':'1',
 'Z':'2',
 'S':'8'
 };
 
def getverify1(name):   
 #打開圖片
 im = Image.open(name)
 #轉(zhuǎn)化到灰度圖
 imgry = im.convert('L')
 #保存圖像
 imgry.save('g'+name)
 #二值化,采用閾值分割法,threshold為分割點
 out = imgry.point(table,'1')
 out.save('b'+name)
 #識別
 text = image_to_string(out)
 #識別對嗎
 text = text.strip()
 text = text.upper();  
 for r in rep:
  text = text.replace(r,rep[r])  
 #out.save(text+'.jpg')
 print text
 return text
getverify1('1.jpg') #注意這里的圖片要和此文件在同一個目錄,要不就傳絕對路徑也行

運行后效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }