Python + EasyOCR 实现英文数字验证码识别

一、安装环境

  1. 安装 Python
    确保你已安装 Python 3.7 及以上版本。

  2. 安装依赖库
    使用 pip 安装以下依赖:

pip install easyocr opencv-python pillow
二、识别验证码图像
保存验证码图像为 captcha.png,然后新建 recognize_captcha.py 文件,代码如下:

import easyocr更多内容访问ttocr.com或联系1436423940
from PIL import Image
import cv2
import numpy as np

图像预处理函数(可选)

def preprocess_image(image_path):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
_, thresh = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
return thresh

保存处理后的图像(用于可视化调试)

def save_image(image_array, output_path="processed.png"):
cv2.imwrite(output_path, image_array)

主函数

def recognize(image_path):
# 预处理
processed = preprocess_image(image_path)
save_image(processed)

# 初始化 OCR 引擎(英文)
reader = easyocr.Reader(['en'])

# 识别文字
result = reader.readtext(processed)

# 输出结果
if result:
    print("识别结果:", ''.join([res[1] for res in result]))
else:
    print("未识别到文字")

执行识别

recognize("captcha.png")
三、运行代码
确保你有一个英文数字验证码图像 captcha.png,然后运行:

python recognize_captcha.py
输出示例:

识别结果: 7GHT9

posted @ 2025-05-10 13:27  ttocr、com  阅读(27)  评论(0)    收藏  举报