教你使用TensorFlow2识别验证码

验证码是根据随机字符生成一幅图片,然后在图片中加入干扰象素,用户必须手动填入,防止有人利用机器人自动批量注册、灌水、发垃圾广告等等 。

数据集来源:

图片是5个字母的单词,可以包含数字。这些图像应用了噪声(模糊和一条线)。它们是200 x 50 png。我们的任务是尝试制作光学字符识别算法的模型。

在数据集中存在的验证码png图片,对应的标签就是图片的名字。

import os
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
# imgaug 图片数据增强
import imgaug.augmenters as iaa
import tensorflow as tf
# conv2d maxpooling2d dropout flatten dense bn  gap
from tensorflow.keras.layers import conv2d, maxpooling2d, dropout, flatten, dense, layer, batchnormalization, globalaveragepooling2d 
from tensorflow.keras.optimizers import adam
from tensorflow.keras import model, input 
from tensorflow.keras.callbacks import earlystopping, reducelronplateau
# 图片处理器
from tensorflow.keras.preprocessing.image import imagedatagenerator
import plotly.express as px
import plotly.graph_objects as go
import plotly.offline as pyo
pyo.init_notebook_mode()

对数据进行一个简单的分析,统计图像中大约出现了什么样的符号。

# 数据路径
dir = '../input/captcha-version-2-images/samples/samples'
# 存储验证码的标签
captcha_list = []
characters = {}
for captcha in os.listdir(dir):
    captcha_list.append(captcha)
    # 每张验证码的captcha_code
    captcha_code = captcha.split(".")[0]
    for i in captcha_code:
        # 遍历captcha_code 
        characters[i] = characters.get(i, 0) +1
symbols = list(characters.keys())
len_symbols = len(symbols)
print(f'图像中只使用了{len_symbols}符号')

plt.bar(*zip(*characters.items()))
plt.title('frequency of symbols')
plt.show()

如何提取图像的数据建立x,y??

# 如何提取图像 建立 model  x 的shape  1070 * 50 * 200 * 1 
# y的shape 5 * 1070 * 19
 
for i, captcha in enumerate(captcha_list):
    captcha_code = captcha.split('.')[0]
    # cv2.imread_grayscale 灰度图
    captcha_cv2 = cv2.imread(os.path.join(dir, captcha),cv2.imread_grayscale)
    # 缩放
    captcha_cv2 = captcha_cv2 / 255.0
    # print(captcha_cv2.shape) (50, 200) 
    # 将captcha_cv2的(50, 200) 切换成(50, 200, 1)
    captcha_cv2 = np.reshape(captcha_cv2, img_shape)
    # (5,19)
    targs = np.zeros((len_captcha, len_symbols))
    
    for a, b in enumerate(captcha_code):
        targs[a, symbols.index(b)] = 1
    x[i] = captcha_cv2
    y[:, i] = targs

print("shape of x:", x.shape)
print("shape of y:", y.shape)

输出如下

print("shape of x:", x.shape)
print("shape of y:", y.shape)

通过numpy中random 随机选择数据,划分训练集和测试集

# 生成随机数
from numpy.random import default_rng

rng = default_rng(seed=1)
test_numbers = rng.choice(1070, size=int(1070*0.3), replace=false)
x_test = x[test_numbers]
x_full = np.delete(x, test_numbers,0)
y_test = y[:,test_numbers]
y_full = np.delete(y, test_numbers,1)

val_numbers = rng.choice(int(1070*0.7), size=int(1070*0.3), replace=false)

x_val = x_full[val_numbers]
x_train = np.delete(x_full, val_numbers,0)
y_val = y_full[:,val_numbers]
y_train = np.delete(y_full, val_numbers,1)

在此验证码数据中,容易出现过拟合的现象,你可能会想到添加更多的新数据、 添加正则项等, 但这里使用数据增强的方法,特别是对于机器视觉的任务,数据增强技术尤为重要。

常用的数据增强操作:imgaug库。imgaug是提供了各种图像增强操作的python库 https://github.com/aleju/imgaug

imgaug几乎包含了所有主流的数据增强的图像处理操作, 增强方法详见github

# sequential(c, r)	 尺寸增加了5倍,
# 选取一系列子增强器c作用于每张图片的位置,第二个参数表示是否对每个batch的图片应用不同顺序的augmenter list     # rotate=(-8, 8)  旋转
# iaa.cropandpad  截取(crop)或者填充(pad),填充时,被填充区域为黑色。
# px: 想要crop(negative values)的或者pad(positive values)的像素点。
# (top, right, bottom, left)
# 当pad_mode=constant的时候选择填充的值
aug =iaa.sequential([iaa.cropandpad(
    px=((0, 10), (0, 35), (0, 10), (0, 35)),
    pad_mode=['edge'],
    pad_cval=1
),iaa.rotate(rotate=(-8,8))])

x_aug_train = none
y_aug_train = y_train
for i in range(40):
    x_aug = aug(images = x_train)
    if x_aug_train is not none:
        x_aug_train = np.concatenate([x_aug_train, x_aug], axis = 0)
        y_aug_train = np.concatenate([y_aug_train, y_train], axis = 1)
    else:
        x_aug_train = x_aug

让我们看看一些数据增强的训练图像。

fig, ax = plt.subplots(nrows=2, ncols =5, figsize = (16,16))
for i in range(10):
    index = np.random.randint(x_aug_train.shape[0])
    ax[i//5][i%5].imshow(x_aug_train[index],cmap='gray')

这次使用函数式api创建模型,函数式api是创建模型的另一种方式,它具有更多的灵活性,包括创建更为复杂的模型。

需要定义inputsoutputs

#函数式api模型创建
captcha = input(shape=(50,200,channels))
x = conv2d(32, (5,5),padding='valid',activation='relu')(captcha)
x = maxpooling2d((2,2),padding='same')(x)
x = conv2d(64, (3,3),padding='same',activation='relu')(x)
x = maxpooling2d((2,2),padding='same')(x)
x = conv2d(128, (3,3),padding='same',activation='relu')(x)
maxpool = maxpooling2d((2,2),padding='same')(x)
outputs = []
for i in range(5):
    x = conv2d(256, (3,3),padding='same',activation='relu')(maxpool)
    x = maxpooling2d((2,2),padding='same')(x)
    x = flatten()(x)
    x = dropout(0.5)(x)
    x = batchnormalization()(x)
    x = dense(64, activation='relu')(x)
    x = dropout(0.5)(x)
    x = batchnormalization()(x)
    x = dense(len_symbols , activation='softmax' , name=f'char_{i+1}')(x)
    outputs.append(x)
    
model = model(inputs = captcha , outputs=outputs)
# reducelronplateau更新学习率
reduce_lr = reducelronplateau(patience =3, factor = 0.5,verbose = 1)
model.compile(loss='categorical_crossentropy', optimizer=adam(learning_rate=0.0005), metrics=["accuracy"])
# earlystopping用于提前停止训练的callbacks。具体地,可以达到当训练集上的loss不在减小
earlystopping = earlystopping(monitor ="val_loss",  
                             mode ="min", patience = 10,
                              min_delta = 1e-4,
                             restore_best_weights = true) 

history = model.fit(x_train, [y_train[i] for i in range(5)], batch_size=32, epochs=30, verbose=1, validation_data = (x_val, [y_val[i] for i in range(5)]), callbacks =[earlystopping,reduce_lr])

下面对model进行一个测试和评估。

score = model.evaluate(x_test,[y_test[0], y_test[1], y_test[2], y_test[3], y_test[4]],verbose=1)
metrics = ['loss','char_1_loss', 'char_2_loss', 'char_3_loss', 'char_4_loss', 'char_5_loss', 'char_1_acc', 'char_2_acc', 'char_3_acc', 'char_4_acc', 'char_5_acc']

for i,j in zip(metrics, score):
    print(f'{i}: {j}')

具体输出如下:

11/11 [==============================] - 0s 11ms/step - loss: 0.7246 - char_1_loss: 0.0682 - char_2_loss: 0.1066 - char_3_loss: 0.2730 - char_4_loss: 0.2636 - char_5_loss: 0.0132 - char_1_accuracy: 0.9844 - char_2_accuracy: 0.9657 - char_3_accuracy: 0.9408 - char_4_accuracy: 0.9626 - char_5_accuracy: 0.9938
loss: 0.7246273756027222
char_1_loss: 0.06818050146102905
char_2_loss: 0.10664034634828568
char_3_loss: 0.27299806475639343
char_4_loss: 0.26359987258911133
char_5_loss: 0.013208594173192978
char_1_acc: 0.9844236969947815
char_2_acc: 0.9657320976257324
char_3_acc: 0.940809965133667
char_4_acc: 0.9626168012619019
char_5_acc: 0.9937694668769836

字母1到字母5的精确值都大于

绘制loss和score

metrics_df = pd.dataframe(history.history)

columns = [col for col in metrics_df.columns if 'loss' in col and len(col)>8]

fig = px.line(metrics_df, y = columns)
fig.show()

plt.figure(figsize=(15,8))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right',prop={'size': 10})
plt.show()

# 预测数据
def predict(captcha):
    captcha = np.reshape(captcha , (1, 50,200,channels))
    result = model.predict(captcha)
    result = np.reshape(result ,(5,len_symbols))
    # 取出最大预测中的输出
    label = ''.join([symbols[np.argmax(i)] for i in result])
    return label
    
predict(x_test[2])
# 25277

下面预测所有的数据

actual_pred = []

for i in range(x_test.shape[0]):
    actual = ''.join([symbols[i] for i in (np.argmax(y_test[:, i],axis=1))])
    pred =  predict(x_test[i])
    actual_pred.append((actual, pred))
print(actal_pred[:10])

输出如下:

[('n4b4m', 'n4b4m'), ('42nxy', '42nxy'), ('25257', '25277'), ('cewnm', 'cewnm'), ('w46ep', 'w46ep'), ('cdcb3', 'edcb3'), ('8gf7n', '8gf7n'), ('nny5e', 'nny5e'), ('gm2c2', 'gm2c2'), ('g7fmc', 'g7fmc')]

samecount = 0
diffcount = 0
letterdiff = {i:0 for i in range(5)}
incorrectness = {i:0 for i in range(1,6)}
for real, pred in actual_pred:
    # 预测和输出相同
    if real == pred:
        samecount += 1
    else:
        # 失败
        diffcount += 1
        # 遍历
        incorrectnesspoint = 0
        for i in range(5):
            if real[i] != pred[i]:
                letterdiff[i] += 1
                incorrectnesspoint += 1
        incorrectness[incorrectnesspoint] += 1


x = ['true predicted', 'false predicted']
y = [samecount, diffcount]

fig = go.figure(data=[go.bar(x = x, y = y)])
fig.show()

在预测数据中,一共有287个数据预测正确。

在这里,我们可以看到出现错误到底是哪一个index。

x1 = ["character " + str(x) for x in range(1, 6)]
    
fig = go.figure(data=[go.bar(x = x1, y = list(letterdiff.values()))])
fig.show()

为了计算每个单词的错误数,绘制相关的条形图。

x2 = [str(x) + " incorrect" for x in incorrectness.keys()]
y2 = list(incorrectness.values())

fig = go.figure(data=[go.bar(x = x2, y = y2)])
fig.show()

下面绘制错误的验证码图像,并标准正确和错误的区别。

fig, ax = plt.subplots(nrows = 8, ncols=4,figsize = (16,20))
count = 0
for i, (actual , pred) in enumerate(actual_pred):
    if actual != pred:
        img = x_test[i]
        try:
            ax[count//4][count%4].imshow(img, cmap = 'gray')
            ax[count//4][count%4].title.set_text(pred + ' - ' + actual)
            count += 1
        except:
            pass

到此这篇关于教你使用tensorflow2识别验证码的文章就介绍到这了,更多相关tensorflow2识别验证码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关推荐:

如何使用Python分析网络流量数据并识别潜在的DDoS攻击特征?

Scapy实时捕获SYN包并按源IP分桶计数可识别SYN洪水,需配合时间窗口清理、阈值校准与iptables精准封禁,避免误判正常高并发;关键在每秒统计各src_ip的SYN数量、用快照遍历字典、及时更新last_cleanup时间戳,并验证封禁规则生效。 直接用scapy实时捕获+简单计数就能识别...

GDAL NetCDF CF-1.0 坐标识别失败的根源与解决方案

GDAL(v3.8+)在读取符合CF-1.0规范的NetCDF地理坐标时,可能因x/y坐标变量使用float32(f4)精度不足而误判为非规则网格,导致gdalinfo显示原点坐标(0,0)而非真实投影坐标;升级为float64(f8)或显式添加GeoTransform+spatial_ref属性可...

Python爬虫如何绕过滑块验证码_使用cv2识别缺口位置自动模拟

滑块验证码不能用requests直接绕过,因其依赖前端拖动轨迹、加速度等行为验证,服务端校验token是否由真实拖动生成;cv2识别缺口需三步:用高对比度背景图、干净缺口模板、TM_CCOEFF_NORMED匹配;识别后还需模拟真实拖动行为,否则仍失败。 滑块验证码为什么不能用requests直接绕...

如何正确识别并分类文件系统中的各类条目(文件、目录、符号链接等)

本文介绍使用pathlib替代os.walk()实现对目标路径下所有条目(包括文件、子目录、符号链接等)的准确类型识别与统一哈希统计,解决传统遍历中仅返回文件而忽略其他类型的问题。 本文介绍使用pathlib替代os.walk()实现对目标路径下所有条目(包括文件、子目录、符号链接等)的准确类型识别...

DeepFace人脸验证与识别:预训练模型选用、微调策略及数据增强实践指南

面对千类万人脸数据集(每类10张图像),直接使用deepface内置预训练模型提取特征并构建分类器是高效可靠的选择;微调需谨慎评估计算成本与泛化风险,通常不建议从零训练。 面对千类万人脸数据集(每类10张图像),直接使用deepface内置预训练模型提取特征并构建分类器是高效可靠的选择;微调需谨慎评...