程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[source code] python+tensorflow to realize verification code recognition

編輯:Python

Data set acquisition

link :https://pan.baidu.com/s/1aLFV-QovCeig4bGaS8U_8w , Extraction code :5u9h

Image source code download

#-*- coding:utf-8 -*-
from urllib.request import urlretrieve
import time, random, os
class Discuz():
def __init__(self):
# Discuz Verification code generates picture address 
self.url = 'http://cuijiahua.com/tutrial/discuz/index.php?label=' # Address failure , Please replace the !!!
def random_captcha_text(self, captcha_size = 4):
""" Verification codes generally ignore case ; Length of verification code 4 Characters Parameters: captcha_size: Length of verification code Returns: captcha_text: Verification code string """
number = ['0','1','2','3','4','5','6','7','8','9']
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
char_set = number + alphabet
captcha_text = []
for i in range(captcha_size):
c = random.choice(char_set)
captcha_text.append(c)
captcha_text = ''.join(captcha_text)
return captcha_text
def download_discuz(self, nums = 50000):
""" Download the captcha image Parameters: nums: Number of verification code pictures downloaded """
dirname = './pic'
if dirname not in os.listdir():
os.mkdir(dirname)
for i in range(nums):
label = self.random_captcha_text()
print(' The first %d A picture :%s download ' % (i + 1,label))
urlretrieve(url = self.url + label, filename = dirname + '/' + label + '.jpg')
# Please add at least 200ms Time delay , Avoid putting too much pressure on my server , If it is found that it affects the normal operation of the server , I will turn off this function .
# Hello, I'm fine , Hello, everyone, it's really good !
time.sleep(0.1)
print(' Congratulations on the completion of the image download !')
if __name__ == '__main__':
dz = Discuz()
dz.download_discuz()

Data training test

#-*- coding:utf-8 -*-
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import os, random, cv2
class Discuz():
def __init__(self):
# Dataset path 
self.data_path = './pic/'
# Write to the specified disk path 
self.log_dir = './logs/'
# Dataset image size 
self.width = 30
self.heigth = 100
# Maximum number of iterations 
self.max_steps = 100
# Reading data sets 
self.test_imgs, self.test_labels, self.train_imgs, self.train_labels = self.get_imgs()
# Training set size 
self.train_size = len(self.train_imgs)
# Test set size 
self.test_size = len(self.test_imgs)
# Every time to obtain batch_size Current training set pointer of size 
self.train_ptr = 0
# Every time to get batch_size Current test set pointer of size 
self.test_ptr = 0
# Character dictionary size :0-9 a-z A-Z _( If the verification code is less than 4, use _ A filling ) altogether 63 Characters 
self.char_set_len = 63
# The longest length of the verification code is 4
self.max_captcha = 4
# input data X Place holder 
self.X = tf.placeholder(tf.float32, [None, self.heigth*self.width])
# input data Y Place holder 
self.Y = tf.placeholder(tf.float32, [None, self.char_set_len*self.max_captcha])
# keepout Place holder 
self.keep_prob = tf.placeholder(tf.float32)
def get_imgs(self, rate = 0.2):
# Read the picture 
imgs = os.listdir(self.data_path)
# Disorganize pictures 
random.shuffle(imgs)
# The total number of data sets 
imgs_num = len(imgs)
# Calculate the number of test sets in proportion 
test_num = int(imgs_num * rate / (1 + rate))
# Test set 
test_imgs = imgs[:test_num]
# Get the test set label according to the file name 
test_labels = list(map(lambda x: x.split('.')[0], test_imgs))
# Training set 
train_imgs = imgs[test_num:]
# Get the training set label according to the file name 
train_labels = list(map(lambda x: x.split('.')[0], train_imgs))
return test_imgs, test_labels, train_imgs, train_labels
def get_next_batch(self, train_flag=True, batch_size=100):
# Get data from the training set 
if train_flag == True:
if (batch_size + self.train_ptr) < self.train_size:
trains = self.train_imgs[self.train_ptr:(self.train_ptr + batch_size)]
labels = self.train_labels[self.train_ptr:(self.train_ptr + batch_size)]
self.train_ptr += batch_size
else:
new_ptr = (self.train_ptr + batch_size) % self.train_size
trains = self.train_imgs[self.train_ptr:] + self.train_imgs[:new_ptr]
labels = self.train_labels[self.train_ptr:] + self.train_labels[:new_ptr]
self.train_ptr = new_ptr
batch_x = np.zeros([batch_size, self.heigth*self.width])
batch_y = np.zeros([batch_size, self.max_captcha*self.char_set_len])
for index, train in enumerate(trains):
img = np.mean(cv2.imread(self.data_path + train), -1)
# Reduce the multidimensional dimension 1 dimension 
batch_x[index,:] = img.flatten() / 255
for index, label in enumerate(labels):
batch_y[index,:] = self.text2vec(label)
# Get data from the test set 
else:
if (batch_size + self.test_ptr) < self.test_size:
tests = self.test_imgs[self.test_ptr:(self.test_ptr + batch_size)]
labels = self.test_labels[self.test_ptr:(self.test_ptr + batch_size)]
self.test_ptr += batch_size
else:
new_ptr = (self.test_ptr + batch_size) % self.test_size
tests = self.test_imgs[self.test_ptr:] + self.test_imgs[:new_ptr]
labels = self.test_labels[self.test_ptr:] + self.test_labels[:new_ptr]
self.test_ptr = new_ptr
batch_x = np.zeros([batch_size, self.heigth*self.width])
batch_y = np.zeros([batch_size, self.max_captcha*self.char_set_len])
for index, test in enumerate(tests):
img = np.mean(cv2.imread(self.data_path + test), -1)
# Reduce the multidimensional dimension 1 dimension 
batch_x[index,:] = img.flatten() / 255
for index, label in enumerate(labels):
batch_y[index,:] = self.text2vec(label)
return batch_x, batch_y
def text2vec(self, text):
""" The amount of text turning Parameters: text: Text Returns: vector: vector """
if len(text) > 4:
raise ValueError(' The verification code is the longest 4 Characters ')
vector = np.zeros(4 * self.char_set_len)
def char2pos(c):
if c =='_':
k = 62
return k
k = ord(c) - 48
if k > 9:
k = ord(c) - 55
if k > 35:
k = ord(c) - 61
if k > 61:
raise ValueError('No Map')
return k
for i, c in enumerate(text):
idx = i * self.char_set_len + char2pos(c)
vector[idx] = 1
return vector
def vec2text(self, vec):
""" Vector to text Parameters: vec: vector Returns: Text """
char_pos = vec.nonzero()[0]
text = []
for i, c in enumerate(char_pos):
char_at_pos = i #c/63
char_idx = c % self.char_set_len
if char_idx < 10:
char_code = char_idx + ord('0')
elif char_idx < 36:
char_code = char_idx - 10 + ord('A')
elif char_idx < 62:
char_code = char_idx - 36 + ord('a')
elif char_idx == 62:
char_code = ord('_')
else:
raise ValueError('error')
text.append(chr(char_code))
return "".join(text)
def crack_captcha_cnn(self, w_alpha=0.01, b_alpha=0.1):
x = tf.reshape(self.X, shape=[-1, self.heigth, self.width, 1])
# Convolution filter: One Tensor. The data dimension is four-dimensional [filter_height, filter_width, in_channels, out_channels]
# The specific meaning is [ The height of the convolution kernel , The width of the convolution kernel , Number of image channels , Number of convolution kernels ]
w_c1 = tf.Variable(w_alpha*tf.random_normal([3, 3, 1, 32]))
b_c1 = tf.Variable(b_alpha*tf.random_normal([32]))
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1))
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
w_c2 = tf.Variable(w_alpha*tf.random_normal([3, 3, 32, 64]))
b_c2 = tf.Variable(b_alpha*tf.random_normal([64]))
conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2))
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
w_c3 = tf.Variable(w_alpha*tf.random_normal([3, 3, 64, 64]))
b_c3 = tf.Variable(b_alpha*tf.random_normal([64]))
conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, w_c3, strides=[1, 1, 1, 1], padding='SAME'), b_c3))
conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
w_d = tf.Variable(w_alpha*tf.random_normal([4*13*64, 1024]))
b_d = tf.Variable(b_alpha*tf.random_normal([1024]))
dense = tf.reshape(conv3, [-1, w_d.get_shape().as_list()[0]])
dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
dense = tf.nn.dropout(dense, self.keep_prob)
w_out = tf.Variable(w_alpha*tf.random_normal([1024, self.max_captcha*self.char_set_len]))
b_out = tf.Variable(b_alpha*tf.random_normal([self.max_captcha*self.char_set_len]))
out = tf.add(tf.matmul(dense, w_out), b_out)
return out
def train_crack_captcha_cnn(self):
output = self.crack_captcha_cnn()
# Create a loss function 
diff = tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=self.Y)
loss = tf.reduce_mean(diff)
tf.summary.scalar('loss', loss)
# Use AdamOptimizer Optimizer training model , Minimize cross entropy loss 
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
# Computational accuracy 
y = tf.reshape(output, [-1, self.max_captcha, self.char_set_len])
y_ = tf.reshape(self.Y, [-1, self.max_captcha, self.char_set_len])
correct_pred = tf.equal(tf.argmax(y, 2), tf.argmax(y_, 2))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
with tf.Session() as sess:
# Write to the specified disk path 
train_writer = tf.summary.FileWriter(self.log_dir + '/train', sess.graph)
test_writer = tf.summary.FileWriter(self.log_dir + '/test')
sess.run(tf.global_variables_initializer())
# Traverse self.max_steps Time 
for i in range(self.max_steps):
# iteration 500 Time , Mess up the data set 
if i % 20 == 0:
self.test_imgs, self.test_labels, self.train_imgs, self.train_labels = self.get_imgs()
# Every time 10 Time , Use test sets , Test the accuracy 
if i % 10 == 0:
batch_x_test, batch_y_test = self.get_next_batch(False, 100)
summary, acc = sess.run([merged, accuracy], feed_dict={
self.X: batch_x_test, self.Y: batch_y_test, self.keep_prob: 1})
print(' Iteration number %d Time accuracy:%f' % (i+1, acc))
test_writer.add_summary(summary, i)
# If the accuracy is greater than 85%, Save the model and exit .
if acc > 0.85:
train_writer.close()
test_writer.close()
break
# Training all the time 
else:
batch_x, batch_y = self.get_next_batch(True, 100)
loss_value, _ = sess.run([loss, optimizer], feed_dict={
self.X: batch_x, self.Y: batch_y, self.keep_prob: 1})
print(' Iteration number %d Time loss:%f' % (i+1, loss_value))
curve = sess.run(merged, feed_dict={
self.X: batch_x_test, self.Y: batch_y_test, self.keep_prob: 1})
train_writer.add_summary(curve, i)
train_writer.close()
test_writer.close()
if __name__ == '__main__':
dz = Discuz()
dz.train_crack_captcha_cnn()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved