当前位置:网站首页>Keras implements verification code identification
Keras implements verification code identification
2022-07-05 12:26:00 【Dongcheng West que】
![]()
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import os
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
import matplotlib.pyplot as plt
epochs = 5
batch_size=128
img_rows = None
img_cols = None
digits_in_img = 4
x_list = list()
y_list = list()
x_train = list()
y_train = list()
x_val= list()
y_val = list()
x_test = list()
y_test = list()
train_image="Task3/internet-verification-code/train_images"
test_image="Task3/internet-verification-code/test_images"
alphabetdic={"a":10,"b":11,"c":12,"d":13,"e":14,
"f":15,"g":16,"h":17,"i":18,"j":19,"k":20,"l":21,
"m":22,"n":23,"o":24,"p":25,"q":26,"r":27,"s":28,
"t":29,"u":30,"v":31,"w":32,"x":33,"y":34,"z":35}
print("alphabetdic",alphabetdic["a"])
def split_digits_in_img(img_array, x_list, y_list):
for i in range(digits_in_img):
step = img_cols // digits_in_img
x_list.append(img_array[:, i * step:(i + 1) * step] / 255)
if img_filename[i].isdigit():
y_list.append(img_filename[i])
else:
y_list.append(alphabetdic[img_filename[i]])
def split_digits_testimage(img_array, x_test,y_test ):
for j in range(digits_in_img):
step = img_cols // digits_in_img
x_test.append(img_array[:, j* step:(j + 1) * step] / 255)
if test_filename[j].isdigit():
y_test.append(test_filename[j])
else:
y_test.append(alphabetdic[test_filename[j]])
# load all img filenames
img_filenames = os.listdir(train_image)
test_filenames=os.listdir(test_image)
# load images as arrays
for img_filename in img_filenames:
if '.jpeg' not in img_filename:
continue
img = load_img(train_image+'/{0}'.format(img_filename), color_mode='grayscale')
img_array = img_to_array(img)
img_rows, img_cols, _ = img_array.shape
split_digits_in_img(img_array, x_list, y_list)
y_list = keras.utils.to_categorical(y_list, num_classes=36)
for test_filename in test_filenames:
if '.jpeg' not in test_filename:
continue
img = load_img(test_image+'/{0}'.format(test_filename ), color_mode='grayscale')
img_array = img_to_array(img)
img_rows, img_cols, _ = img_array.shape
split_digits_testimage(img_array, x_test,y_test )
y_test= keras.utils.to_categorical(y_test, num_classes=36)
# split data into training set and Validation set
x_train, x_val, y_train, y_val = train_test_split(x_list, y_list)
# model
if os.path.isfile('model/cnn_model.h5'):
# recreate the exact same model purely from the file if exist
model = models.load_model('model/cnn_model.h5')
print('Model loaded from file.')
else:
# otherwise, create a new cnn model
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(img_rows, img_cols // digits_in_img, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(rate=0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(rate=0.5))
model.add(layers.Dense(36, activation='softmax'))
print('New model created.')
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
# start training
model.fit(np.array(x_list), np.array(y_list), batch_size=128, epochs=epochs, verbose=1, validation_data=(np.array(x_val), np.array(y_val)))
# evaluate model
loss, accuracy = model.evaluate(np.array(x_test), np.array(y_test), verbose=0)
print('Test loss:', loss)
print('Test accuracy:', accuracy)
# save the model
model.save('model/cnn_model.h5')边栏推荐
- MySQL log module of InnoDB engine
- 信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
- MySQL transaction
- Why learn harmonyos and how to get started quickly?
- Deep discussion on the decoding of sent protocol
- Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
- [superhard core] is the core technology of redis
- JS for循环 循环次数异常
- Automated test lifecycle
- Halcon template matching actual code (I)
猜你喜欢

Hiengine: comparable to the local cloud native memory database engine

Linux Installation and deployment lamp (apache+mysql+php)

嵌入式软件架构设计-消息交互
你做自动化测试为什么总是失败?

MySQL transaction

Redis highly available sentinel mechanism

Take you hand in hand to develop a service monitoring component

报错ModuleNotFoundError: No module named ‘cv2.aruco‘

Why learn harmonyos and how to get started quickly?

Yum only downloads the RPM package of the software to the specified directory without installing it
随机推荐
ZABBIX monitors mongodb templates and configuration operations
Average lookup length when hash table lookup fails
MySQL trigger
What is the difference between canvas and SVG?
What is digital existence? Digital transformation starts with digital existence
Summary of C language learning problems (VS)
MySQL view
16 channel water lamp experiment based on Proteus (assembly language)
Understanding the architecture type of mobile CPU
MySQL splits strings for conditional queries
MySQL storage engine
Open3d mesh (surface) coloring
Learn the memory management of JVM 02 - memory allocation of JVM
MySQL basic operation -dql
Video networkState 属性
Learn garbage collection 01 of JVM -- garbage collection for the first time and life and death judgment
PIP command reports an error pip is configured with locations that requires tls/ssl problems
ZABBIX customized monitoring disk IO performance
NPM install reports an error
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署