前言
①python第三方庫ddddocr(帶帶弟弟ocr)通用驗證碼識別。
②環境要求:python版本<=3.9。
調用方法
import ddddocr # 導入 ddddocr ocr = ddddocr.DdddOcr(use_gpu=False, device_id=0) # 實例化 file_name = 'test.png' with open(file_name, 'rb') as f: # 打開圖片 img_bytes = f.read() # 讀取圖片 content = ocr.classification(img_bytes=img_bytes) # 識別 print("識別到的內容 {}".format(content))
參數說明
Ddddocr 類接受兩個實例參數
參數名 | 默認值 | 說明 |
---|---|---|
use_gpu | False | Bool 是否使用gpu進行推理,如果該值為False則device_id不生效 |
device_id | 0 | int cuda設備號,目前僅支持單張顯卡 |
classification() 方法需要一個參數
參數名 | 默認值 | 說明 |
---|---|---|
img | 0 | bytes 圖片的bytes格式 |
下載安裝
pip install ddddocr
運行如下:
實例1
圖片示例:
代碼如下:
import ddddocr ocr = ddddocr.DdddOcr() file_name = 'test.png' with open(file_name, 'rb') as f: img_bytes = f.read() content = ocr.classification(img_bytes) print("識別到的內容 {}".format(content))
運行結果:
實例2
環境:win10 + python3.7 + selenium + ddddocr + PIL 實現自動化測試過程中識別驗證碼
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait import time import ddddocr from PIL import Image import os def getToken(): url = "http://localhost/#/login" from selenium.webdriver.firefox.options import Options firefox_options = Options() firefox_options.headless = False driver = webdriver.Firefox(options=firefox_options) # 設置等待時間 waite = WebDriverWait(driver, 5) driver.get(url) driver.implicitly_wait(6) # 1.先保存當前頁面為一個png圖 driver.get_screenshot_as_file("page.png") inputname = driver.find_elements_by_class_name("el-input__inner")[0] inputname.click() inputname.send_keys("10000") inputpassword = driver.find_elements_by_class_name("el-input__inner")[1] inputpassword.send_keys("123456") # choice = driver.find_element_by_class_name("person") choice = driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div[2]/div/div[2]/div[2]') choice.click() # 2.然后將頁面截圖中的驗證碼截取出來 image = Image.open("page.png") captchaurl = driver.find_element_by_class_name("login-code-img") left = captchaurl.location.get("x") top = captchaurl.location.get("y") right = left + captchaurl.size.get("width") bottom = top + captchaurl.size.get("height") cropImg = image.crop((left, top, right, bottom)) cropImg.save("code.png") # 3.讀取驗證碼圖 with open('code.png','rb') as f: content_captcha = f.read() # print(content_captcha) # 4.此處是重點,解析驗證碼,得到4位數字,只需要2行代碼 ocr = ddddocr.DdddOcr() code = ocr.classification(content_captcha) print(code) inputcaptcha = driver.find_elements_by_class_name("el-input__inner")[2] inputcaptcha.send_keys(code) button = driver.find_element_by_tag_name("button") button.click() # logs = driver.get_log('performance') cookie = driver.get_cookies() print(cookie) if cookie != []: token = cookie[0]['value'] else: token = "" driver.quit() return token token = "" result = False while result == False: token = getToken() if token != "": result = True print(token)
未完成:
https://www.cnblogs.com/mrlayfolk/p/12617077.html