验证码识别系统开发实战指南
一、项目架构设计
验证码识别系统的核心组件包括:
数据生成模块 - 创建训练数据集
预处理模块 - 图像增强和标准化
模型训练模块 - 深度学习模型开发
部署服务模块 - 生产环境API服务
更多内容访问ttocr.com或联系1436423940
二、数据生成实现
2.1 基础验证码生成
python
from PIL import Image, ImageDraw, ImageFont
import random
import string
def generate_captcha(text_length=4):
# 创建空白图像
img = Image.new('RGB', (120, 40), color=(255, 255, 255))
draw = ImageDraw.Draw(img)
# 生成随机文本
chars = string.ascii_letters + string.digits
text = ''.join(random.choice(chars) for _ in range(text_length))
# 绘制文本
font = ImageFont.load_default()
text_width, text_height = draw.textsize(text, font=font)
draw.text(
((120-text_width)/2, (40-text_height)/2),
text,
fill=(0, 0, 0),
font=font
)
return text, img
2.2 添加干扰元素
python
def add_noise(image):
draw = ImageDraw.Draw(image)
# 添加干扰线
for _ in range(3):
start = (random.randint(0, 120), random.randint(0, 40))
end = (random.randint(0, 120), random.randint(0, 40))
draw.line([start, end], fill=(random.randint(0, 255),
random.randint(0, 255), random.randint(0, 255)), width=1)
# 添加噪点
for _ in range(100):
xy = (random.randint(0, 120), random.randint(0, 40))
draw.point(xy, fill=(random.randint(0, 255),
random.randint(0, 255), random.randint(0, 255)))
return image
三、模型开发
3.1 CNN模型实现
python
import tensorflow as tf
from tensorflow.keras import layers
def build_model(char_count):
model = tf.keras.Sequential([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(40, 120, 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(char_count*4, activation='softmax'),
layers.Reshape((4, char_count))
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
3.2 数据预处理
python
import numpy as np
def preprocess_data(images, labels, char_to_idx):
# 图像归一化
images = np.array([np.array(img.convert('L'))/255.0 for img in images])
images = np.expand_dims(images, axis=-1)
# 标签编码
encoded_labels = []
for text in labels:
encoded = [char_to_idx[c] for c in text]
encoded_labels.append(encoded)
return images, np.array(encoded_labels)
四、训练流程
4.1 准备数据集
python
生成训练数据
char_set = string.ascii_letters + string.digits
char_to_idx = {c:i for i,c in enumerate(char_set)}
train_texts, train_images = [], []
for _ in range(1000):
text, img = generate_captcha()
img = add_noise(img)
train_texts.append(text)
train_images.append(img)
X_train, y_train = preprocess_data(train_images, train_texts, char_to_idx)
4.2 模型训练
python
model = build_model(len(char_set))
model.fit(X_train, y_train, epochs=10, batch_size=32)
五、部署应用
5.1 预测函数
python
def predict_captcha(model, image_path, char_set):
img = Image.open(image_path).convert('L')
img = img.resize((120, 40))
img_array = np.array(img)/255.0
img_array = np.expand_dims(img_array, axis=(0,-1))
pred = model.predict(img_array)
pred_text = ''.join([char_set[i] for i in np.argmax(pred[0], axis=1)])
return pred_text
5.2 Flask API服务
python
from flask import Flask, request, jsonify
app = Flask(name)
model = tf.keras.models.load_model('captcha_model.h5')
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
img = Image.open(file.stream).convert('L')
# 预处理
img = img.resize((120, 40))
img_array = np.array(img)/255.0
img_array = np.expand_dims(img_array, axis=(0,-1))
# 预测
pred = model.predict(img_array)
result = ''.join([char_set[i] for i in np.argmax(pred[0], axis=1)])
return jsonify({'result': result})
if name == 'main':
app.run(host='0.0.0.0', port=5000)
浙公网安备 33010602011771号