最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python驗(yàn)證碼識別方式(使用pytesseract庫)

 更新時間:2025年06月04日 10:50:09   作者:深海藍(lán)山的博客  
這篇文章主要介紹了Python驗(yàn)證碼識別方式(使用pytesseract庫),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python中使用pytesseract庫進(jìn)行ocr識別,需要安裝Tesseract-OCR,通過指定pytesseract.tesseract_cmd路徑,可以將esseract-OCR集成到pytho程序中,避免客戶端電腦的依賴。

1、安裝Tesseract-OCR

Tesseract是一個高度精確的開源OCR(光學(xué)字符識別)系統(tǒng),廣泛應(yīng)用于文本識別項(xiàng)目中。

下載地址:

安裝程序:下載后安裝程序

中文包下載:

  • 地址:https://gitcode.com/open-source-toolkit/90e2f
  • 下載了最新版本的chi-sim.traineddata文件,復(fù)制到Tesseract的tessdata目錄下
  • 通常,路徑類似于C:\Program Files\tesseract\tessdata(Windows)
  • 或 /usr/share/tesseract-ocr/4.00/tessdata(Linux)。

2、在python中使用

安裝依賴

pip install pytesseract

3、本地圖片識別

import pytesseract
from PIL import Image

# 獲取文件的絕對路徑
def get_abspath(filename):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, filename))
        # print(f"get_abspath文件路徑:{filename}")
        return filename
    except Exception as e:
        print(f"獲取文件絕對路徑時出現(xiàn)錯誤: {e}")
        return ""
        
# 手動指定路徑(Windows常見) Tesseract 系統(tǒng)路徑
driver_path = r"Tesseract-OCR\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)

#使用示例
if __name__ == "__main__":
  # 1 識別本地圖片 
  # 英文識別
  current_dir = os.getcwd()
    filename = os.path.normpath(os.path.join(current_dir, f"code.jpg"))
    file = Image.open(filename)
    text = pytesseract.image_to_string(file, lang="eng")
    print(text)
  #中文識別,需要下載語言包
    filename = os.path.normpath(os.path.join(current_dir, f"sushi.png"))
    file = Image.open(filename)
    text = pytesseract.image_to_string(file, lang='chi_sim') 
    print(f"識別結(jié)果:{text}")

識別結(jié)果示例:

4、結(jié)合playwright動態(tài)識別網(wǎng)站驗(yàn)證碼

import os
import pytesseract
from PIL import Image
from playwright.sync_api import Playwright
import tools.pwHander as pwHander
from PIL import Image

# 獲取文件的絕對路徑
def get_abspath(filename):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, filename))
        # print(f"get_abspath文件路徑:{filename}")
        return filename
    except Exception as e:
        print(f"獲取文件絕對路徑時出現(xiàn)錯誤: {e}")
        return ""
        
# 手動指定路徑(Windows常見) Tesseract 系統(tǒng)路徑
driver_path = r"Tesseract-OCR\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)

# 驗(yàn)證碼圖片識別
def get_captcha(page: Playwright, element_selector="img#captcha", file_name="code.jpg"):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, f"{file_name}"))
               
        # 通過class選擇器獲取img元素
        code_img = page.locator(element_selector)
        if not code_img:
            raise ValueError("驗(yàn)證碼元素未找到!")

        # 刷新驗(yàn)證碼
        # code_img.click()

        # 下載驗(yàn)證碼圖片
        code_img.screenshot(path=filename)

        file = Image.open(filename)
        text = pytesseract.image_to_string(file, lang="eng")
        print("驗(yàn)證碼識別結(jié)果:", text)
        return text.strip()
    except Exception as e:
        print(f"獲取驗(yàn)證碼 失?。簕str(e)}")
        return ""
#使用示例
if __name__ == "__main__":
  # 2 動態(tài)識別網(wǎng)站驗(yàn)證碼
  with sync_playwright() as p:
   		browser = p.chromium.launch(headless=False, slow_mo=1000)
        context = browser.new_context()
        page = context.new_page()
        page.goto("測試網(wǎng)址")
	    # 驗(yàn)證碼圖片下載
	    imgText = get_captcha(page, "img#jcaptcha")
	    print(f"驗(yàn)證碼:{imgTest}")

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

鄱阳县| 普兰店市| 承德市| 登封市| 长岭县| 册亨县| 云林县| 墨脱县| 五大连池市| 皮山县| 盖州市| 忻城县| 石家庄市| 伽师县| 九龙坡区| 来宾市| 株洲市| 霍城县| 澄江县| 宜兴市| 清流县| 潍坊市| 古田县| 宝山区| 宁波市| 闻喜县| 从江县| 张家口市| 渝北区| 郁南县| 宜宾市| 兰坪| 五大连池市| 奇台县| 宜丰县| 彝良县| 渑池县| 漳州市| 云林县| 珲春市| 桓仁|