Path: blob/master/CharClassification/generate_images.py
3118 views
import os1import random2import numpy as np3import uuid45PATH_TO_LIGHT_BACKGROUNDS = 'light_backgrounds/'6PATH_TO_DARK_BACKGROUNDS = 'dark_backgrounds/'7PATH_TO_FONT_FILES = 'fonts/'8OUTPUT_DIR = 'output/'9NUM_IMAGES_PER_CLASS = 101011# Get all files from directory12def get_files_from_dir(dirname):13list_files = (os.listdir(dirname))14list_files = [dirname + x for x in list_files]15return list_files161718# Random perspective distortion created by randomly moving the for corners of the image.19def get_distort_arg():20amount = 521hundred_minus_amount = 100 - amount22return '\'0,0 ' + str(np.random.randint(0,amount)) + ',' + str(np.random.randint(0,amount)) + ' 100,0 ' + str(np.random.randint(hundred_minus_amount,100)) + ',' + str(np.random.randint(0,amount)) + ' 0,100 ' + str(np.random.randint(0,amount)) + ',' + str(np.random.randint(hundred_minus_amount,100)) + ' 100,100 ' + str(np.random.randint(hundred_minus_amount,100)) + ',' + str(np.random.randint(hundred_minus_amount,100)) + '\''2324# Randomly extracts 32x32 regions of an image and saves it to outdir25def create_random_crops(image_filename, num_crops, out_dir):26dim = os.popen('convert ' + image_filename + ' -ping -format "%w %h" info:').read()27dim = dim.split()28im_width = int(dim[0])29im_height = int(dim[1])3031for i in range(0, num_crops):32# Randomly select first co-ordinate of square for cropping image33x = random.randint(0,im_width - 32)34y = random.randint(0,im_height - 32)35outfile = uuid.uuid4().hex + '.jpg'36command = "magick convert "+ image_filename + " -crop 32x32"+"+"+str(x)+"+"+str(y)+" " + os.path.join(out_dir, outfile)37os.system(str(command))3839# Generate crops for all files in file_list and store them in dirname40def generate_crops(file_list, dirname):41if not os.path.isdir(dirname):42os.mkdir(dirname)43for f in file_list:44create_random_crops(f, 10, dirname)454647# List of characters48char_list = []49for i in range(65, 65+26):50char_list.append(chr(i))5152# List of digits53for j in range(48,48+10):54char_list.append(chr(j))5556# List of light font colors57color_light = ['white','lime','gray','yellow','silver','aqua']5859# List of light dark colors60color_dark = ['black','green','maroon','blue','purple','red']616263# List of light backgrounds64light_backgrounds = get_files_from_dir(PATH_TO_LIGHT_BACKGROUNDS)6566# List of dark backgrounds67dark_backgrounds = get_files_from_dir(PATH_TO_DARK_BACKGROUNDS)6869# List of font files70list_files_fontt = get_files_from_dir(PATH_TO_FONT_FILES)71727374light_backgrounds_crops_dir = 'light_backgrounds_crops/'75dark_backgrounds_crops_dir = 'dark_backgrounds_crops/'7677generate_crops(light_backgrounds, light_backgrounds_crops_dir)78generate_crops(dark_backgrounds, dark_backgrounds_crops_dir)7980# List of all files in the crops directory81light_backgrounds = get_files_from_dir(light_backgrounds_crops_dir)82dark_backgrounds = get_files_from_dir(dark_backgrounds_crops_dir)8384# List of all backgrounds85all_backgrounds = [dark_backgrounds, light_backgrounds]868788# Sample Command----- magick convert image.jpg -fill Black -font Courier-Oblique -weight 50 -pointsize 12 -gravity center -blur 0x8 -evaluate Gaussian-noise 1.2 -annotate 0+0 "Some text" output_image8990for i in range(0,len(char_list)):91char = char_list[i]92char_output_dir = OUTPUT_DIR + str(char) + "/"9394if not os.path.exists(char_output_dir):95os.makedirs(char_output_dir)9697print("Generating data " + char_output_dir)9899100# Generate synthetic images101for j in range(0,NUM_IMAGES_PER_CLASS):102103# Choose a light or dark background104path = random.choice(all_backgrounds)105106# Choose a file107list_filernd = random.choice(path)108109# Choose a font110list_rfo = random.choice(list_files_fontt)111112# Get random distortion113distort_arg = get_distort_arg()114115# Get random blur amount116blur = random.randint(0,3)117118# Get random noise amount119noise = random.randint(0,5)120121# Add random shifts from the center122x = str(random.randint(-3,3))123y = str(random.randint(-3,3))124125# Choose light color for dark backgrounds and vice-versa126if path == all_backgrounds[0] :127color = random.choice(color_light)128else:129color = random.choice(color_dark)130131command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \132str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity center" + " -blur 0x" + str(blur) \133+ " -evaluate Gaussian-noise " + str(noise) + " " + " -annotate +" + x + "+" + y + " " + str(char_list[i]) + " " + char_output_dir + "output_file"+str(i)+str(j)+".jpg"134135# Uncomment line below to see what command is executed.136# print(command)137os.system(str(command))138139140