generate_images.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import os
  2. import random
  3. import numpy as np
  4. import uuid
  5. PATH_TO_LIGHT_BACKGROUNDS = 'light_backgrounds/'
  6. PATH_TO_DARK_BACKGROUNDS = 'dark_backgrounds/'
  7. PATH_TO_FONT_FILES = 'fonts/'
  8. OUTPUT_DIR = 'output/'
  9. NUM_IMAGES_PER_CLASS = 10
  10. # Get all files from directory
  11. def get_files_from_dir(dirname):
  12. list_files = (os.listdir(dirname))
  13. list_files = [dirname + x for x in list_files]
  14. return list_files
  15. # Random perspective distortion created by randomly moving the for corners of the image.
  16. def get_distort_arg():
  17. amount = 5
  18. hundred_minus_amount = 100 - amount
  19. return '\'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)) + '\''
  20. # Randomly extracts 32x32 regions of an image and saves it to outdir
  21. def create_random_crops(image_filename, num_crops, out_dir):
  22. dim = os.popen('convert ' + image_filename + ' -ping -format "%w %h" info:').read()
  23. dim = dim.split()
  24. im_width = int(dim[0])
  25. im_height = int(dim[1])
  26. for i in range(0, num_crops):
  27. # Randomly select first co-ordinate of square for cropping image
  28. x = random.randint(0,im_width - 32)
  29. y = random.randint(0,im_height - 32)
  30. outfile = uuid.uuid4().hex + '.jpg'
  31. command = "magick convert "+ image_filename + " -crop 32x32"+"+"+str(x)+"+"+str(y)+" " + os.path.join(out_dir, outfile)
  32. os.system(str(command))
  33. # Generate crops for all files in file_list and store them in dirname
  34. def generate_crops(file_list, dirname):
  35. if not os.path.isdir(dirname):
  36. os.mkdir(dirname)
  37. for f in file_list:
  38. create_random_crops(f, 10, dirname)
  39. # List of characters
  40. char_list = []
  41. for i in range(65, 65+26):
  42. char_list.append(chr(i))
  43. # List of digits
  44. for j in range(48,48+10):
  45. char_list.append(chr(j))
  46. # List of light font colors
  47. color_light = ['white','lime','gray','yellow','silver','aqua']
  48. # List of light dark colors
  49. color_dark = ['black','green','maroon','blue','purple','red']
  50. # List of light backgrounds
  51. light_backgrounds = get_files_from_dir(PATH_TO_LIGHT_BACKGROUNDS)
  52. # List of dark backgrounds
  53. dark_backgrounds = get_files_from_dir(PATH_TO_DARK_BACKGROUNDS)
  54. # List of font files
  55. list_files_fontt = get_files_from_dir(PATH_TO_FONT_FILES)
  56. light_backgrounds_crops_dir = 'light_backgrounds_crops/'
  57. dark_backgrounds_crops_dir = 'dark_backgrounds_crops/'
  58. generate_crops(light_backgrounds, light_backgrounds_crops_dir)
  59. generate_crops(dark_backgrounds, dark_backgrounds_crops_dir)
  60. # List of all files in the crops directory
  61. light_backgrounds = get_files_from_dir(light_backgrounds_crops_dir)
  62. dark_backgrounds = get_files_from_dir(dark_backgrounds_crops_dir)
  63. # List of all backgrounds
  64. all_backgrounds = [dark_backgrounds, light_backgrounds]
  65. # 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_image
  66. for i in range(0,len(char_list)):
  67. char = char_list[i]
  68. char_output_dir = OUTPUT_DIR + str(char) + "/"
  69. if not os.path.exists(char_output_dir):
  70. os.makedirs(char_output_dir)
  71. print("Generating data " + char_output_dir)
  72. # Generate synthetic images
  73. for j in range(0,NUM_IMAGES_PER_CLASS):
  74. # Choose a light or dark background
  75. path = random.choice(all_backgrounds)
  76. # Choose a file
  77. list_filernd = random.choice(path)
  78. # Choose a font
  79. list_rfo = random.choice(list_files_fontt)
  80. # Get random distortion
  81. distort_arg = get_distort_arg()
  82. # Get random blur amount
  83. blur = random.randint(0,3)
  84. # Get random noise amount
  85. noise = random.randint(0,5)
  86. # Add random shifts from the center
  87. x = str(random.randint(-3,3))
  88. y = str(random.randint(-3,3))
  89. # Choose light color for dark backgrounds and vice-versa
  90. if path == all_backgrounds[0] :
  91. color = random.choice(color_light)
  92. else:
  93. color = random.choice(color_dark)
  94. command = "magick convert " + str(list_filernd) + " -fill "+str(color)+" -font "+ \
  95. str(list_rfo) + " -weight 200 -pointsize 24 -distort Perspective "+str(distort_arg)+" "+"-gravity center" + " -blur 0x" + str(blur) \
  96. + " -evaluate Gaussian-noise " + str(noise) + " " + " -annotate +" + x + "+" + y + " " + str(char_list[i]) + " " + char_output_dir + "output_file"+str(i)+str(j)+".jpg"
  97. # Uncomment line below to see what command is executed.
  98. # print(command)
  99. os.system(str(command))