generate_data_augmentation_zones.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # main imports
  2. import os, sys
  3. import argparse
  4. import pickle
  5. import random
  6. import numpy as np
  7. import math
  8. # image processing imports
  9. from PIL import Image
  10. from ipfml.processing import transform, segmentation
  11. from ipfml import utils
  12. # modules imports
  13. sys.path.insert(0, '') # trick to enable import of main folder module
  14. import custom_config as cfg
  15. from modules.utils import data as dt
  16. import utils as utils_functions
  17. # getting configuration information
  18. zone_folder = cfg.zone_folder
  19. min_max_filename = cfg.min_max_filename_extension
  20. # define all scenes values
  21. scenes_list = cfg.scenes_names
  22. scenes_indexes = cfg.scenes_indices
  23. path = cfg.dataset_path
  24. zones = cfg.zones_indices
  25. seuil_expe_filename = cfg.seuil_expe_filename
  26. output_data_folder = cfg.output_data_folder
  27. image_scene_size = cfg.image_scene_size
  28. image_zone_size = cfg.image_zone_size
  29. possible_point_zone = cfg.possible_point_zone
  30. def main():
  31. parser = argparse.ArgumentParser(description="Compute and prepare data augmentation of scenes")
  32. parser.add_argument('--data', type=str, help="object filename saved using pickle", required=True)
  33. parser.add_argument('--scene', type=str, help="scene name to display click information", required=True, choices=cfg.scenes_names)
  34. parser.add_argument('--n', type=int, help="number of clics per zone wished")
  35. parser.add_argument('--images', type=int, help="number of images (with estimated thresholds) wished by scene")
  36. parser.add_argument('--output', type=str, help="output file with new thresholds data")
  37. args = parser.parse_args()
  38. p_data = args.data
  39. p_scene = args.scene
  40. p_n = args.n
  41. p_images = args.images
  42. p_output = args.output
  43. # load data extracted by zones
  44. fileObject = open(p_data, 'rb')
  45. scenes_data = pickle.load(fileObject)
  46. # get clicks data of specific scene
  47. scene_data = scenes_data[p_scene]
  48. # getting image zone size and usefull information
  49. zone_width, zone_height = image_zone_size
  50. scene_width, scene_height = image_scene_size
  51. nb_x_parts = math.floor(scene_width / zone_width)
  52. # get scenes list
  53. scenes = os.listdir(path)
  54. # remove min max file from scenes folder
  55. scenes = [s for s in scenes if min_max_filename not in s]
  56. # go ahead each scenes in order to get threshold
  57. for folder_scene in scenes:
  58. scene_path = os.path.join(path, folder_scene)
  59. # construct each zones folder name
  60. zones_folder = []
  61. zones_threshold = []
  62. # get zones list info
  63. for index in zones:
  64. index_str = str(index)
  65. if len(index_str) < 2:
  66. index_str = "0" + index_str
  67. current_zone = "zone"+index_str
  68. zones_folder.append(current_zone)
  69. zone_path = os.path.join(scene_path, current_zone)
  70. with open(os.path.join(zone_path, seuil_expe_filename)) as f:
  71. zones_threshold.append(int(f.readline()))
  72. # generate a certain number of images
  73. for i in range(p_images):
  74. ###########################################
  75. # Compute weighted threshold if necessary #
  76. ###########################################
  77. ##############################
  78. # 1. Get random point from possible position
  79. ##############################
  80. possible_x, possible_y = possible_point_zone
  81. p_x, p_y = (random.randrange(possible_x), random.randrange(possible_y))
  82. ##############################
  83. # 2. Get zone indices of this point (or only one zone if `%` 200)
  84. ##############################
  85. # coordinate of specific zone, hence use threshold of zone
  86. if p_x % zone_width == 0 and p_y % zone_height == 0:
  87. zone_index = utils_functions.get_zone_index(p_x, p_y)
  88. final_threshold = int(zones_threshold[zone_index])
  89. else:
  90. # get zone identifiers of this new zones (from endpoints)
  91. p_top_left = (p_x, p_y)
  92. p_top_right = (p_x + zone_width, p_y)
  93. p_bottom_right = (p_x + zone_width, p_y + zone_height)
  94. p_bottom_left = (p_x, p_y + zone_height)
  95. points = [p_top_left, p_top_right, p_bottom_right, p_bottom_left]
  96. p_zones_indices = []
  97. # for each points get threshold information
  98. for p in points:
  99. x, y = p
  100. zone_index = utils_functions.get_zone_index(x, y)
  101. p_zones_indices.append(zone_index)
  102. # 2.3. Compute area of intersected zones (and weights)
  103. # get proportions of pixels of img into each zone
  104. overlaps = []
  105. p_x_max = p_x + zone_width
  106. p_y_max = p_y + zone_height
  107. for index, zone_index in enumerate(p_zones_indices):
  108. x_zone = (zone_index % nb_x_parts) * zone_width
  109. y_zone = (math.floor(zone_index / nb_x_parts)) * zone_height
  110. x_max_zone = x_zone + zone_width
  111. y_max_zone = y_zone + zone_height
  112. # computation of overlap
  113. # x_overlap = max(0, min(rect1.right, rect2.right) - max(rect1.left, rect2.left))
  114. # y_overlap = max(0, min(rect1.bottom, rect2.bottom) - max(rect1.top, rect2.top))
  115. x_overlap = max(0, min(x_max_zone, p_x_max) - max(x_zone, p_x))
  116. y_overlap = max(0, min(y_max_zone, p_y_max) - max(y_zone, p_y))
  117. overlapArea = x_overlap * y_overlap
  118. overlaps.append(overlapArea)
  119. overlapSum = sum(overlaps)
  120. # area weights are saved into proportions
  121. proportions = [item / overlapSum for item in overlaps]
  122. # 2.4. Count number of clicks present into each zones intersected (and weights)
  123. # 2.5. Compute final threshold of `x` and `y` using `3` and `4` steps
  124. p_thresholds = np.array(zones_threshold)[p_zones_indices]
  125. # 3. Save this new entry into .csv file (scene_name; x; y; threshold)
  126. if __name__== "__main__":
  127. main()