def get_next_batch(batch_size=64, cnt=0): """ # 生成一个训练batch :param batch_size cnt :return: """ batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) f = open(root + "mappings.txt", 'r') lines = f.readlines() f.close() i = 0 for j in range(cnt * batch_size, (cnt + 1) * batch_size): text = lines[j].split(",")[-1] text = text.split("=")[0] image = Image.open(root + str(j).zfill(4) + ".jpg") # print(j) # print(text) # image.show() image = convert2gray(image) batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) i += 1 return batch_x, batch_y
def get_batch(batch_size=128): batch_x = np.zeros([batch_size, image_size]) batch_y = np.zeros([batch_size, char_size]) for i in range(batch_size): text, image = captcha(char_set=char_set, captcha_size=1, width=image_w, height=image_h) batch_x[i, :] = img2vec(img2gray(image)) batch_y[i, :] = text2vec(char_set, text) return batch_x, batch_y
def get_batch(batch_size=128): batch_files = random.sample(files, batch_size) batch_x = np.zeros([batch_size, image_size]) batch_y = np.zeros([batch_size, captcha_size * char_size]) for i in range(batch_size): image = loadimg(os.path.join(fonts_dir, batch_files[i])) text = batch_files[i][:4] batch_x[i, :] = img2vec(img2gray(image)) batch_y[i, :] = text2vec(char_set, text) return batch_x, batch_y
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: """ imgpath = "D:/gitrepos/captcha-tensorflow/vcode1/" batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) td = train_data() for i in range(batch_size): #text, image = wrap_gen_captcha_text_and_image() text, image = td.get_text_img(imgpath) 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
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) #x[:,i]表示取所有维中第i个数据,通常返回数组 #x[:,m:n],即取所有维中第m到n-1个数据,含左不含右 #x[i,:]表示取第一维中下标为i的所有元素,通常返回数组 #flatten() 是将多维数组降位到一维并返回拷贝,默认降维是横向的 batch_x[ i, :] = image.flatten() / 255 # (image.flatten()-128)/128 mean为0 batch_y[i, :] = text2vec(text) return batch_x, batch_y