def get_next_batch_test_hzeng(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]) rootdir = join(capt.cfg.workspace, 'test') file_names = [] for parent, dirnames, filenames in os.walk( rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames for i in range(batch_size): text, image = wrap_get_captcha_text_and_image(rootdir, file_names) image = convert2gray(image) # fig, axarr = pylab.plt.subplots(2, 2) # axarr[0, 0].imshow(image) # pylab.show() batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y
def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 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 = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 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) if text == predict_text: right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt)
def batch_hack_captcha_save(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 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 = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 rootdir = join(capt.cfg.workspace, 'test') file_names = [] for parent, dirnames, filenames in os.walk( rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames for file_name in filenames: print(file_name) file_name, image = get_captcha_text_and_image2(rootdir, file_name) image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) file_name2 = '__' + predict_text + '__' + file_name os.rename(join(rootdir, file_name), join(rootdir, file_name2))
def predict_captcha(image): image = Image.open(image) image = np.array(image) image = convert2gray(image) image = image.flatten() / 255 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: # restore only once saver.restore(sess, tf.train.latest_checkpoint(model_path)) text = hack_function(sess, predict, image) return text
def hack_captcha(captcha_image): # 定义预测计算图 output = crack_captcha_cnn() predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() predict_text = "error" with tf.Session() as sess: # saver = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() image = convert2gray(captcha_image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) return predict_text
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() image = convert2gray(image) text = text.upper() batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 try: batch_y[i, :] = text2vec(text) except IndexError as e: i -= 1 print("IndexError:%s", text) continue return batch_x, batch_y
def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 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 = tf.train.import_meta_graph(save_model + ".meta") saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 rootdir = join(capt.cfg.workspace, 'test') file_names = [] for parent, dirnames, filenames in os.walk( rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 file_names = filenames for i in range(task_cnt): text, image = get_captcha_text_and_image(rootdir, file_names) image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: right_cnt += 1 else: print("标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt)
def batch_hack_captcha(): """ 批量生成验证码,然后再批量进行识别 :return: """ # 定义预测计算图 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 = tf.train.import_meta_graph(save_model + ".meta") # https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/api_docs/python/state_ops.md#latest_checkpoint saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() task_cnt = 1000 right_cnt = 0 for i in range(task_cnt): text, image = wrap_gen_captcha_text_and_image() text = text.upper() image = convert2gray(image) image = image.flatten() / 255 predict_text = hack_function(sess, predict, image) if text == predict_text: right_cnt += 1 print("正确预测:标记: {} 预测: {}".format(text, predict_text)) else: print("错误预测:标记: {} 预测: {}".format(text, predict_text)) pass # print("标记: {} 预测: {}".format(text, predict_text)) print('task:', task_cnt, ' cost time:', (time.time() - stime), 's') print('right/total-----', right_cnt, '/', task_cnt) pass
def hack_capt(self, image): image = Image.open(image) image = np.array(image) image = convert2gray(image) image = image.flatten() / 255 return hack_function(self.sess, self.predict, image)
def hack_captcha(captcha_image): w_alpha = 0.01 b_alpha = 0.1 with tf.Graph().as_default() as g: X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN]) keep_prob = tf.placeholder(tf.float32) # dropout """ 定义CNN cnn在图像大小是2的倍数时性能最高, 如果你用的图像大小不是2的倍数,可以在图像边缘补无用像素。 np.pad(image,((2,3),(2,2)), 'constant', constant_values=(255,)) # 在图像上补2行,下补3行,左补2行,右补2行 """ x = tf.reshape(X, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) # w_c1_alpha = np.sqrt(2.0/(IMAGE_HEIGHT*IMAGE_WIDTH)) # # w_c2_alpha = np.sqrt(2.0/(3*3*32)) # w_c3_alpha = np.sqrt(2.0/(3*3*64)) # w_d1_alpha = np.sqrt(2.0/(8*32*64)) # out_alpha = np.sqrt(2.0/1024) # 3 conv layer 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') conv1 = tf.nn.dropout(conv1, keep_prob) 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') conv2 = tf.nn.dropout(conv2, keep_prob) 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') conv3 = tf.nn.dropout(conv3, keep_prob) # Fully connected layer w_d = tf.Variable(w_alpha * tf.random_normal([8 * 20 * 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, keep_prob) w_out = tf.Variable( w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN])) b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN])) output = tf.add(tf.matmul(dense, w_out), b_out) # 36*4 predict_text = "error" #output=crack_captcha_cnn() with g.name_scope( "myscope" ) as scope: # 有了这个scope,下面的op的name都是类似myscope/Placeholder这样的前缀 sess = tf.Session(target='', graph=g, config=None) # target表示要连接的tf执行引擎 predict = tf.argmax( tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) saver = tf.train.Saver() predict_text = "error" with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(model_path)) stime = time.time() image = convert2gray(captcha_image) image = image.flatten() / 255 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) return predict_text