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()
相关推荐
fyakm12 小时前
python和java爬虫优劣对比
java·爬虫·python
BYSJMG12 小时前
计算机大数据毕业设计推荐:基于Spark的新能源汽车保有量可视化分析系统
大数据·分布式·python·spark·django·编辑器·课程设计
Pocker_Spades_A12 小时前
Python快速入门专业版(三):print 格式化输出:% 占位符、format 方法与 f-string(谁更高效?)
开发语言·python
nightunderblackcat12 小时前
新手向:AI IDE+AI 辅助编程
开发语言·python·microsoft·信息可视化
xdpcxq102913 小时前
Java项目打包成EXE全攻略
java·python·pycharm
Pocker_Spades_A13 小时前
Python快速入门专业版(二):print 函数深度解析:不止于打印字符串(含10+实用案例)
开发语言·python·microsoft
java1234_小锋13 小时前
[免费]基于Python的Django+Vue图书借阅推荐系统【论文+源码+SQL脚本】
开发语言·python·django
IT_陈寒13 小时前
Spring Boot 3 + GraalVM:5个实战技巧让Java应用启动速度提升300%
前端·人工智能·后端
无奈何杨13 小时前
CoolGuard风控系统配置评分卡、权重策略|QLExpress脚本
前端·后端
Pocker_Spades_A13 小时前
Python快速入门专业版(一):Windows/macOS/Linux 系统环境搭建(附常见报错解决)
windows·python·macos