验证码识别-关键源代码

关键源代码-传统方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <math.h>

#include "OCR.h"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
ifstream file("E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test\\imageName.txt"); //输入图像的目录

int img_index = 0;//判断当前是第几张图片
int num_error = 0;

while (!file.eof())
{
char txt_cont[1000];
file.getline(txt_cont, 1000);//读出输入流file中imageName.txt的每一行

char img_file[1000], save_file[1000], save_file1[1000], save_file2[1000], save_file3[1000], save_file4[1000], save_file5[1000];
sprintf(img_file, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test\\%s", txt_cont);//输入图像的目录

sprintf(save_file, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\%s", txt_cont);//保存输入图像
sprintf(save_file1, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\%c[1]%s", txt_cont[0], txt_cont);
sprintf(save_file2, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\%c[2]%s", txt_cont[1], txt_cont);
sprintf(save_file3, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\%c[3]%s", txt_cont[2], txt_cont);
sprintf(save_file4, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\%c[4]%s", txt_cont[3], txt_cont);
sprintf(save_file5, "E:\\vs project\\项目\\验证码识别\\my验证码识别\\my验证码识别\\test识别结果\\识别错误\\%s", txt_cont);

cout << "--------------------------------------------" << endl;
cout << " 识别第" << img_index+1 << "张"<< txt_cont<<"验证码" << endl;
cout << "--------------------------------------------" << endl;

Mat src_original = imread(img_file);
//cout << img_file;
//getchar();

// Mat src_original=imread("1.jpg");
if (src_original.empty())
{
cout << "图片读取错误" << endl;
getchar();
cout << "===============按下任意键,将显示当前识别图片数目及错误率===============" << endl;
break;
}

++img_index;
unsigned int frameCount = 0;//帧数
double time0 = static_cast<double>(getTickCount());//记录起始时间

Mat src = src_original.clone();
imshow("src", src);

Mat src_gray;
cvtColor(src, src_gray, CV_BGR2GRAY);
imshow("src_gray", src_gray);
Mat src_threshold;
threshold(src_gray, src_threshold, 210, 255, CV_THRESH_BINARY_INV);
imshow("src_threshold", src_threshold);

Mat src_contours;
src_threshold.copyTo(src_contours);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src_contours, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);//findContours大写,输入为8位单通道(所以应先二值化再找轮廓,因为查找的都是白色像素)

vector<RotatedRect> rRects(contours.size()); //定义包围点集的最小矩形vector

for (int i = 0; i < contours.size(); ++i)
{
rRects[i] = minAreaRect(Mat(contours[i]));

Point2f rect_points[4];
rRects[i].points(rect_points);
for (int j = 0; j < 4; ++j)
line(src, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 255, 0), 1, 8);
//cout << "候选ROI轮廓面积为" << contourArea(contours[i]) << '\n';
cout << "width " << rRects[i].size.width << " height " << rRects[i].size.height
<< " center " << rRects[i].center << endl;//面积阈值调试用
}
imshow("src", src);//调试用

if (rRects.size() == 4)
{
//对矩形框位置按数字顺序排序
for (int i = 0; i < 4; ++i)
{
for (int j = i + 1; j<4; ++j)
if (rRects[i].center.x > rRects[j].center.x)
swap(rRects[i], rRects[j]);
}
}
else
{
vector<RotatedRect>::const_iterator it = rRects.begin();
//-----除去尺寸较为极端的非字符矩形框-----
while (it != rRects.end())
{
//排除小方格,排除条件依据与数字1的区别
//比it->size.height * it->size.width<8更好
if ((it->size.height<2 && it->size.width <= 8) || (it->size.width<2 && it->size.height <= 8))
{
it = rRects.erase(it);
}
else
{
++it;
}
}
cout << rRects.size();

if (rRects.size() != 4)
{
cout << "字符框提取错误 return 2" << endl;
return 2;
}
else
for (int i = 0; i < 4; ++i)
{
for (int j = i + 1; j<4; ++j)
if (rRects[i].center.x > rRects[j].center.x)
swap(rRects[i], rRects[j]);
}
}
Mat img_ROI[4];
Mat result_resized[4];
int charSize = 28;//分割出的字符归一化尺寸
for (int i = 0; i < 4; ++i)
{
float r = (float)rRects[i].size.height / (float)rRects[i].size.width;
float angle = rRects[i].angle;
if (r < 1)
angle = 90 + angle;
Mat rotmat = getRotationMatrix2D(rRects[i].center, angle, 1);//旋转矩阵

Mat img_rotated;
warpAffine(src_threshold, img_rotated, rotmat, src_threshold.size(), CV_INTER_CUBIC);
imshow("img_rotated", img_rotated);
//因为RotatedRect的宽高因角度不同而不同。
//此处是在旋转后,矩形位置已经摆正了。此时应将长的作为宽,短的作为高(旋转后必须这样调整。如果未旋转,裁剪直接用rotatedrects[i].size即可,见上面getRectSubPix用法)

if (r < 1)
swap(rRects[i].size.width, rRects[i].size.height);

getRectSubPix(img_rotated, rRects[i].size, rRects[i].center, img_ROI[i]);

//----------仿射变换进行预处理-----------
int h = img_ROI[i].rows;//Mat类只有cols和rows,没有height和width成员
int w = img_ROI[i].cols;
Mat transformMat = Mat::eye(2, 3, CV_32F);
int m = max(w, h);
transformMat.at<float>(0, 2) = m / 2 - w / 2;
transformMat.at<float>(1, 2) = m / 2 - h / 2;

Mat warpImage(m, m, img_ROI[i].type());
warpAffine(img_ROI[i], warpImage, transformMat, warpImage.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar(0));

//归一化为charSzie 此处为28*28
//注意;resize过后就不是二值图了
resize(warpImage, result_resized[i], Size(charSize, charSize));

//---------------直接resize会产生变形-------------
//result_resized[i].create(28, 18, CV_8UC1);//inline void Mat::create(int _rows, int _cols, int _type)
//resize(img_ROI[i], result_resized[i], result_resized[i].size(), 0, 0, INTER_CUBIC);
//imshow("result_resized", result_resized[i]);
}

CvSVM svm;
svm.load("svm.xml");

OCR ocr;
char characters[8];
for (int i = 0; i<4; ++i)
{
//此处字符图像已经是gray灰度图了,不需要再cvtColor一下
//将原图像转换为二值图像.threshold的输入为8位
cv::threshold(result_resized[i], result_resized[i], 128, 1, cv::THRESH_BINARY);//???为啥是1,改成255最后细化反而变粗了

//提取特征、预测
Mat f = ocr.features(result_resized[i], 28);//此处应与OCR::OCR(string trainFile)中的数据集选择对应
f.convertTo(f, CV_32FC1);//必须要转换成CV_32FC1格式
characters[i] = svm.predict(f);
}

/* imwrite(save_file1, result_resized[0]);
imwrite(save_file2, result_resized[1]);
imwrite(save_file3, result_resized[2]);
imwrite(save_file4, result_resized[3]);
*/
cout << "验证码为:" << characters[0] << characters[1] << characters[2] << characters[3] << endl << endl;

cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl;
cout << "处理时间为:" << (getTickCount() - time0)/ getTickFrequency()<<"s" << endl;

stringstream ss;
string labNumber;
ss << characters[0] << characters[1] << characters[2] << characters[3] << endl;
ss >> labNumber;

resize(src_original, src_original, Size(660, 140));
putText(src_original, labNumber, Point(src_original.cols / 2, src_original.rows / 5), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2);


int num_same=0;
for (int i = 0;i < 4;++i)
{
if (txt_cont[i] == characters[i])
++num_same;
}

if (num_same == 4)
imwrite(save_file, src_original);
else
{
imwrite(save_file5, src_original);
++num_error;
}
}//这个是前面批处理操作 while (!file.eof()) {} 的括号

cout << "共识别了" << img_index << "张验证码――――" << num_error <<"张识别错误"<< endl;
float error = float(num_error) / img_index;
cout << "错误率为" << error << endl;
waitKey();

}

关键源代码-CNN方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# coding: utf-8

# In[1]:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
'''验证码识别-6个3x3conv+2个FC层
'''

import tensorflow as tf
import numpy as np
import os
# os.environ["CUDA_VISIBLE_DEVICES"]="-1" # 禁止GPU
# os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"]="0" # 使用对应序号的GPU
# config = tf.ConfigProto()
# config.gpu_options.per_process_gpu_memory_fraction = 0.9 # 占用GPU90%的显存

# 【1】载入数据集,设置训练参数、网络参数、placeholder=============================
# mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

# 训练参数
batch_size = 16
# n_batch = mnist.train.num_examples // batch_size + 1
n_batch = 3000 // batch_size + 1
epoches = 5000
diaplay_steps = 1
learning_rate = 1e-4 # 另,也可将学习率定义为变量?
dropout = 0.8 # Dropout, probability to keep units

# 网络参数(数据集参数)
num_train = 142326 # 训练集图片个数
num_val = 3560
num_test = 1196
n_classes = 36

# placeholder: tf graph的输入x, y、keep_prob
# 要有名字,因为后续保存模型再使用,需要读取出来重新给占位符赋值
x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x')
# 标签数据不是独热编码,如果标签数据是独热编码shape=[batch_size,ClassesNumber]
y = tf.placeholder(tf.int32, [None, n_classes])
# 要有名字,保存模型之后再使用需要把它调用出来使用的
keep_prob = tf.placeholder(tf.float32, name='keep_prob')

# 读取文件
# tfrecords_path所在目录位置
tfrecord_train_path = "./data_segment/tfrecords-28/captcha_train.tfrecords"
tfrecord_val_path = "./data_segment/tfrecords-28/captcha_val.tfrecords"
tfrecord_test_path = "./data_segment/tfrecords-28/captcha_test.tfrecords"


# In[5]:


# 【2】创建网络
# 变量封装:各层weights、biases
weights = {
'w_conv1': tf.Variable(tf.truncated_normal([3, 3, 1, 32], stddev=0.1)),
'w_conv2': tf.Variable(tf.truncated_normal([3, 3, 32, 32], stddev=0.1)),
'w_conv3': tf.Variable(tf.truncated_normal([3, 3, 32, 32], stddev=0.1)),

'w_conv4': tf.Variable(tf.truncated_normal([3, 3, 32, 64], stddev=0.1)),
'w_conv5': tf.Variable(tf.truncated_normal([3, 3, 64, 64], stddev=0.1)),
'w_conv6': tf.Variable(tf.truncated_normal([3, 3, 64, 64], stddev=0.1)),

# 全连接层有1024个神经元
'w_fc1': tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1)),
'w_fc2': tf.Variable(tf.truncated_normal([1024, n_classes], stddev=0.1))
}

biases = {
'b_conv1': tf.Variable(tf.truncated_normal([32], stddev=0.1)),
'b_conv2': tf.Variable(tf.truncated_normal([32], stddev=0.1)),
'b_conv3': tf.Variable(tf.truncated_normal([32], stddev=0.1)),

'b_conv4': tf.Variable(tf.truncated_normal([64], stddev=0.1)),
'b_conv5': tf.Variable(tf.truncated_normal([64], stddev=0.1)),
'b_conv6': tf.Variable(tf.truncated_normal([64], stddev=0.1)),

'b_fc1': tf.Variable(tf.truncated_normal([1024], stddev=0.1)), # 1024个节点
'b_fc2': tf.Variable(tf.truncated_normal([n_classes], stddev=0.1))
}

# 函数封装:卷积、pooling、网络结构


def conv2d(x, weights, biases, strides=1):
x = tf.nn.conv2d(
x,
weights,
strides=[
1,
strides,
strides,
1],
padding='SAME') + biases
# 此处加上b也可以这样写,x = tf.nn.bias_add(x, biases)
return tf.nn.relu(x)


def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')


def conv_net(x, weights, biases, keep_prob):
# 改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]`
x = tf.reshape(x, shape=[-1, 28, 28, 1]) # -1表示该值不定

conv1 = conv2d(x, weights['w_conv1'], biases['b_conv1'])
conv2 = conv2d(conv1, weights['w_conv2'], biases['b_conv2'])
conv3 = conv2d(conv2, weights['w_conv3'], biases['b_conv3'])
conv3 = maxpool2d(conv3)

conv4 = conv2d(conv3, weights['w_conv4'], biases['b_conv4'])
conv5 = conv2d(conv4, weights['w_conv5'], biases['b_conv5'])
conv6 = conv2d(conv5, weights['w_conv6'], biases['b_conv6'])
conv6 = maxpool2d(conv6)

# .get_shape().as_list()返回保存tensor尺寸的一个元组
fc1 = tf.reshape(
conv6, shape=[-1, weights['w_fc1'].get_shape().as_list()[0]])
fc1 = tf.matmul(fc1, weights['w_fc1']) + biases['b_fc1']
fc1 = tf.nn.relu(fc1)
fc1 = tf.nn.dropout(fc1, keep_prob)

fc2 = tf.matmul(fc1, weights['w_fc2']) + biases['b_fc2']
return fc2


# In[6]:


def read_tfrecord(filename):
filename_queue = tf.train.string_input_producer([filename]) # 生成一个queue队列

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # 返回文件名和文件
features = tf.parse_single_example(
serialized_example, features={
'label': tf.FixedLenFeature(
[], tf.int64), 'img_raw': tf.FixedLenFeature(
[], tf.string), }) # 将image数据和label取出来

img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [28, 28, 1])
img = tf.cast(img, tf.float32) * (1. / 255) # 图像数据归一化
# img = tf.cast(img, tf.float32)
label = tf.cast(features['label'], tf.int32) # 在流中抛出label张量
return img, label

# 取出batch的数据


def get_tfrecord_batch(batch_size, isTrain=True, isVal=True):
if isTrain:
tfrecords_path = tfrecord_train_path
elif isVal:
tfrecords_path = tfrecord_val_path
else:
tfrecords_path = tfrecord_test_path
img, label = read_tfrecord(tfrecords_path)
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=batch_size,
num_threads=256, # 线程数
capacity=600, # 即每次取出capacity个样本,batch再从中选
min_after_dequeue=200) # 即队列中至少min_after_dequeue个样本,该值小于capacity的值
return img_batch, label_batch


# In[7]:


# 【3】模型训练与评估============================================
# 模型loss、optimizer选择
fc2 = conv_net(x, weights, biases, keep_prob)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
labels=y, logits=fc2), name='loss')
optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
# optimizer = tf.train.AdadeltaOptimizer(1e-3)
# optimizer = tf.train.GradientDescentOptimizer(0.1)
# optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, name='Adam')
train_step = optimizer.minimize(loss, name='train_step')

# 模型评估
prediction = tf.nn.softmax(fc2, name='prediction')
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')


# In[8]:


# 【4】开始训练,print结果
# 导入数据
with tf.device('/cpu:0'):
train_x_batch, train_y_batch = get_tfrecord_batch(
batch_size, isTrain=True, isVal=False)
# tfrecords中的label不是one-hot格式,需要转一下
train_y_batch = tf.one_hot(train_y_batch, n_classes)

val_x_batch, val_y_batch = get_tfrecord_batch(
batch_size, isTrain=False, isVal=True)
val_y_batch = tf.one_hot(val_y_batch, n_classes)

# 整个train和val数据集数据=====================
train_x, train_y = get_tfrecord_batch(
num_train, isTrain=True, isVal=False) # 这里去的batch数量>capacity,但并未有错误
train_y = tf.one_hot(train_y, n_classes)

val_x, val_y = get_tfrecord_batch(num_val, isTrain=False, isVal=True)
val_y = tf.one_hot(val_y, n_classes)

test_x, test_y = get_tfrecord_batch(
num_test, isTrain=False, isVal=False) # 这里1196是整个测试图片的个数
test_y = tf.one_hot(test_y, n_classes)


# In[10]:


saver = tf.train.Saver(max_to_keep=3) # 只保存精度最好的那3个

max_acc = 0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

saver.restore(
sess,
'./checkpoints/3-10-3,3,3,3/3-10-6layers_tiny_vgg.ckpt-524')

coord = tf.train.Coordinator() # 生成queue队列好像是(读取tfrecord用的)
threads = tf.train.start_queue_runners(coord=coord)

print('Start training\n')
img_test, label_test = sess.run([test_x, test_y])

for epoch in range(1, epoches + 1):
avg_loss = 0.
for _ in range(1, n_batch + 1):
img_batch, label_batch = sess.run([train_x_batch, train_y_batch])

sess.run(
train_step,
feed_dict={
x: img_batch,
y: label_batch,
keep_prob: dropout})
avg_loss += sess.run(loss,
feed_dict={x: img_batch,
y: label_batch,
keep_prob: 1.0}) / n_batch

if epoch % diaplay_steps == 0 or epoch == 1:
img_batch, label_batch = sess.run([train_x_batch, train_y_batch])
img_val, label_val = sess.run([val_x, val_y])
train_acc = sess.run(
accuracy,
feed_dict={
x: img_batch,
y: label_batch,
keep_prob: 1.0}) # 求accuracy时,keep_prob应为1(此时相当于检测阶段)
val_acc = sess.run(accuracy, feed_dict={x: img_val,
y: label_val,
keep_prob: 1.0})
# 测试集准确率==============================================
test_acc = sess.run(
accuracy,
feed_dict={
x: img_test,
y: label_test,
keep_prob: 1.0})

print(
"Epoch: {:d}/{:d}, avg_loss: {:.6f}, (batch)train_acc: {:.4f}, val_acc: {:.4f}, test_acc:{:.4f}".format(
epoch,
epoches,
avg_loss,
train_acc,
val_acc,
test_acc))

if val_acc > max_acc:
max_acc = val_acc
saver.save(
sess,
'./checkpoints/3-10-3,3,3,3/3-10-6layers_tiny_vgg.ckpt',
global_step=epoch)

print("training finish!")

coord.request_stop()
coord.join(threads)