python图片识别(但是有时候识别不精准,有没有大佬帮忙解答):)

适合于selenium自动化测试对于图片验证码的识别

安装 Tesseract OCR:

首页 ·UB-曼海姆/tesseract Wiki

使用事列:

python 复制代码
import pytesseract
from PIL import Image
from io import BytesIO
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 指定Tesseract OCR的可执行文件路径(根据实际安装路径修改)
# Windows系统示例
pytesseract.pytesseract.tesseract_cmd = r'D:\tesseract-ocr\tesseract.exe'

# 1. 首先需要初始化WebDriver
driver = webdriver.Chrome()
try:
    # 2. 访问目标网页
    driver.get("https://www.gushiwen.cn/user/login.aspx?from=http://www.gushiwen.cn/user/collect.aspx")

    # 等待页面加载
    time.sleep(2)

    # 3. 定位验证码元素
    captcha_element = driver.find_element(By.ID, "imgCode")
    captcha_screenshot = captcha_element.screenshot_as_png

    # 4. 使用OCR识别验证码
    image = Image.open(BytesIO(captcha_screenshot))
    # 保存验证码图片用于调试
    image.save("captcha.png")
    text = pytesseract.image_to_string(image).strip()
    print(f"识别出的验证码: {text}")

    # 5. 输入验证码
    # driver.find_element(By.ID, "captcha_input").send_keys(text)

    # 6. 其他登录操作...
    # driver.find_element(By.ID, "username").send_keys("your_username")
    # driver.find_element(By.ID, "password").send_keys("your_password")
    # driver.find_element(By.ID, "login-btn").click()

    # 保持浏览器打开查看结果
    input("按回车键退出...")
finally:
    # 确保浏览器最终会被关闭
    driver.quit()

完整登录古诗文网的例子:

python 复制代码
import pytesseract
from PIL import Image, ImageFilter, ImageEnhance
from io import BytesIO
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import cv2
import numpy as np

# Tesseract路径(根据自己的安装路径改)
pytesseract.pytesseract.tesseract_cmd = r'D:\tesseract-ocr\tesseract.exe'


# 预处理函数
def preprocess(image):
    # 1. 转灰度
    gray = image.convert('L')
    # 2. 二值化
    threshold = 127
    gray = gray.point(lambda p: p > threshold and 255)
    # 3. 降噪
    gray = gray.filter(ImageFilter.MedianFilter())
    # 4. 增强对比度
    enhancer = ImageEnhance.Contrast(gray)
    gray = enhancer.enhance(2)
    return gray


# 初始化浏览器
driver = webdriver.Chrome()
try:
    # 访问登录页
    driver.get("https://www.gushiwen.cn/user/login.aspx?from=http://www.gushiwen.cn/user/collect.aspx")

    # 等待验证码加载
    captcha_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "imgCode"))
    )

    # 循环识别,直到成功(最多尝试3次)
    max_attempts = 3
    for attempt in range(max_attempts):
        # 截图验证码
        captcha_screenshot = captcha_element.screenshot_as_png
        image = Image.open(BytesIO(captcha_screenshot))

        # 预处理
        image = preprocess(image)

        # OCR识别(限定字符+模式)
        text = pytesseract.image_to_string(
            image,
            config="--psm 10 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        ).strip()

        # 验证长度(假设4位)
        if len(text) == 4:
            break
        else:
            # 识别失败,刷新验证码
            captcha_element.click()
            time.sleep(1)
            captcha_element = WebDriverWait(driver, 10).until(
                EC.visibility_of_element_located((By.ID, "imgCode"))
            )

    # 填充登录信息
    driver.find_element(By.ID, "email").send_keys("用户名")
    driver.find_element(By.ID, "pwd").send_keys("密码")
    driver.find_element(By.ID, "code").send_keys(text)
    driver.find_element(By.ID, "denglu").click()
    # print(text)
    
    # 保持浏览器打开
    input("按回车键退出...")

finally:
    driver.quit()
相关推荐
小兔崽子去哪了11 小时前
Python 数据分析环境搭建与工具使用指南
python
血小溅11 小时前
Spring Boot 整合 Spring AI:接入 DeepSeek 与 Ollama 调用大模型
后端·ollama·deepseek
李慕婉学姐12 小时前
Springboot的民宿管理系统的设计与实现29rhm9uh(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
不惑_12 小时前
Java 使用 FileOutputStream 写 Excel 文件不落盘?
开发语言·python
IT小哥哥呀12 小时前
Python实用技巧:批量处理Excel数据并生成销售报表(含实战案例)
python·pandas·数据可视化·数据处理·报表生成·excel自动化·办公神器
用户30745969820712 小时前
门面(Facade)—— 静态语法的“动态伪装术”
后端·php
辜月十12 小时前
CentOS7 离线安装字体
后端
烤奶要加冰12 小时前
PyCharm 社区版全平台安装指南
ide·windows·python·pycharm·mac
Siren_dream12 小时前
anaconda与pycharm
ide·python·pycharm
whale fall12 小时前
Windows下PyCharm如何激活python的虚拟环境
ide·python·pycharm