def wrap_gen_captcha_text_and_image(): ''' 获取一张图,判断其是否符合(60,160,3)的规格''' while True: text, image = gen_captcha_text_and_image() if image.shape == (60, 160, 3): #此部分应该与开头部分图片宽高吻合 #if image.shape == (60, 200, 3):#此部分应该与开头部分图片宽高吻合 return text, image
def wrap_gen_captcha_text_and_image(): ''' 获取一张图,判断其是否符合(60,160,3)的规格''' while True: text, image = gen_captcha_text_and_image() if image.shape == (IMAGE_HEIGHT, IMAGE_WIDTH, 3): #此部分应该与开头部分图片宽高吻合 return text, image
def test_times(n): output = crack_captcha_cnn() all = n saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('./')) right_times = 0 for i in range(n): raw_text, image = gen_captcha_text_and_image() image = convert2gray(image) #生成一张新图 image = image.flatten() / 255 # 将图片一维化 predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [image], keep_prob: 1}) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 predict_text = vec2text(vector) print("正确: {} 预测: {},tolower:{},{}".format( raw_text, predict_text, raw_text.lower(), predict_text.lower())) if raw_text.lower() == predict_text.lower(): right_times += 1 print("right times:", right_times, all) right_prop = float(right_times) / float(all) print("正确率:", right_prop)
def make_prediction(): text, image = gen_captcha_text_and_image() image = convert2gray(image) image = image.flatten() / 255 predict_text = crack_captcha(image) print("正确: {} 预测: {}".format(text, predict_text)) '''
def crack_captcha(): #tf.reset_default_graph()#avoid variable change output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('saveyzm')) for intt in range(10): text1, image = gen_captcha_text_and_image() plt.imshow(image) plt.show() image = convert2gray(image) captcha_image = image.flatten() / 255 predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={ X: [captcha_image], keep_prob: 1 }) text = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in text: vector[i * CHAR_SET_LEN + n] = 1 i += 1 print("正确: {} 预测: {}".format(text1, vec2text(vector)))
def wrap_gen_captcha_text_and_image(): while True: text, image = gen_captcha_text_and_image(IMAGE_WIDTH, IMAGE_HEIGHT, CHAR_SET, CAPTCHA_TEXT_LEN, FONT_SIZE) if image.shape == (IMAGE_HEIGHT, IMAGE_WIDTH, 3): return text, image
def crack_captcha_test(): session, predict = restore_latest_checkpoint() failed_count = 0 for i in range(100): text, image = gen_captcha_text_and_image(IMAGE_WIDTH, IMAGE_HEIGHT, CHAR_SET, CAPTCHA_TEXT_LEN, FONT_SIZE) image = convert2gray(image) image = image.flatten() / 255 predict_text = crack_captcha(session, predict, image) if text != predict_text: failed_count += 1 print("expected: {} predicted: {} failed_count:{}".format( text, predict_text, failed_count))
def get_next_batch(batch_size=128): batch_x = np.zeros([batch_size, IMAGE_HEIGHT*IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA*CHAR_SET_LEN]) for i in range(batch_size): #获取图片,并灰度转换 text, image = gen_captcha_text_and_image() image = convert2gray(image) # flatten 图片一维化 以及对应的文字内容也一维化,形成一个128行每行一个图片及对应文本 batch_x[i,:] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i,:] = text2vec(text) return batch_x, batch_y
def get_next_batch(batch_size=128): """ 生成一个训练batch """ batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) for i in range(batch_size): text, image = gen_captcha_text_and_image() image = convert2gray(image) batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y
def crack_captcha(): output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('.')) print(tf.train.latest_checkpoint('.')) k = 0 while True: text, image = gen_captcha_text_and_image() image = convert2gray(image) image = image.flatten() / 255 predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [image], keep_prob: 1}) texts = text_list[0].tolist() vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) i = 0 for n in texts: vector[i * CHAR_SET_LEN + n] = 1 i += 1 predict_text = vec2text(vector) print("text: {} predict_text: {}".format(text, predict_text)) k += 1
def wrap_gen_captcha_text_and_image(): while True: text, _image = gen_captcha_text_and_image() if _image.shape == (IMAGE_HEIGHT, IMAGE_WIDTH, 3): return text, _image
def wrap_gen_captcha_text_and_image(): while True: text, image = gen_captcha_text_and_image() #logging.debug("image,shape:%s",str(image.shape)) if image.shape == (60, 160, 3): return text, image
def wrap_gen_captcha_text_and_image(): ''' 获取一张图,判断其是否符合(60,160,3)的规格''' while True: text, image = gen_captcha_text_and_image() if image.shape == (60, 160, 3): return text, image
def wrap_gen_captcha_text_and_image(): while True: text, image = gen_captcha_text_and_image() if image.shape == (60, 160, 3): return text, image
def wrap_gen_captcha_text_and_image(): while True: text, image = gen_captcha_text_and_image() if image.shape == (60, 160, 3): return text, image
def test_crack_captcha_cnn(): text, image = gen_captcha_text_and_image() image = convert2gray(image) #生成一张新图 image = image.flatten() / 255 # 将图片一维化 predict_text = crack_captcha(image) #导入模型识别 print("正确: {} 预测: {}".format(text, predict_text))
def cnn_random(): text, image = gen_captcha_text_and_image() image = Image.fromarray(image) image.save(captcha_path) return redirect(url_for('cnn.cnn_index'))
def wrap_gen_captcha_text_and_image(self): while True: text, image = gen_captcha_text_and_image() if image.shape == (self.IMAGE_HEIGHT, self.IMAGE_WIDTH, self.IMAGE_CHANNEL): return text, image
# 验证码最长N字符; 如果验证码长度小于N,用'_'补齐 MAX_CAPTCHA = 6 MIN_CAPTCHA = 6 print("Max number of label:", MAX_CAPTCHA) # 图像大小 IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 BATCH_SIZE = 128 EPOCH_SIZE = 50000 EVAL_PER_STEPS = 100 text, image = gen_captcha_text_and_image(MAX_CAPTCHA, MIN_CAPTCHA) print("verification code iamge channel:", image.shape) # (60, 160, 3) """ cnn在图像大小是2的倍数时性能最高, 如果你用的图像大小不是2的倍数,可以在图像边缘补无用像素。 np.pad(image,((2,3),(2,2)), 'constant', constant_values=(255,)) # 在图像上补2行,下补3行,左补2行,右补2行 """ # 文本转向量 char_set = number + ['_'] # 如果验证码长度小于N, '_'用来补齐 CHAR_SET_LEN = len(char_set) # 向量转回文本 def vec2text(vec): char_pos = vec.nonzero()[0] text = []
# -*- coding: utf-8 -*- """ Created on Tue Jan 23 09:29:46 2018 @author: Administrator """ import os import numpy as np import tensorflow as tf from gen_captcha import gen_captcha_text_and_image from gen_captcha import CHAR_SET_LEN, IMAGE_HEIGHT, IMAGE_WIDTH, MAX_CAPTCHA import matplotlib.pyplot as plt from cnn import crack_captcha_cnn, X, keep_prob from data_iter import convert2gray, vec2text _, random_im = gen_captcha_text_and_image() def crack_captcha(captcha_image=random_im): if captcha_image.shape != (IMAGE_HEIGHT, IMAGE_WIDTH, 3): captcha_image = captcha_image.resize((IMAGE_HEIGHT, IMAGE_WIDTH, 3)) plt.imshow(captcha_image) plt.show() # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess=sess,
# coding:utf-8 ''' @author = super_fazai @File : TensorFlow_cnn_批量生成验证码并用cnn训练.py @connect : [email protected] ''' from gen_captcha import gen_captcha_text_and_image, number, alphabet, ALPHABET import numpy as np import tensorflow as tf text, image = gen_captcha_text_and_image() # 先生成验证码和文字测试模块是否完全 print("验证码图像channel:", image.shape) # (60, 160, 3) # 图像大小 IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 MAX_CAPTCHA = len(text) print("验证码文本最长字符数", MAX_CAPTCHA) # 验证码最长4字符; 我全部固定为4,可以不固定. 如果验证码长度小于4,用'_'补齐 def convert2gray(img): ''' # 把彩色图像转为灰度图像(色彩对识别验证码没有什么用) :param img: :return: ''' if len(img.shape) > 2: gray = np.mean(img, -1) # 上面的转法较快,正规转法如下 # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
#coding:utf-8 from gen_captcha import gen_captcha_text_and_image from gen_captcha import number from gen_captcha import alphabet from gen_captcha import ALPHABET import numpy as np import tensorflow as tf text, image = gen_captcha_text_and_image() #先生成验证码和文字测试模块是否完全 print("验证码图像channel:", image.shape) # (60, 160, 3) # 图像大小 IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 MAX_CAPTCHA = len(text) print("验证码文本最长字符数", MAX_CAPTCHA) # 验证码最长4字符; 我全部固定为4,可以不固定. 如果验证码长度小于4,用'_'补齐 # 把彩色图像转为灰度图像(色彩对识别验证码没有什么用) def convert2gray(img): if len(img.shape) > 2: gray = np.mean(img, -1) # 上面的转法较快,正规转法如下 # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2] # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return gray else: return img """ cnn在图像大小是2的倍数时性能最高, 如果你用的图像大小不是2的倍数,可以在图像边缘补无用像素。 np.pad(image【,((2,3),(2,2)), 'constant', constant_values=(255,)) # 在图像上补2行,下补3行,左补2行,右补2行
def train_crack_captcha_cnn(): import time start_time = time.time() output = crack_captcha_cnn() # loss #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, Y)) #sigmoid函数输出的是概率 # 定义损失函数,依旧使用交叉熵 同时定义优化器 learning rate = 1e-4 # 函数说明:sigmoid损失函数计算 # 参数1:labels # 类型和logits一致 # 参数2:logits # 类型 `float32` or `float64`. loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y)) # 最后一层用来分类的softmax和sigmoid有什么不同? # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰 optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) max_idx_p = tf.argmax(predict, 2) max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) # 定义评测准确率 correct_pred = tf.equal(max_idx_p, max_idx_l) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) #开始训练 isTrain = False #来区分训练阶段和测试阶段,True 表示训练,False表示测试 train_steps = 1000 #表示训练的次数,例子中使用100 checkpoint_steps = 10 #表示训练多少次保存一下checkpoints,例子中使用50 checkpoint_dir = '.\\' #表示checkpoints文件的保存路径,例子中使用当前路径F:\\py3workspace\\train_captcha\\ isAgainTrain = True #表示是否恢复保存的模型继续训练 if isTrain: saver = tf.train.Saver(max_to_keep=1) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #初始化所有变量 start_step = 0 if isAgainTrain: # 检查是否有checkpoint checkpoint = tf.train.get_checkpoint_state(checkpoint_dir) if checkpoint and checkpoint.model_checkpoint_path: #saver.restore(sess, checkpoint.model_checkpoint_path) #print(checkpoint.model_checkpoint_path.rsplit('-',1)[1]) start_step = int( checkpoint.model_checkpoint_path.rsplit('-', 1)[1]) print(start_step) saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir)) # params=tf.trainable_variables() # print("Trainable variables:------------------------") # for idx, v in enumerate(params): # print(" param {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name)) # #读取图片 # text, image = gen_captcha_text_and_image() # image = convert2gray(image) # image = image.flatten() / 255 # sess=tf.Session() # sess.run(tf.global_variables_initializer()) # #提取最后一个全连接层的参数 W和b # W=sess.run(params[8]) # b=sess.run(params[9]) # #print(W,b) # predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) # #提取第二个全连接层的输出值作为特征 # fea=sess.run(predict,feed_dict={X:image}) # print(fea) # quit() f = open(checkpoint_dir + 'acc.txt', 'w') for step in range(start_step, train_steps): #step += 50 batch_x, batch_y = get_next_batch(64) _, loss_ = sess.run([optimizer, loss], feed_dict={ X: batch_x, Y: batch_y, keep_prob: 0.75 }) print( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), step, loss_) f.write(str(step) + ', val_acc: ' + str(loss_) + '\n') # 每100 step计算一次准确率 if step % checkpoint_steps == 0 and step > 0: batch_x_test, batch_y_test = get_next_batch(100) acc = sess.run(accuracy, feed_dict={ X: batch_x_test, Y: batch_y_test, keep_prob: 1. }) print( u'***************************************************************第%s次的准确率为%s' % (step, acc)) #saver.save(sess, checkpoint_dir + 'model.ckpt', global_step=step) saver.save(sess, checkpoint_dir + "crack_capcha.model", global_step=step) # 如果准确率大于50%,保存模型,完成训练 if acc > 0.5: ##我这里设了0.9,设得越大训练要花的时间越长,如果设得过于接近1,很难达到。如果使用cpu,花的时间很长,cpu占用很高电脑发烫。 #saver.save(sess, checkpoint_dir +"crack_capcha.model", global_step=step) print(time.time() - start_time) break #summary_writer = tf.summary.FileWriter("F://py3workspace//train_captcha//log", sess.graph) #summary_writer.close() else: #output = crack_captcha_cnn() saver = tf.train.Saver(max_to_keep=1) sess = tf.Session() #latest_checkpoint自动获取最后一次保存的模型 saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir)) #batch_x_test, batch_y_test = get_next_batch(100) #_, loss_ = sess.run([optimizer, loss], feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.}) #print(loss_) #quit() n = 10 while (n): text, image = gen_captcha_text_and_image() image = convert2gray(image) image = image.flatten() / 255 predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [image], keep_prob: 1}) #print(text_list) predict_text = text_list[0].tolist() #print(predict_text) vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) #print(vector) i = 0 for t in predict_text: vector[i * 5 + t] = 1 i += 1 # break #print(vector) print("正确: {} 预测: {}".format(text, vec2text(vector))) n = n - 1 #break sess.close()
def wrap_gen_captcha_text_and_image(): ''' 获取一张图,判断其是否符合(60,160,3)的规格''' while True: text, image = gen_captcha_text_and_image() if image.shape == (60, 160, 3):#此部分应该与开头部分图片宽高吻合 return text, image
#!/usr/bin/env python3 # coding=utf-8 import os from gen_captcha import gen_captcha_text_and_image from gen_captcha import number from gen_captcha import alphabet from gen_captcha import ALPHABET import numpy as np import tensorflow as tf text, image = gen_captcha_text_and_image() print("验证码图像channel:", image.shape) # (60, 160, 3) # 图像大小 IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 MAX_CAPTCHA = len(text) print("验证码文本最长字符数", MAX_CAPTCHA) # 验证码最长4字符; 我全部固定为4,可以不固定. 如果验证码长度小于4,用'_'补齐 # 把彩色图像转为灰度图像(色彩对识别验证码没有什么用) def convert2gray(img): if len(img.shape) > 2: gray = np.mean(img, -1) # 上面的转法较快,正规转法如下 # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2] # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return gray else: return img
def cnn_random(): text, image = gen_captcha_text_and_image() image = Image.fromarray(image) image.save(captcha_path) return redirect(url_for('cnn.cnn_index'))
import tensorflow as tf import random import os from tensorflow.python.framework import ops from tensorflow.python.framework import dtypes from gen_captcha import gen_captcha_text_and_image from gen_captcha import number from gen_captcha import alphabet from gen_captcha import ALPHABET import numpy as np import tensorflow as tf text, image = gen_captcha_text_and_image() IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 MAX_CAPTCHA = len(text) def transformX(image): image = convert2gray(image) return image / 255 def transformY(text): return text2vec(text) def convert2gray(img): if len(img.shape) > 2: gray = np.mean(img, -1) # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
def wrap_gen_captcha_text_and_image(self): while True: text, image = gen_captcha_text_and_image() if image.shape == (self.IMAGE_HEIGHT, self.IMAGE_WIDTH, self.IMAGE_CHANNEL): return text, image