python簡單驗(yàn)證碼識別的實(shí)現(xiàn)過程
1. 環(huán)境準(zhǔn)備
1.1 安裝pillow 和 pytesseract
python模塊庫需要 pillow 和 pytesseract 這兩個庫,直接pip install 安裝就好了。
pip install pillow pip install pytesseract
1.2 安裝Tesseract-OCR.exe
下載地址:ocr下載地址
建議下載最新穩(wěn)定版本:
tesseract-ocr-w64-setup-v5.0.0.20190623.exe。
安裝過程很簡單,直接點(diǎn)擊下一步就完事了,其間可以默認(rèn)安裝路徑,也可以自定義安裝路徑,裝好之后,把它的安裝路徑添加到環(huán)境變量中即可,如我的這樣:
我的安裝位置:
環(huán)境變量就這樣加:
1.3 更改pytesseract.py的ocr路徑
我們pip install pytesseract 之后,在python解釋器安裝位置包里可以找到pytesseract.py文件如下:
打開之后,更改:
至此,環(huán)境準(zhǔn)備工作算是大功告成了。
2. 測試識別效果
ocr一直默認(rèn)安裝,起始就可以支持?jǐn)?shù)字和英文字母識別的,接下來
我們準(zhǔn)備一張驗(yàn)證碼圖片:
將圖片,命名為captcha.png,放到程序同一目錄下
import pytesseract from PIL import Image image = Image.open("captcha.png") print(pytesseract.image_to_string(image))
效果:
我們再嘗試一下中文識別。
在進(jìn)行識別之前我們要先下載好中文拓展語言包,
語言包地址
下載需要的的語言包,如下圖,紅框內(nèi)為中文簡體語言包:
下載后將該包直接放在ocr程序安裝目錄的tessdata文件夾里面即可。
找一張圖片測試一下:
import pytesseract from PIL import Image image = Image.open("00.jpg") print(pytesseract.image_to_string(image,lang='chi_sim'))
效果:
有時候文本識別率并不高,建議圖像識別前,先對圖像進(jìn)行灰度化和 二值化
代碼示例:
import pytesseract from PIL import Image file = r"00.jpg" # 先對圖像進(jìn)行灰度化和 二值化 image = Image.open(file) Img = image.convert('L') # 灰度化 #自定義灰度界限,這里可以大于這個值為黑色,小于這個值為白色。threshold可根據(jù)實(shí)際情況進(jìn)行調(diào)整(最大可為255)。 threshold = 180 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) photo = Img.point(table, '1') #圖片二值化 #保存處理好的圖片 photo.save('01.jpg') image = Image.open('01.jpg') # 解析圖片,lang='chi_sim'表示識別簡體中文,默認(rèn)為English # 如果是只識別數(shù)字,可再加上參數(shù)config='--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789' content = pytesseract.image_to_string(image, lang='chi_sim') print(content)
3. 實(shí)戰(zhàn)案例–實(shí)現(xiàn)古詩文網(wǎng)驗(yàn)證碼自動識別登錄
import pytesseract from PIL import Image from selenium import webdriver def save_captcha(path): driver = webdriver.Chrome() # 創(chuàng)建瀏覽器對象 driver.maximize_window() driver.implicitly_wait(10) driver.get(url=url) image = driver.find_element_by_id('imgCode') image.screenshot(path) return driver def recognize_captcha(captcha_path): captcha = Image.open(captcha_path) # 打開圖片 grap = captcha.convert('L') # 對圖片進(jìn)行灰度化處理 data = grap.load() # 將圖片對象加載成數(shù)據(jù) w, h = captcha.size # 獲取圖片的大小(寬度,高度) # 圖片二值化處理 for x in range(w): for y in range(h): if data[x, y] < 140: data[x, y] = 0 else: data[x, y] = 255 code = pytesseract.image_to_string(grap) # 對圖片進(jìn)行識別 return code def login(driver, code): flag = True email = '1242931802@qq.com' # 注冊的古詩文網(wǎng)賬號和密碼 password = 'xxxx' try: driver.find_element_by_id('email').send_keys(email) driver.find_element_by_id('pwd').send_keys(password) driver.find_element_by_id('code').send_keys(code) driver.implicitly_wait(10) driver.find_element_by_id('denglu').click() except Exception as ex: flag = False return flag if __name__ == '__main__': url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx' captcha_path = './captcha.png' count = 1 driver = save_captcha(captcha_path) # 獲取驅(qū)動 code = recognize_captcha(captcha_path) # 獲取驗(yàn)證碼 print('識別驗(yàn)證碼為:', code) if login(driver, code): driver.quit()
效果如下(有時候第一次可能識別失敗,可以寫個循環(huán)邏輯讓它多識別幾次,一般程序運(yùn)行1-3次基本會識別成功):
總結(jié)
到此這篇關(guān)于python實(shí)現(xiàn)簡單驗(yàn)證碼識別的文章就介紹到這了,更多相關(guān)python驗(yàn)證碼識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python采集某網(wǎng)站文檔并保存word格式的示例
這篇文章主要介紹了Python采集某網(wǎng)站文檔并保存word格式的示例,我們平常需要下載文檔的時候,是不是發(fā)現(xiàn),要么不能下載,要么不能復(fù)制,那么我們今天來分享一下,如何用Python將這些不給下載的文檔給批量下載下來,需要的朋友可以參考下2023-07-07python識別圖標(biāo)并點(diǎn)擊功能實(shí)現(xiàn)
這篇文章主要介紹了python識別圖標(biāo)并點(diǎn)擊功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07Python利用pandas和matplotlib實(shí)現(xiàn)繪制堆疊柱狀圖
在數(shù)據(jù)可視化中,堆疊柱狀圖是一種常用的圖表類型,它能夠清晰地展示多個類別的數(shù)據(jù),本文將演示如何使用 Python 的 pandas 和 matplotlib 庫繪制優(yōu)化的堆疊柱狀圖,需要的可以參考下2023-11-11django model通過字典更新數(shù)據(jù)實(shí)例
這篇文章主要介紹了django model通過字典更新數(shù)據(jù)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04