使用 Python + OpenCV + Tesseract OCR 自动识别

验证码(CAPTCHA)用于防止机器人恶意提交数据,但在自动化测试和数据采集中,我们有时需要自动识别验证码。本文将介绍如何使用 Python 结合 OpenCV 和 Tesseract OCR 进行验证码识别,并优化识别效果。

  1. 环境安装

在开始之前,我们需要安装 Python 及相关依赖库。

1.1 安装 Python

如果尚未安装 Python,请前往 Python 官方网站
下载并安装最新版本。安装完成后,使用以下命令检查是否安装成功:
更多内容访问ttocr.com或联系1436423940
python --version

1.2 安装 Tesseract OCR
Windows 用户

从 Tesseract OCR 官方 GitHub
下载并安装 Windows 版本。

记住 Tesseract 的安装路径,例如 C:\Program Files\Tesseract-OCR\tesseract.exe。

Linux/macOS 用户

Ubuntu

sudo apt update && sudo apt install tesseract-ocr

macOS (使用 Homebrew)

brew install tesseract

安装完成后,检查是否成功安装:

tesseract --version

1.3 安装 Python 依赖库

在 Python 代码中使用 OCR 和图像处理功能,需要安装以下库:

pip install pytesseract opencv-python numpy pillow

  1. 代码实现

下面的 Python 代码示例展示了如何读取验证码图像、进行预处理,并使用 Tesseract OCR 进行识别。

2.1 代码示例
import cv2
import numpy as np
import pytesseract
from PIL import Image

Windows 需指定 Tesseract 路径

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

def preprocess_image(image_path):
"""加载验证码图像并进行预处理"""
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # 转为灰度图像

# 二值化处理
_, binary_img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# 形态学去噪(可选)
kernel = np.ones((2, 2), np.uint8)
processed_img = cv2.morphologyEx(binary_img, cv2.MORPH_OPEN, kernel)

return processed_img

def recognize_captcha(image_path):
"""使用 Tesseract OCR 识别验证码"""
processed_img = preprocess_image(image_path)

# OpenCV 图像转换为 PIL 格式
pil_img = Image.fromarray(processed_img)

# 设定 Tesseract 参数(psm 6 适用于验证码)
custom_config = r'--oem 3 --psm 6'

# OCR 识别
text = pytesseract.image_to_string(pil_img, config=custom_config)

return text.strip()

if name == "main":
captcha_image = "captcha.png" # 替换为你的验证码图片路径
result = recognize_captcha(captcha_image)
print(f"识别出的验证码: {result}")

  1. 代码解析
    3.1 图像预处理

为了提高 OCR 识别率,我们对验证码图像进行了以下优化处理:

灰度化:转换为黑白图像,减少颜色干扰。

二值化:通过 cv2.threshold() 提高对比度,使字符更清晰。

形态学去噪(可选):使用 cv2.morphologyEx() 进行 开运算 去除小噪点,提高识别率。

3.2 OCR 解析

pytesseract.image_to_string() 进行文字识别。

设置 psm 6 适用于单行验证码文本,提高识别效果。

  1. 运行程序

将代码保存为 captcha_solver.py,并确保 captcha.png 图片在同一目录下,然后运行:

python captcha_solver.py

程序会加载验证码图像,进行预处理,并输出识别的文本。

  1. 提升验证码识别准确率
    5.1 调整 PSM 模式

Tesseract 提供了不同的 页面分割模式(PSM),验证码通常使用 PSM 6 或 PSM 7:

custom_config = r'--oem 3 --psm 6' # 适用于单行验证码

5.2 指定语言

如果验证码只包含 数字 和 英文字母,可以指定 eng 语言,以减少错误识别:

text = pytesseract.image_to_string(pil_img, lang="eng", config=custom_config)

5.3 进一步优化图像

去噪:可使用 中值滤波 或 高斯模糊 降低噪点干扰。

字符分割:对于字符粘连的验证码,可以尝试轮廓检测进行分割,提高单字符识别率。

5.4 采用深度学习 OCR 方案

如果 Tesseract 不能满足需求,可以考虑使用深度学习 OCR,如:

EasyOCR

PaddleOCR

posted @ 2025-09-07 09:46  ttocr、com  阅读(17)  评论(0)    收藏  举报