def get_next_batch(batch_size=128): images = [] to_images = [] max_width_image = 0 font_min_length = random.randint(10, 20) for i in range(batch_size): font_name = random.choice(AllFontNames) # font_length = random.randint(font_min_length-5, font_min_length+5) font_length = random.randint(3, 5) font_size = random.randint(image_height, 64) font_mode = random.choice([0, 1, 2, 4]) font_hint = random.choice([0, 1, 2, 3, 4, 5]) text = utils_font.get_random_text(CHARS, eng_world_list, font_length) image = utils_font.get_font_image_from_url(text, font_name, font_size, fontmode=font_mode, fonthint=font_hint) to_image = image.copy() image = utils_font.add_noise(image) image = utils_pil.convert_to_gray(image) rate = random.randint(8, 17) / font_size image = utils_pil.resize(image, rate) image = np.asarray(image) image = utils.resize(image, height=image_height) image = (255. - image) / 255. images.append(image) # to_image = utils_font.get_font_image_from_url(text, font_name ,image_height, fontmode = font_mode, fonthint = font_hint) to_image = utils_pil.convert_to_gray(to_image) to_image = np.asarray(to_image) to_image = utils.resize(to_image, height=image_height) to_image = utils.img2bwinv(to_image) to_image = to_image / 255. to_images.append(to_image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] if to_image.shape[1] > max_width_image: max_width_image = to_image.shape[1] max_width_image = max_width_image + (POOL_SIZE - max_width_image % POOL_SIZE) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = utils.img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i, :] = np.transpose(image_vec) labels = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(to_images)): image_vec = utils.img2vec(to_images[i], height=image_height, width=max_width_image, flatten=False) labels[i, :] = np.transpose(image_vec) return inputs, labels
def get_next_batch_for_srgan(batch_size=128): inputs_images = [] targets_images = [] max_width_image = 0 for i in range(batch_size): font_name = random.choice(AllFontNames) font_length = random.randint(3, 70) font_size = 36 #random.randint(image_height, 64) font_mode = random.choice([0, 1, 2, 4]) font_hint = random.choice([0, 1, 2, 3, 4, 5]) #删除了2 text = utils_font.get_random_text(CHARS, eng_world_list, font_length) image = utils_font.get_font_image_from_url(text, font_name, font_size, font_mode, font_hint) image = utils_pil.resize_by_height(image, image_height) image = utils_pil.convert_to_gray(image) targets_image = image.copy() targets_image = np.asarray(targets_image) targets_image = (255. - targets_image) / 255. # targets_image = np.reshape(targets_image,[-1]) # targets_image = np.pad(targets_image,(0, image_size*image_size-np.size(targets_image)),"constant") # targets_image = np.reshape(targets_image, [image_size,image_size]) targets_images.append(targets_image) _h = random.randint(9, image_height // random.choice([1, 1.5, 2, 2.5])) image = utils_pil.resize_by_height(image, _h) image = utils_pil.resize_by_height(image, image_height, random.random() > 0.5) # image,_,_,_,_ = utils_pil.random_space(image) image = utils_font.add_noise(image) image = np.asarray(image) # image = utils.resize(image, height=image_height) image = image * random.uniform(0.3, 1) if random.random() > 0.5: image = (255. - image) / 255. else: image = image / 255. # image = np.reshape(image,[-1]) # image = np.pad(image,(0, image_size*image_size-np.size(image)),"constant") # image = np.reshape(image, [image_size,image_size]) inputs_images.append(image) inputs = np.zeros([batch_size, image_size, image_size]) for i in range(batch_size): inputs[i, :] = utils.square_img(inputs_images[i], np.zeros([image_size, image_size])) targets = np.zeros([batch_size, image_size, image_size]) for i in range(batch_size): targets[i, :] = utils.square_img(targets_images[i], np.zeros([image_size, image_size])) return inputs, targets
def get_next_batch_for_srgan(batch_size=128): inputs_images = [] targets_images = [] max_width_image = 0 for i in range(batch_size): font_name = random.choice(AllFontNames) font_length = random.randint(4, 5) font_size = 36 #random.randint(image_height, 64) font_mode = random.choice([0,1,2,4]) font_hint = random.choice([0,1,2,3,4,5]) #删除了2 text = utils_font.get_random_text(CHARS, eng_world_list, font_length) image = utils_font.get_font_image_from_url(text, font_name, font_size, font_mode, font_hint) image = utils_pil.resize_by_height(image, image_height) image = utils_pil.convert_to_gray(image) targets_image = image.copy() _h = random.randint(9, image_height // random.choice([1,1.5,2,2.5])) image = utils_pil.resize_by_height(image, _h) image = utils_pil.resize_by_height(image, image_height, random.random()>0.5) targets_image = np.asarray(targets_image) # targets_image = utils.resize(targets_image, height=image_height) # targets_image = utils_pil.convert_to_bw(targets_image) targets_images.append((255. - targets_image) / 255.) image = utils_font.add_noise(image) image = np.asarray(image) # image = utils.resize(image, height=image_height) image = image * random.uniform(0.3, 1) if random.random()>0.5: image = (255. - image) / 255. else: image = image / 255. inputs_images.append(image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] if targets_image.shape[1] > max_width_image: max_width_image = targets_image.shape[1] # max_width_image = max_width_image + (POOL_SIZE - max_width_image % POOL_SIZE) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(batch_size): image_vec = utils.img2vec(inputs_images[i], height=image_height, width=max_width_image, flatten=False) inputs[i,:] = np.transpose(image_vec) targets = np.zeros([batch_size, max_width_image, image_height]) for i in range(batch_size): image_vec = utils.img2vec(targets_images[i], height=image_height, width=max_width_image, flatten=False) targets[i,:] = np.transpose(image_vec) return inputs, targets
def getImage(CHARS, font_name, image_height, font_length, font_size, word_dict): text = utils_font.get_random_text(CHARS, word_dict, font_length) img = utils_font.get_font_image_from_url(text, font_name, font_size) img = utils_font.add_noise(img) img = utils_pil.convert_to_gray(img) w, h = img.size _h = random.randint(9, image_height) _w = round(w * _h / h) img = img.resize((_w, _h), Image.ANTIALIAS) img = np.asarray(img) # img = utils.clearBackgroundColor(img) img = 1 - img / 255. img = utils.dropZeroEdges(img) filter = np.random.random(img.shape) - 0.9 filter = np.maximum(filter, 0) img = img + filter * 5 imin, imax = img.min(), img.max() img = (img - imin) / (imax - imin) img = utils.resize(img, image_height) return text, img
def get_next_batch(batch_size=128): images = [] to_images = [] codes = [] max_width_image = 0 for i in range(batch_size): font_name = random.choice(AllFontNames) font_length = random.randint(25, 30) font_size = 36 #random.randint(image_height, 64) font_mode = random.choice([0, 1, 2, 4]) font_hint = random.choice([0, 1, 2, 3, 4, 5]) text = utils_font.get_random_text(CHARS, eng_world_list, font_length) # text = random.sample(CHARS, 12) # text = text+text # random.shuffle(text) # text = "".join(text).strip() codes.append([CHARS.index(char) for char in text]) image = utils_font.get_font_image_from_url(text, font_name, font_size, fontmode=font_mode, fonthint=font_hint) image = utils_pil.resize_by_height(image, image_height) to_image = image.copy() image = utils_font.add_noise(image) image = utils_pil.convert_to_gray(image) _h = random.randint(9, image_height // random.choice([1, 1.5, 2, 2.5])) image = utils_pil.resize_by_height(image, _h, random.random() > 0.5) image = utils_pil.resize_by_height(image, image_height, random.random() > 0.5) image = np.asarray(image) image = utils.resize(image, height=image_height) image = (255. - image) / 255. images.append(image) # to_image = utils_font.get_font_image_from_url(text, font_name ,image_height, fontmode = font_mode, fonthint = font_hint) to_image = utils_pil.convert_to_gray(to_image) to_image = np.asarray(to_image) to_image = utils.resize(to_image, height=image_height) to_image = utils.img2bwinv(to_image) to_image = to_image / 255. to_images.append(to_image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] if to_image.shape[1] > max_width_image: max_width_image = to_image.shape[1] max_width_image = max_width_image + (POOL_SIZE - max_width_image % POOL_SIZE) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = utils.img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i, :] = np.transpose(image_vec) targets = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(to_images)): image_vec = utils.img2vec(to_images[i], height=image_height, width=max_width_image, flatten=False) targets[i, :] = np.transpose(image_vec) labels = [np.asarray(i) for i in codes] sparse_labels = utils.sparse_tuple_from(labels) seq_len = np.ones(batch_size) * (max_width_image * image_height) // (POOL_SIZE * POOL_SIZE) return inputs, targets, sparse_labels, seq_len
def get_next_batch_for_res(batch_size=128, add_noise=True, _font_name=None, _font_size=None, _font_mode=None, _font_hint=None): inputs_images = [] codes = [] max_width_image = 0 info = [] for i in range(batch_size): font_name = _font_name font_size = _font_size font_mode = _font_mode font_hint = _font_hint if font_name == None: font_name = random.choice(AllFontNames) if font_size == None: if random.random() > 0.5: font_size = random.randint(9, 49) else: font_size = random.randint(9, 15) if font_mode == None: font_mode = random.choice([0, 1, 2, 4]) if font_hint == None: font_hint = random.choice([0, 1, 2, 3, 4, 5]) while True: font_length = random.randint(5, 400) # text = random.sample(CHARS, font_length) # text = text+text+[" "," "] # random.shuffle(text) # text = "".join(text).strip() text = utils_font.get_random_text(CHARS, eng_world_list, font_length) image = utils_font.get_font_image_from_url(text, font_name, font_size, font_mode, font_hint) temp_image = utils_pil.resize_by_height(image, image_height) w, h = temp_image.size if w * h < image_size * image_size: break image = utils_pil.convert_to_gray(image) w, h = image.size if h > image_height: image = utils_pil.resize_by_height(image, image_height) if add_noise and random.random() > 0.5: _h = random.randint(9, image_height + 1) image = utils_pil.resize_by_height(image, _h) image = utils_pil.random_space2(image, image_height) if add_noise: image = utils_font.add_noise(image) image = np.asarray(image) # image = utils.resize(image, height=image_height) if add_noise: image = image * random.uniform(0.3, 1) if add_noise and random.random() > 0.5: image = image / 255. else: image = (255. - image) / 255. inputs_images.append(image) codes.append([CHARS.index(char) for char in text]) info.append( [font_name, str(font_size), str(font_mode), str(font_hint)]) inputs = np.zeros([batch_size, image_size, image_size]) for i in range(batch_size): inputs[i, :] = utils.square_img(inputs_images[i], np.zeros([image_size, image_size])) labels = [np.asarray(i) for i in codes] sparse_labels = utils.sparse_tuple_from(labels) seq_len = np.ones(batch_size) * (image_size * image_size) // (POOL_SIZE * POOL_SIZE) return inputs, sparse_labels, seq_len, info