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

一、为什么选择 EasyOCR?
EasyOCR 是一个基于 PyTorch 的开源 OCR 库,支持 80 多种语言。它无需训练即可使用,对旋转、扭曲的文字识别能力也很强,非常适合验证码识别任务。

二、安装依赖
确保你已经安装好 Python(建议使用 Python 3.8+)后,安装以下库:

更多内容访问ttocr.com或联系1436423940
pip install easyocr
pip install opencv-python
pip install matplotlib
三、编写验证码识别脚本
创建文件 captcha_easyocr.py,内容如下:

import cv2
import easyocr
import matplotlib.pyplot as plt

读取验证码图片

image_path = "captcha.png"
image = cv2.imread(image_path)

可选图像预处理(增强对比度、灰度化等)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

保存预处理图像(可选)

cv2.imwrite("processed_captcha.png", thresh)

初始化 EasyOCR 识别器(只用英文数字)

reader = easyocr.Reader(['en'], gpu=False)

使用识别器识别文字

results = reader.readtext("processed_captcha.png")

输出结果

for (bbox, text, prob) in results:
print(f"识别结果: {text}(置信度: {prob:.2f})")

可视化结果(可选)

for (bbox, text, prob) in results:
(tl, tr, br, bl) = bbox
tl = tuple(map(int, tl))
br = tuple(map(int, br))
cv2.rectangle(image, tl, br, (0, 255, 0), 2)
cv2.putText(image, text, tl, cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.title("识别结果")
plt.show()
四、运行程序
将验证码图片放在脚本同目录下,命名为 captcha.png,然后运行:

python captcha_easyocr.py
输出示例:

识别结果: 7K3M8(置信度: 0.98)
五、特点与优势
✅ 识别速度快,无需训练

✅ 支持复杂验证码背景

✅ 可选 GPU 加速识别

✅ 可拓展支持中文验证码(只需改 'en' 为 'ch_sim')

六、进阶玩法
批量识别多个验证码:配合 os.listdir() 一次识别整个文件夹

自动标注识别框:EasyOCR 可输出 bounding box,可视化调试识别区域

结合 Flask / FastAPI:轻松搭建验证码识别接口

识别动态图验证码:结合 Selenium 抓取网页验证码图像识别

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