def generate_chinese_images_to_check(obj_size=CHAR_IMG_SIZE, augmentation=False): print("Get font_file_list ...") font_file_list = [ os.path.join(FONT_FILE_DIR, font_name) for font_name in os.listdir(FONT_FILE_DIR) if font_name.lower()[-4:] in (".otf", ".ttf", ".ttc", ".fon") ] # font_file_list = [os.path.join(FONT_FINISHED_DIR, "chinese_fonts_暂时移出/康熙字典体完整版本.otf")] chinese_char_num = len(CHAR2ID_DICT) total_num = len(font_file_list) * chinese_char_num count = 0 for font_file in font_file_list: # 外层循环是字体 font_name = os.path.basename(font_file) font_type = font_name.split(".")[0] # 创建保存该字体图片的目录 font_img_dir = os.path.join(CHAR_IMGS_DIR, font_type) remove_then_makedirs(font_img_dir) for chinese_char, bigger_PIL_img in generate_all_chinese_images_bigger( font_file, image_size=int(obj_size * 1.2)): # 内层循环是字 # 检查生成的灰度图像是否可用,黑底白字 image_data = list(bigger_PIL_img.getdata()) if sum(image_data) < 10: continue if not augmentation: PIL_img = get_standard_image(bigger_PIL_img, obj_size, reverse_color=True) else: PIL_img = get_augmented_image(bigger_PIL_img, obj_size, rotation=True, dilate=False, erode=True, reverse_color=True) # 保存生成的字体图片 image_name = chinese_char + ".jpg" save_path = os.path.join(font_img_dir, image_name) PIL_img.save(save_path, format="jpeg") # 当前进度 count += 1 if count % 200 == 0: print("Progress bar: %.2f%%" % (count * 100 / total_num)) sys.stdout.flush() return
def convert_chinese_images_to_check(obj_size=CHAR_IMG_SIZE, augmentation=True): print("Get total images num ...") font_images_num_list = [ len(os.listdir(os.path.join(EXTERNEL_IMAGES_DIR, content))) for content in os.listdir(EXTERNEL_IMAGES_DIR) if os.path.isdir(os.path.join(EXTERNEL_IMAGES_DIR, content)) ] print("Begin to convert images ...") total_num = sum(font_images_num_list) count = 0 for font_type, image_paths_list in get_external_image_paths( root_dir=EXTERNEL_IMAGES_DIR): # 创建保存该字体图片的目录 font_img_dir = os.path.join(CHAR_IMGS_DIR, font_type) remove_then_makedirs(font_img_dir) for image_path in image_paths_list: # 加载外部图片,将图片调整为正方形 # 为了保证图片旋转时不丢失信息,生成的图片比本来的图片稍微bigger # 为了方便图片的后续处理,图片必须加载为黑底白字,可以用reverse_color来调整 try: bigger_PIL_img = load_external_image_bigger( image_path, white_background=True, reverse_color=True) except OSError: print("The image %s result in OSError !" % image_path) continue if not augmentation: PIL_img = get_standard_image(bigger_PIL_img, obj_size, reverse_color=True) else: PIL_img = get_augmented_image(bigger_PIL_img, obj_size, rotation=True, dilate=False, erode=True, reverse_color=True) # 保存生成的字体图片 image_name = os.path.basename(image_path).split(".")[0] + ".jpg" save_path = os.path.join(font_img_dir, image_name) PIL_img.save(save_path, format="jpeg") # 当前进度 count += 1 if count % 200 == 0: print("Progress bar: %.2f%%" % (count * 100 / total_num)) sys.stdout.flush()
def generate_chinese_images(obj_size=CHAR_IMG_SIZE, num_imgs_per_font=NUM_IMAGES_PER_FONT): print("Get font_file_list ...") font_file_list = [ os.path.join(FONT_FILE_DIR, font_name) for font_name in os.listdir(FONT_FILE_DIR) if font_name.lower()[-4:] in (".otf", ".ttf", ".ttc", ".fon") ] print("Begin to generate images ...") chinese_char_num = len(CHAR2ID_DICT) total_num = len(font_file_list) * chinese_char_num count = 0 for font_file in font_file_list: # 外层循环是字体 font_name = os.path.basename(font_file) font_type = font_name.split(".")[0] # 创建保存该字体图片的目录 save_dir = os.path.join(CHAR_IMGS_DIR, font_type) remove_then_makedirs(save_dir) for chinese_char, bigger_PIL_img in generate_all_chinese_images_bigger( font_file, image_size=int(obj_size * 1.2)): # 内层循环是字 # 检查生成的灰度图像是否可用,黑底白字 image_data = list(bigger_PIL_img.getdata()) if sum(image_data) < 10: continue PIL_img_list = \ [get_augmented_image(bigger_PIL_img, obj_size, rotation=True, dilate=False, erode=True, reverse_color=True) for i in range(num_imgs_per_font)] # 保存生成的字体图片 for index, PIL_img in enumerate(PIL_img_list): image_name = chinese_char + "_" + str(index) + ".jpg" save_path = os.path.join(save_dir, image_name) PIL_img.save(save_path, format="jpeg") # 当前进度 count += 1 if count % 200 == 0: print("Progress bar: %.2f%%" % (count * 100 / total_num)) sys.stdout.flush()
def convert_tfrecords(obj_size=CHAR_IMG_SIZE, num_imgs_per_font=NUM_IMAGES_PER_FONT): print("Get total images num ...") font_images_num_list = [ len(os.listdir(os.path.join(EXTERNEL_IMAGES_DIR, content))) for content in os.listdir(EXTERNEL_IMAGES_DIR) if os.path.isdir(os.path.join(EXTERNEL_IMAGES_DIR, content)) ] # 创建保存tfrecords文件的目录 check_or_makedirs(CHAR_TFRECORDS_DIR) # 可以把变换的图片直接存入tfrecords文件 # 不必将变换的图片先保存到磁盘,再从磁盘读取出来保存到tfrecords文件,这样效率太低 # 通常是用一种字体的一个字图片增强出很多个图片,这些图片最好是分开存放 # 若直接把同一字体同一个字图片增强出的多张图片连续放到同一个tfrecords里,那么每一个训练batch的多样性就不好 writers_list = \ [tf.io.TFRecordWriter(os.path.join(CHAR_TFRECORDS_DIR, "chinese_imgs_%d_from_img.tfrecords" % i)) for i in range(20)] print("Begin to convert images ...") total_num = sum(font_images_num_list) count = 0 for font_type, image_paths_list in get_external_image_paths( root_dir=EXTERNEL_IMAGES_DIR): for image_path in image_paths_list: chinese_char = os.path.basename(image_path)[0] # 加载外部图片,将图片调整为正方形 # 为了保证图片旋转时不丢失信息,生成的图片比本来的图片稍微bigger # 为了方便图片的后续处理,图片必须加载为黑底白字,可以用reverse_color来调整 try: bigger_PIL_img = load_external_image_bigger( image_path, white_background=True, reverse_color=True) except OSError: print("The image %s result in OSError !" % image_path) continue PIL_img_list = \ [get_augmented_image(bigger_PIL_img, obj_size, rotation=True, dilate=False, erode=True, reverse_color=True) for i in range(num_imgs_per_font)] # 保存生成的字体图片 for index, PIL_img in enumerate(PIL_img_list): # train_set和test_set的比例约为 5:1 writer = random.choice(writers_list) bytes_image = PIL_img.tobytes() # 将图片转化为原生bytes bytes_char = chinese_char.encode('utf-8') example = tf.train.Example(features=tf.train.Features( feature={ 'bytes_image': tf.train.Feature(bytes_list=tf.train.BytesList( value=[bytes_image])), 'bytes_char': tf.train.Feature(bytes_list=tf.train.BytesList( value=[bytes_char])), })) writer.write(example.SerializeToString()) # 当前进度 count += 1 if count % 200 == 0: print("Progress bar: %.2f%%" % (count * 100 / total_num)) sys.stdout.flush() # 关闭所有的 tfrecords writer [writer.close() for writer in writers_list]
def generate_tfrecords(obj_size=CHAR_IMG_SIZE, num_imgs_per_font=NUM_IMAGES_PER_FONT): print("Get font_file_list ...") font_file_list = [ os.path.join(FONT_FILE_DIR, font_name) for font_name in os.listdir(FONT_FILE_DIR) if font_name.lower()[-4:] in (".otf", ".ttf", ".ttc", ".fon") ] # 创建保存tfrecords文件的目录 check_or_makedirs(CHAR_TFRECORDS_DIR) # 可以把生成的图片直接存入tfrecords文件 # 不必将生成的图片先保存到磁盘,再从磁盘读取出来保存到tfrecords文件,这样效率太低 # 通常是用某种字体对一个字生成很多个增强的图片,这些图片最好是分开存放 # 若直接把同一字体同一个字的多张图片连续放到同一个tfrecords里,那么训练batch的多样性不好 writers_list = \ [tf.io.TFRecordWriter(os.path.join(CHAR_TFRECORDS_DIR, "chinese_imgs_%d_from_font.tfrecords" % i)) for i in range(20)] print("Begin to generate images ...") chinese_char_num = len(CHAR2ID_DICT) total_num = len(font_file_list) * chinese_char_num count = 0 for font_file in font_file_list: # 外层循环是字体 for chinese_char, bigger_PIL_img in generate_all_chinese_images_bigger( font_file, image_size=int(obj_size * 1.2)): # 内层循环是字 # 检查生成的灰度图像是否可用,黑底白字 image_data = list(bigger_PIL_img.getdata()) if sum(image_data) < 10: continue PIL_img_list = \ [get_augmented_image(bigger_PIL_img, obj_size, rotation=True, dilate=False, erode=True, reverse_color=True) for i in range(num_imgs_per_font)] # 保存生成的字体图片 for PIL_img in PIL_img_list: writer = random.choice(writers_list) bytes_image = PIL_img.tobytes() # 将图片转化为原生bytes bytes_char = chinese_char.encode('utf-8') example = tf.train.Example(features=tf.train.Features( feature={ 'bytes_image': tf.train.Feature(bytes_list=tf.train.BytesList( value=[bytes_image])), 'img_height': tf.train.Feature(int64_list=tf.train.Int64List( value=[PIL_img.height])), 'img_width': tf.train.Feature(int64_list=tf.train.Int64List( value=[PIL_img.width])), 'bytes_char': tf.train.Feature(bytes_list=tf.train.BytesList( value=[bytes_char])) })) writer.write(example.SerializeToString()) # 当前进度 count += 1 if count % 200 == 0: print("Progress bar: %.2f%%" % (count * 100 / total_num)) sys.stdout.flush() # 关闭所有的tfrecords写者 [writer.close() for writer in writers_list]