验证码识别极简教程:150行Python代码搞定
一、环境准备(3个核心库)
bash
pip install tensorflow opencv-python pillow
二、完整代码(captcha_solver.py)
python
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import random
import string
from tensorflow import keras
from tensorflow.keras import layers
更多内容访问ttocr.com或联系1436423940
配置参数
CHARS = string.digits + string.ascii_uppercase # 识别字符
CAPTCHA_LEN = 4 # 验证码长度
IMG_SIZE = (160, 60) # 图片尺寸
1. 验证码生成器
def gen_captcha():
"""生成单张验证码"""
text = ''.join(random.choices(CHARS, k=CAPTCHA_LEN))
img = Image.new('RGB', IMG_SIZE, (255, 255, 255))
draw = ImageDraw.Draw(img)
# 绘制扭曲文字
x = 10
for i, c in enumerate(text):
y = random.randint(5, IMG_SIZE[1]-40)
angle = random.randint(-30, 30)
char_img = Image.new('RGBA', (40, 40), (0, 0, 0, 0))
ImageDraw.Draw(char_img).text((0, 0), c, fill=(0, 0, 0))
char_img = char_img.rotate(angle, expand=1)
img.paste(char_img, (x, y), char_img)
x += 30 + random.randint(-8, 8)
# 添加干扰线
for _ in range(3):
draw.line([(random.randint(0, IMG_SIZE[0]),
(random.randint(0, IMG_SIZE[1]))] * 2,
fill=(random.randint(0, 255),)*3, width=1)
return text, np.array(img)
2. 创建数据集
def create_data(samples=1000):
"""生成训练数据"""
X = np.zeros((samples, IMG_SIZE[1], IMG_SIZE[0], 1))
y = np.zeros((samples, CAPTCHA_LEN))
char_map = {c:i for i,c in enumerate(CHARS)}
for i in range(samples):
text, img = gen_captcha()
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
X[i] = np.expand_dims(gray/255.0, axis=-1)
y[i] = [char_map[c] for c in text]
return X, y
3. 构建CNN模型
def build_model():
model = keras.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG_SIZE[::-1], 1)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(len(CHARS)CAPTCHA_LEN, activation='softmax'),
layers.Reshape((CAPTCHA_LEN, len(CHARS)))
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
4. 训练与测试
if name == 'main':
# 生成数据
X, y = create_data(1500)
# 分割数据集
split = int(0.8 * len(X))
X_train, y_train = X[:split], y[:split]
X_test, y_test = X[split:], y[split:]
# 训练模型
model = build_model()
model.fit(X_train, y_train,
validation_data=(X_test, y_test),
epochs=20,
batch_size=32)
# 测试识别
test_text, test_img = gen_captcha()
test_gray = cv2.cvtColor(test_img, cv2.COLOR_RGB2GRAY)
test_input = np.expand_dims(test_gray/255.0, axis=(0,-1))
pred = model.predict(test_input)
result = ''.join([CHARS[i] for i in np.argmax(pred[0], axis=1)])
# 显示结果
print(f"真实验证码: {test_text}")
print(f"识别结果: {result}")
cv2.imshow('Captcha', test_img)
cv2.waitKey(0)
三、代码解析
数据生成:
动态生成含扭曲文字和干扰线的验证码
无需准备真实数据集
模型结构:
2层CNN提取特征
全连接层输出预测结果
训练流程:
自动生成1500个样本
20个epoch训练
输出最终测试结果
四、使用方法
保存为captcha_solver.py
安装依赖:pip install tensorflow opencv-python pillow
直接运行:python captcha_solver.py