???最近研究了一下圖像識別,一直找到很好的應用場景,今天我就發現可以用百度的旋轉驗證碼來做一個實驗。沒想到效果還挺好,下面就是實際的識別效果。
1、效果演示
2、如何識別
2.1準備數據集
首先需要使用爬蟲,對驗證碼圖片進行采集,盡量每一種類型都要采集到。
2.2圖像矯正
接下來對采集的數據進行人工校正
2.3數據清洗
(1)對數據進行進行旋轉,達到增加數據量的目的。
(2)對數據進行灰度化處理,將三維圖片降為二維。
(3)對圖片大小進行resize,可以提高訓練速度。
# 圖片轉換部分,得到x
picture = Picture(path=img_path)
# 圖像灰度化處理
temp_img = picture.gray()
# 圖像resize
temp_img = temp_img.resize((50, 50), Image.LANCZOS)
# 獲取y
word = img_path.split('\\')[-1].split('-')[0]# 結果包裝成列表,保證x,y是一個整體,不被打亂
res = [np.array(temp_img),np.array(word)]
# 將結構給全局變量
result_list.append(res)
# 記錄完成數量
complete_list.append(img_path)
2.4劃分訓練集與測試集
一般訓練集占數據量的80%,測試集占總數據量的20%,當然也可以根據自己的情況調整比例。
2.5訓練模型
這里可以使用CNN神經網絡模型進行訓練,效果非常不錯。
2.6實戰測試
下面直接上代碼。其中的滑動系數可能需要自行調整,這個變動不會太頻繁,可能幾個月某度變一次。
__author__ = "dengxinyan"import os
import sys
import time
import base64
import random
import requests
from PIL import Image
from io import BytesIO
sys.path.append(os.path.abspath(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))))
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains#PIL圖片保存為base64編碼
def PIL_base64(img, coding='utf-8'):img_format = img.formatif img_format == None:img_format = 'JPEG'format_str = 'JPEG'if 'png' == img_format.lower():format_str = 'PNG'if 'gif' == img_format.lower():format_str = 'gif'if img.mode == "P":img = img.convert('RGB')if img.mode == "RGBA":format_str = 'PNG'img_format = 'PNG'output_buffer = BytesIO()# img.save(output_buffer, format=format_str)img.save(output_buffer, quality=100, format=format_str)byte_data = output_buffer.getvalue()base64_str = 'data:image/' + img_format.lower() + ';base64,' + base64.b64encode(byte_data).decode(coding)return base64_str# 根據鏈接下載旋轉圖片
def get_img(url):header = {"Host": "passport.baidu.com","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0","Accept": "image/webp,*/*","Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2","Accept-Encoding": "gzip, deflate, br","Connection": "keep-alive","Referer": "https://wappass.baidu.com/static/captcha/tuxing.html?&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fs%3Fcl%3D3%26tn%3Dbaidutop10%26fr%3Dtop1000%26wd%3D%25E6%25B6%2588%25E9%2598%25B2%25E6%2588%2598%25E5%25A3%25AB%25E8%25BF%259E%25E5%25A4%259C%25E7%25AD%2591%25E5%259D%259D%25E5%25BA%2594%25E5%25AF%25B9%25E6%25B4%25AA%25E5%25B3%25B0%25E8%25BF%2587%25E5%25A2%2583%26rsv_idx%3D2%26rsv_dl%3Dfyb_n_homepage%26hisfilter%3D1&logid=8309940529500911554&signature=4bce59041938b160b7c24423bde0b518×tamp=1624535702","Cookie": "BAIDUID=A0621DC238F4D936B38F699B70A7E41F:SL=0:NR=10:FG=1; BIDUPSID=A0621DC238F4D9360CD42C9C31352635; PSTM=1667351865; HOSUPPORT=1; UBI=fi_PncwhpxZ%7ETaKAanh2ue0vFk6vHMY02DgvigILJIFul8Z1nzMr9do3SYLtjAUqHSpUz7LvOKV27cIr18-YJryP0Q8j92oo93%7E6hGa0CLdraAlaHUZG-0PW9QrpZkW7MTyUn-yrAq7OmSRBIJ7%7E8gM9pv-; USERNAMETYPE=2; SAVEUSERID=3cd458184c56c2fe28174e594101f074d63463446d; HISTORY=0ece87e30ec8ecccd52ff3d5c42f98002a893bfb73ff358893; BDUSS_BFESS=NOcWd6YWJRbmFVUVBBaWVkaHJNSm5tRUpUaUVMaTNHOHcwZVVaVDdsYXlLZmxrSVFBQUFBJCQAAAAAAAAAAAEAAAC13Mct0KHQwl9keHkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALKc0WSynNFkRD; H_WISE_SIDS=219946_216846_213346_219942_213039_230178_204909_230288_110085_236307_243888_244730_245412_243706_232281_249910_247148_250889_249892_252577_234296_253427_253705_240590_254471_179345_254689_254884_254864_253213_255713_254765_255939_255959_255982_107317_256062_256093_256083_255803_253993_256257_255661_256025_256223_256439_256446_254831_253151_256252_256196_256726_256739_251973_256230_256611_256996_257068_257079_257047_254075_257110_257208_251196_254144_257290_251068_256095_257287_254317_251059_251133_254299_257454_257302_255317_255907_255324_257481_244258_257582_257542_257503_255177_257745_257786_257937_257167_257904_197096_257586_257402_255231_257790_258193_258248_258165_8000084_8000115_8000114_8000126_8000140_8000149_8000166_8000172_8000178_8000181_8000185_8000204; ZFY=SxMcCdU3pSsmienZSgA2BTmHLR9S6caVmiP5Ic:Awuz0:C; BAIDUID_BFESS=A0621DC238F4D936B38F699B70A7E41F:SL=0:NR=10:FG=1; Hm_lvt_90056b3f84f90da57dc0f40150f005d5=1690961642,1692328306; STOKEN=01dbff3d6ff696219b39c9fb730c31c34e032c0eebff4fe535d2f1dde0c7b45b; BDUSS=NOcWd6YWJRbmFVUVBBaWVkaHJNSm5tRUpUaUVMaTNHOHcwZVVaVDdsYXlLZmxrSVFBQUFBJCQAAAAAAAAAAAEAAAC13Mct0KHQwl9keHkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALKc0WSynNFkRD; PTOKEN=92e828db8120372a7baa2557ea4ec476; MAWEBCUID=web_VYfxPuQDaKjEzVgXMFgoHouACkpXyjcDpcWwhATKqELuuwEtNy; __bid_n=18a4ab547aa11525d249ea",}response = requests.get(url=url,headers=header)if response.status_code == 200:img = Image.open(BytesIO(response.content))# 將圖片轉換成base64字符串并返回return PIL_base64(img)# 驗證碼識別接口
def shibie(img_base64):url = "http://www.detayun.cn/openapi/verify_code_identify/"data = {# 用戶的key"key":"JxSfP4E1yfcmJZB6ynOB",# 驗證碼類型"verify_idf_id":"16",# 樣例圖片"img_base64":img_base64,"img_byte": None,# 中文點選,空間語義類型驗證碼的文本描述(這里缺省為空字符串)"words":""}header = {"Content-Type": "application/json"}# 發送請求調用接口response = requests.post(url=url, json=data, headers=header)print(response.text)return int(str(response.json()['data']['res_str']).replace('順時針旋轉','').replace('度',''))if __name__ == '__main__':# 加載防檢測jswith open('.\webdriver\stealth.min.js') as f:js = f.read()options = webdriver.ChromeOptions()driver = webdriver.Chrome(executable_path='.\webdriver\chromedriver.exe', options=options)driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": js})# 訪問百度首頁driver.get('https://wappass.baidu.com/static/captcha/tuxing.html?&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fs%3Fcl%3D3%26tn%3Dbaidutop10%26fr%3Dtop1000%26wd%3D%25E6%25B6%2588%25E9%2598%25B2%25E6%2588%2598%25E5%25A3%25AB%25E8%25BF%259E%25E5%25A4%259C%25E7%25AD%2591%25E5%259D%259D%25E5%25BA%2594%25E5%25AF%25B9%25E6%25B4%25AA%25E5%25B3%25B0%25E8%25BF%2587%25E5%25A2%2583%26rsv_idx%3D2%26rsv_dl%3Dfyb_n_homepage%26hisfilter%3D1&logid=8309940529500911554&signature=4bce59041938b160b7c24423bde0b518×tamp=1624535702')# 等待滑塊出現WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//div[contains(@class,"passMod_slide-btn")]'))yzm_button = driver.find_element_by_xpath('//div[contains(@class,"passMod_slide-btn")]')time.sleep(1)move_x = 100# 等待驗證碼出現WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//img[contains(@class,"passMod_spin-background")]'))img_src = driver.find_element_by_xpath('//img[contains(@class,"passMod_spin-background")]').get_attribute('src')# 下載圖片并轉化為base64img_base64 = get_img(img_src)# 識別圖片旋轉角度move_x = shibie(img_base64)# 通過旋轉角度 * 滑動系數 = 滑動距離move_x = move_x * 0.661# 開始滑動action = ActionChains(driver)action.click_and_hold(yzm_button).perform() # 鼠標左鍵按下不放action.move_by_offset(move_x, 0).perform()action.release().perform() # 釋放鼠標time.sleep(2)# 第二次滑動# 等待滑塊出現WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//div[contains(@class,"passMod_slide-btn")]'))yzm_button = driver.find_element_by_xpath('//div[contains(@class,"passMod_slide-btn")]')time.sleep(1)move_x = 100# 等待驗證碼出現WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//img[contains(@class,"passMod_spin-background")]'))img_src = driver.find_element_by_xpath('//img[contains(@class,"passMod_spin-background")]').get_attribute('src')# 下載圖片并轉化為base64img_base64 = get_img(img_src)# 識別圖片旋轉角度move_x = shibie(img_base64)# 通過旋轉角度 * 滑動系數 = 滑動距離move_x = move_x * 0.661# 開始滑動action = ActionChains(driver)action.click_and_hold(yzm_button).perform() # 鼠標左鍵按下不放action.move_by_offset(move_x, 0).perform()action.release().perform() # 釋放鼠標
3、總結
這個旋轉驗證碼非常有特色,而且有很大的難度。特別是在標記訓練圖片的時候,非常耗費時間。
現在我也把識別模型封裝成了接口,感興趣的小伙伴可以免費使用:得塔云