Python小紅書旋轉驗證碼識別實戰(zhàn)教程
本周免費接了一個用戶的需求,研究了一下小紅書旋轉驗證碼。剛開始小瞧了它,覺得它應該沒有百度旋轉驗證碼那么難,畢竟圖像沒有干擾,需要的訓練樣本就可以很少。然而事情并沒有這么簡單,所以記錄一下。
首先看一下最終的效果:
驗證碼識別過程
1、利用爬蟲采集圖像
這里最好大小圖都采集,剛開始我就只采集了小圖,就踩了一個坑,因為只有小圖很難通過小圖旋轉到正確位置。并不能通過眼睛等特征來確定是否選擇正了,因為有很多圖片本身頭的歪的。所以就會導致最終識別結果偏差較大。
(1)采集大圖
(2)采集小圖
2、人工標記
為了保證旋轉到正確的角度,我還專門開發(fā)了一個標記小工具如下圖。
可以通過拖動滑塊旋轉小圖到大概位置,再通過點擊按鈕進行微調,旋轉到絕對正確的角度。
這樣能保證我標記的圖片角度100%正確,只有提升了標記數(shù)據(jù)的質量,才會讓最終識別的效果達到最好。
3、訓練模型
4、測試驗證
我們將訓練好的模型用100張圖片來進行測試,發(fā)現(xiàn)只有4張圖片旋轉角度有問題,所以最終模型的實際正確率為96%。
如果再想提升正確率,可以再增加訓練的數(shù)據(jù)量,就需要再投入大量人力,這個投入與提升產(chǎn)出比需要自己權衡。
5、實戰(zhàn)測試
這里我就直接上代碼,就是文章開通動圖的演示效果。我也將模型封裝成了免費的接口給感興趣的小伙伴調用:得塔云
__author__ = 'Xin Yan Deng' import os import sys import time import requests import random import base64 from io import BytesIO from PIL import Image 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.common.action_chains import ActionChains # PIL圖片保存為base64編碼 def PIL_base64(img, coding='utf-8'): img_format = img.format if 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 shibie(img): url = "http://www.detayun.cn/openapi/verify_code_identify/" data = { # 用戶的key "key":"", # 驗證碼類型 "verify_idf_id":"24", # 樣例圖片 "img_base64":PIL_base64(img), "img_byte": None, # 中文點選,空間語義類型驗證碼的文本描述(這里缺省為空字符串) "words":"" } header = {"Content-Type": "application/json"} # 發(fā)送請求調用接口 response = requests.post(url=url, json=data, headers=header) print(response.text) return response.json() driver = webdriver.Chrome(executable_path='.\webdriver\chromedriver.exe') # 加載防檢測js with open('.\webdriver\stealth.min.js') as f: js = f.read() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": js }) driver.get('https://www.xiaohongshu.com/website-login/captcha?redirectPath=https%3A%2F%2Fwww.xiaohongshu.com%2Fexplore&verifyUuid=shield-4f9bcc31-0bc0-462a-843a-e60239713e46&verifyType=101&verifyBiz=461') driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": js }) time.sleep(5) for i in range(10): # 等待【旋轉圖像】元素出現(xiàn) WebDriverWait(driver, 5).until(lambda x: x.find_element_by_xpath('//div[@id="red-captcha-rotate"]/img')) # 找到【旋轉圖像】元素 tag1 = driver.find_element_by_xpath('//div[@id="red-captcha-rotate"]/img') # 獲取圖像鏈接 img_url = tag1.get_attribute('src') print(img_url) header = { "Host": "picasso-static.xiaohongshu.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "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", "Cookie": "xsecappid=login; a1=1896916369fehn0yq7nomanvre3fghfkj0zubt7zx50000120287; webId=75af27905db67b6fcb29a4899d200062; web_session=030037a385d8a837e5e590cace234a6e266fd5; gid=yYjKjyK484VKyYjKjyKqK89WjidxI8vAWIl6uuC0IhFdq728ikxiTD888yJ8JYW84DySKW0Y; webBuild=2.17.8; websectiga=634d3ad75ffb42a2ade2c5e1705a73c845837578aeb31ba0e442d75c648da36a; sec_poison_id=41187a04-9f82-4fbc-8b98-d530606b7696", "Upgrade-Insecure-Requests": "1", "If-Modified-Since": "Thu, 06 Jul 2023 11:42:07 GMT", "If-None-Match": '"7e53c313a9f321775e8f5e190de21081"', "TE": "Trailers", } # 下載圖片 response = requests.get(url=img_url, headers=header) img = Image.open(BytesIO(response.content)) img.convert('RGB').save('train_img/{}.jpg'.format(int(time.time() * 1000))) res = shibie(img) angle = int(str(res['data']['res_str']).replace('順時針旋轉','').replace('度','')) print(angle) # img = img.rotate(360 - angle, fillcolor=(0, 0, 0)) # img.show() # 等待【旋轉圖像】元素出現(xiàn) WebDriverWait(driver, 5).until(lambda x: x.find_element_by_xpath('//div[@class="red-captcha-slider"]')) # 找到【旋轉圖像】元素 tag2 = driver.find_element_by_xpath('//div[@class="red-captcha-slider"]') # 滑動滑塊 action = ActionChains(driver) action.click_and_hold(tag2).perform() time.sleep(1) # 計算實際滑動距離 = 像素距離 + 前面空白距離 move_x = angle * 0.79 # 滑動1:直接滑動 action.move_by_offset(move_x, 5) # 滑動2:分段滑動 # n = (random.randint(3, 5)) # move_x = move_x / n # for i in range(n): # action.move_by_offset(move_x, 5) # time.sleep(0.01) time.sleep(1) action.release().perform() time.sleep(2)
6、總結分析
(1)和百度相比,圖片標注變簡單了,圖像種類比百度少了一半。
(2)和百度相比,對 selenium 檢測更厲害了,我用火狐+反檢測一直過不了,使用谷歌+反檢測可以通過,但是滑對了也會多次驗證
(3)和百度相比,滑動軌跡檢測更厲害了,目前不是太確定,因為我一次快速滑動,還是分段滑動效果感覺差不多
各位大神如果對滑動提高通過率,或者有其他建議都可以給我留言,或私信我,謝謝指點。
到此這篇關于Python小紅書旋轉驗證碼識別的文章就介紹到這了,更多相關python旋轉驗證碼識別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解利用OpenCV提取圖像中的矩形區(qū)域(PPT屏幕等)
這篇文章主要介紹了詳解利用OpenCV提取圖像中的矩形區(qū)域(PPT屏幕等),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-07-07解決python3中的requests解析中文頁面出現(xiàn)亂碼問題
requests是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務器響應數(shù)據(jù)時經(jīng)常會用到。這篇文章給大家介紹了解決python3中的requests解析中文頁面出現(xiàn)亂碼問題,感興趣的朋友一起看看吧2019-04-04淺談Keras中fit()和fit_generator()的區(qū)別及其參數(shù)的坑
這篇文章主要介紹了Keras中fit()和fit_generator()的區(qū)別及其參數(shù)的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Python Sweetviz輕松實現(xiàn)探索性數(shù)據(jù)分析
Sweetviz是一個開放源代碼Python庫,可生成精美的高密度可視化文件,以單行代碼啟動EDA(探索性數(shù)據(jù)分析)。輸出是一個完全獨立的HTML應用程序,該系統(tǒng)圍繞快速可視化目標值和比較數(shù)據(jù)集而構建。其目標是幫助快速分析目標特征,訓練與測試數(shù)據(jù)以及其他此類數(shù)據(jù)表征任務2021-11-11