generate_data_augmentation.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. import time
  5. import random
  6. import math
  7. # image processing imports
  8. from PIL import Image
  9. from ipfml.processing import transform, segmentation
  10. from ipfml import utils
  11. # modules imports
  12. sys.path.insert(0, '') # trick to enable import of main folder module
  13. import custom_config as cfg
  14. from modules.utils import data as dt
  15. # getting configuration information
  16. zone_folder = cfg.zone_folder
  17. min_max_filename = cfg.min_max_filename_extension
  18. # define all scenes values
  19. scenes_list = cfg.scenes_names
  20. scenes_indexes = cfg.scenes_indices
  21. choices = cfg.normalization_choices
  22. path = cfg.dataset_path
  23. zones = cfg.zones_indices
  24. seuil_expe_filename = cfg.seuil_expe_filename
  25. features_choices = cfg.features_choices_labels
  26. output_data_folder = cfg.output_data_folder
  27. image_scene_size = (800, 800)
  28. image_zone_size = (200, 200)
  29. possible_point_zone = tuple(np.asarray(image_scene_size) - np.array(image_zone_size))
  30. data_augmented_filename = cfg.data_augmented_filename
  31. def main():
  32. parser = argparse.ArgumentParser(description="Compute and prepare data augmentation of scenes")
  33. parser.add_argument('--output', type=str, help="output folder expected", required=True)
  34. parser.add_argument('--number', type=int, help="number of images for each sample of scene", required=True)
  35. parser.add_argument('--rotation', type=bool, help="", required=True, default=False)
  36. args = parser.parse_args()
  37. p_output = args.output
  38. p_number = args.number
  39. p_rotation = args.rotation
  40. scenes = os.listdir(path)
  41. # remove min max file from scenes folder
  42. scenes = [s for s in scenes if min_max_filename not in s]
  43. # getting image zone size and usefull information
  44. zone_width, zone_height = image_zone_size
  45. scene_width, scene_height = image_scene_size
  46. nb_x_parts = math.floor(scene_width / zone_width)
  47. output_dataset_filename_path = os.path.join(p_output, data_augmented_filename)
  48. # go ahead each scenes
  49. for folder_scene in scenes:
  50. scene_path = os.path.join(path, folder_scene)
  51. # build output scene path
  52. output_scene_path = os.path.join(p_output, folder_scene)
  53. if not os.path.exists(output_scene_path):
  54. os.makedirs(output_scene_path)
  55. # construct each zones folder name
  56. zones_folder = []
  57. zones_threshold = []
  58. # get zones list info
  59. for index in zones:
  60. index_str = str(index)
  61. if len(index_str) < 2:
  62. index_str = "0" + index_str
  63. current_zone = "zone"+index_str
  64. zones_folder.append(current_zone)
  65. zone_path = os.path.join(scene_path, current_zone)
  66. with open(os.path.join(zone_path, seuil_expe_filename)) as f:
  67. zones_threshold.append(int(f.readline()))
  68. possible_x, possible_y = possible_point_zone
  69. # get all images of folder
  70. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  71. number_scene_image = len(scene_images)
  72. for id_img, img_path in enumerate(scene_images):
  73. current_img = Image.open(img_path)
  74. img = np.array(current_img)
  75. for generation in range(p_number):
  76. p_x, p_y = (random.randrange(possible_x), random.randrange(possible_y))
  77. # extract random zone into scene image
  78. extracted_img = img[p_y:(p_y + zone_height), p_x:(p_x + zone_width)]
  79. extracted_img.shape
  80. pil_extracted_img = Image.fromarray(extracted_img)
  81. # coordinate of specific zone, hence use threshold of zone
  82. if p_x % zone_width == 0 and p_y % zone_height == 0:
  83. zone_index = math.floor(p_x / zone_width) + math.floor(p_y / zone_height) * nb_x_parts
  84. final_threshold = int(zones_threshold[zone_index])
  85. else:
  86. # get zone identifiers of this new zones (from endpoints)
  87. p_top_left = (p_x, p_y)
  88. p_top_right = (p_x + zone_width, p_y)
  89. p_bottom_right = (p_x + zone_width, p_y + zone_height)
  90. p_bottom_left = (p_x, p_y + zone_height)
  91. points = [p_top_left, p_top_right, p_bottom_right, p_bottom_left]
  92. p_zones_indices = []
  93. # for each points get threshold information
  94. for p in points:
  95. x, y = p
  96. zone_index = math.floor(x / zone_width) + math.floor(y / zone_height) * nb_x_parts
  97. p_zones_indices.append(zone_index)
  98. p_thresholds = np.array(zones_threshold)[p_zones_indices]
  99. # get proportions of pixels of img into each zone
  100. overlaps = []
  101. p_x_max = p_x + zone_width
  102. p_y_max = p_y + zone_height
  103. for index, zone_index in enumerate(p_zones_indices):
  104. x_zone = (zone_index % nb_x_parts) * zone_width
  105. y_zone = (math.floor(zone_index / nb_x_parts)) * zone_height
  106. x_max_zone = x_zone + zone_width
  107. y_max_zone = y_zone + zone_height
  108. # computation of overlap
  109. # x_overlap = max(0, min(rect1.right, rect2.right) - max(rect1.left, rect2.left))
  110. # y_overlap = max(0, min(rect1.bottom, rect2.bottom) - max(rect1.top, rect2.top))
  111. x_overlap = max(0, min(x_max_zone, p_x_max) - max(x_zone, p_x))
  112. y_overlap = max(0, min(y_max_zone, p_y_max) - max(y_zone, p_y))
  113. overlapArea = x_overlap * y_overlap
  114. overlaps.append(overlapArea)
  115. overlapSum = sum(overlaps)
  116. proportions = [item / overlapSum for item in overlaps]
  117. final_threshold = 0
  118. for index, proportion in enumerate(proportions):
  119. final_threshold += proportion * p_thresholds[index]
  120. final_threshold = int(final_threshold)
  121. # save image into new scene folder
  122. current_image_postfix = dt.get_scene_image_postfix(img_path)
  123. # prepare output img name
  124. label_img = (int(current_image_postfix) < final_threshold)
  125. extracted_image_name = dt.get_scene_image_prefix(img_path) + '_' + str(generation) + '_x' + str(p_x) + '_y' + str(p_y) + '_label' + str(int(label_img))
  126. # if wished add of rotations images with same final threshold (increase data)
  127. # write new line into global .csv ('threshold', 'filepath')
  128. if p_rotation:
  129. # do rotations and save
  130. rotations = [0, 90, 180, 270]
  131. for rotation in rotations:
  132. rotated_img_name = extracted_image_name + 'rot' + str(rotation) + '_' + current_image_postfix + cfg.scene_image_extension
  133. rotated_img_path = os.path.join(output_scene_path, rotated_img_name)
  134. saved_rotated_img_path = os.path.join(folder_scene, rotated_img_name)
  135. rotated_img = pil_extracted_img.rotate(rotation)
  136. rotated_img.save(rotated_img_path)
  137. csv_line = folder_scene + ';' + str(final_threshold) + ';' + str(int(current_image_postfix)) + ';' + str(int(label_img)) + ';' + saved_rotated_img_path + '\n'
  138. with open(output_dataset_filename_path, 'a') as f:
  139. f.write(csv_line)
  140. else:
  141. extracted_image_name += current_image_postfix + cfg.scene_image_extension
  142. extracted_image_path = os.path.join(output_scene_path, extracted_image_name)
  143. saved_extracted_image_path = os.path.join(output_scene_path, extracted_image_name)
  144. pil_extracted_img.save(extracted_image_path)
  145. csv_line = folder_scene + ';' + str(final_threshold) + ';' + str(int(current_image_postfix)) + ';' + str(int(label_img)) + ';' + saved_extracted_image_path + '\n'
  146. with open(output_dataset_filename_path, 'a') as f:
  147. f.write(csv_line)
  148. print(folder_scene + " - " + "{0:.2f}".format(((id_img * p_number + generation) + 1) / (p_number * number_scene_image) * 100.) + "%")
  149. sys.stdout.write("\033[F")
  150. if __name__== "__main__":
  151. main()