def batch_hack_captcha(): # 定义预测计算图 global_step = tf.Variable(0, trainable=False) output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() with tf.Session(config=tf.ConfigProto(device_count={'gpu':0})) as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 100 right_cnt = 0 for i in range(task_cnt): text, image = wrap_gen_captcha_text_and_image() image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: print() text=text.replace('_','') print("----MATCH: {}".format(text)) print() right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt)
def test_hack_captcha_training_data(sess, output): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) stime = time.time() right_cnt = 0 task_cnt = 300 for i in range(task_cnt): text, image = wrap_gen_captcha_text_and_image() #image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) #predict_text=predict_text.replace('_','') if text == predict_text: print("----标记: {} 预测: {}".format(text, predict_text)) right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) #print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') #print('right/total-----', right_cnt, '/', task_cnt) return right_cnt / task_cnt
def get_next_batch_from_web(batch_size=64): batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) text, image = wrap_gen_captcha_text_and_image(batch_size) for i in range(batch_size): batch_x[i, :] = image[i].flatten( ) / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text[i]) return batch_x, batch_y
def get_next_batch(batch_size=128): """ # 生成一个训练batch :param batch_size: :return: """ 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 = wrap_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 get_next_batch(batch_size=128): """ # 生成一个训练batch :param batch_size: :return: """ 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 = wrap_gen_captcha_text_and_image() if False: #此处改为Ture,用以输出查看训练图片 image_ = Image.fromarray(image) image_.save('./test_out/%s.jpg' % text) #exit() batch_x[i, :] = image.flatten() / 255 batch_y[i, :] = text2vec(text) return batch_x, batch_y