Python中識別圖片/滑塊驗證碼準確率極高的ddddocr庫詳解
前言
驗證碼的種類有很多,它是常用的一種反爬手段,包括:圖片驗證碼,滑塊驗證碼,等一些常見的驗證碼場景。
識別驗證碼的python 庫有很多,用起來也并不簡單,這里推薦一個簡單實用的識別驗證碼的庫 ddddocr (帶帶弟弟ocr)庫.
環(huán)境準備
python 版本要求小于等于python3.9 版本
pip 安裝
pip install ddddocr
下載的安裝包比較大,一般用國內(nèi)的下載源可以加快下載速度
pip install ddddocr -i https://pypi.douban.com/simple
github地址 https://github.com/sml2h3/ddddocr
快速開始
先隨便找個純英文的驗證碼,保持為a1.png
代碼示例
import ddddocr # 導入 ddddocr ocr = ddddocr.DdddOcr() # 實例化 with open('a1.png', 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 res = ocr.classification(img_bytes) # 識別 print(res)
運行結果
已經(jīng)能識別到 xnen ,但是會出現(xiàn)"歡迎使用ddddocr,本項目專注帶動行業(yè)內(nèi)卷…"提示語, 可以加一個參數(shù)show_ad=False
import ddddocr # 導入 ddddocr ocr = ddddocr.DdddOcr(show_ad=False) # 實例化 with open('a1.png', 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 res = ocr.classification(img_bytes) # 識別 print(res)
圖片驗證碼
識別一下三種驗證碼
代碼示例
import ddddocr # 導入 ddddocr ocr = ddddocr.DdddOcr(show_ad=False) # 實例化 with open('a2.png', 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 res2 = ocr.classification(img_bytes) # 識別 print(res2) with open('a3.png', 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 res3 = ocr.classification(img_bytes) # 識別 print(res3) with open('a4.png', 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 res4 = ocr.classification(img_bytes) # 識別 print(res4)
運行結果
giv6j
zppk
4Tskh
滑塊驗證碼
滑塊驗證碼場景如下場景示例
先摳出2張圖片,分別為background.png 和 target.png
解決問題的重點是計算缺口的位置
import ddddocr det = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False) with open('target.png', 'rb') as f: target_bytes = f.read() with open('background.png', 'rb') as f: background_bytes = f.read() res = det.slide_match(target_bytes, background_bytes, simple_target=True) print(res)
運行結果
{'target_y': 0, 'target': [184, 58, 246, 120]}
target 的四個值就是缺口位置的左上角和右下角的左邊位置
識別中文
識別圖片上的文字
import ddddocr import cv2 det = ddddocr.DdddOcr(det=True) with open("test.png", 'rb') as f: image = f.read() poses = det.detection(image) im = cv2.imread("test.png") for box in poses: x1, y1, x2, y2 = box im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2) cv2.imwrite("result.jpg", im)
保存后的圖片
到此這篇關于Python中識別圖片/滑塊驗證碼準確率極高的ddddocr庫詳解的文章就介紹到這了,更多相關Python ddddocr庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python基于pygame實現(xiàn)單機版五子棋對戰(zhàn)
這篇文章主要為大家詳細介紹了Python基于pygame實現(xiàn)單機版五子棋對戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12