教你使用TensorFlow2识别验证码


Posted in Python onJune 11, 2021

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

数据集来源:https://www.kaggle.com/fournierp/captcha-version-2-images

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

教你使用TensorFlow2识别验证码

在数据集中存在的验证码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()

教你使用TensorFlow2识别验证码

如何提取图像的数据建立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')

教你使用TensorFlow2识别验证码

教你使用TensorFlow2识别验证码

这次使用函数式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])

教你使用TensorFlow2识别验证码

教你使用TensorFlow2识别验证码

教你使用TensorFlow2识别验证码

下面对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()

教你使用TensorFlow2识别验证码

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()

教你使用TensorFlow2识别验证码

# 预测数据
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个数据预测正确。

教你使用TensorFlow2识别验证码

在这里,我们可以看到出现错误到底是哪一个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()

教你使用TensorFlow2识别验证码

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

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()

教你使用TensorFlow2识别验证码

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

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识别验证码

教你使用TensorFlow2识别验证码

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

Python 相关文章推荐
寻找网站后台地址的python脚本
Sep 01 Python
Python实现向QQ群成员自动发邮件的方法
Nov 19 Python
Python里disconnect UDP套接字的方法
Apr 23 Python
Python实战小程序利用matplotlib模块画图代码分享
Dec 09 Python
python用plt画图时,cmp设置方法
Dec 13 Python
对PyQt5基本窗口控件 QMainWindow的使用详解
Jun 19 Python
解决python DataFrame 打印结果不换行问题
Apr 09 Python
Python使用configparser读取ini配置文件
May 25 Python
Python如何在windows环境安装pip及rarfile
Jun 15 Python
python 发送邮件的示例代码(Python2/3都可以直接使用)
Dec 03 Python
Python实现对word文档添加密码去除密码的示例代码
Dec 29 Python
Python极值整数的边界探讨分析
Sep 15 Python
Python使用OpenCV和K-Means聚类对毕业照进行图像分割
Python3中PyQt5简单实现文件打开及保存
Jun 10 #Python
Python selenium的这三种等待方式一定要会!
python实现股票历史数据可视化分析案例
Python如何识别银行卡卡号?
使用python+pygame开发消消乐游戏附完整源码
Python数据可视化之基于pyecharts实现的地理图表的绘制
You might like
PHP 采集程序 常用函数
2008/12/18 PHP
PHP clearstatcache()函数详解
2010/03/02 PHP
强烈声明: 不要使用(include/require)_once
2013/06/06 PHP
php实现的RSS生成类实例
2015/04/23 PHP
php empty 函数判断结果为空但实际值却为非空的原因解析
2018/05/28 PHP
jquery 删除字符串最后一个字符的方法解析
2014/02/11 Javascript
jquery实现表格本地排序的方法
2015/03/11 Javascript
JavaScript的设计模式经典之建造者模式
2016/02/24 Javascript
Bootstrap modal使用及点击外部不消失的解决方法
2016/12/13 Javascript
前端框架学习总结之Angular、React与Vue的比较详解
2017/03/14 Javascript
jQuery的时间datetime控件在AngularJs中的使用实例(分享)
2017/08/17 jQuery
小程序图片长按识别功能的实现方法
2018/08/30 Javascript
微信小程序时间控件picker view使用详解
2018/12/28 Javascript
JavaScript使用小插件实现倒计时的方法讲解
2019/03/11 Javascript
基于node简单实现RSA加解密的方法步骤
2019/03/21 Javascript
layui表格数据重载
2019/07/27 Javascript
layui 上传插件 带预览 非自动上传功能的实例(非常实用)
2019/09/23 Javascript
[04:29]2016国际邀请赛中国区预选赛Ehome战队教练采访
2016/06/27 DOTA
为Python的web框架编写前端模版的教程
2015/04/30 Python
Python简单连接MongoDB数据库的方法
2016/03/15 Python
python爬虫框架talonspider简单介绍
2017/06/09 Python
Python 实现购物商城,含有用户入口和商家入口的示例
2017/09/15 Python
Python使用OpenCV进行标定
2018/05/08 Python
使用python读取csv文件快速插入数据库的实例
2018/06/21 Python
对DataFrame数据中的重复行,利用groupby累加合并的方法详解
2019/01/30 Python
马来西亚综合购物网站:Lazada马来西亚
2018/06/05 全球购物
瑞典廉价机票预订网站:Seat24
2018/06/19 全球购物
ajax是什么及其工作原理
2012/02/08 面试题
Python里面如何拷贝一个对象
2014/02/17 面试题
汽车专业毕业生自荐信
2013/11/03 职场文书
母婴店促销方案
2014/03/05 职场文书
励志演讲稿大全
2014/08/21 职场文书
处级干部反四风个人对照检查材料思想汇报
2014/09/27 职场文书
小学体育组工作总结
2015/08/13 职场文书
2016年暑期教师培训心得体会
2016/01/09 职场文书
《学会生存》读后感3篇
2019/12/09 职场文书