generate_all_data.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. import random
  5. import time
  6. import json
  7. # image processing imports
  8. from PIL import Image
  9. from skimage import color
  10. from ipfml.processing.segmentation import divide_in_blocks
  11. from ipfml import utils
  12. # modules and config imports
  13. sys.path.insert(0, '') # trick to enable import of main folder module
  14. import custom_config as cfg
  15. from data_attributes import get_image_features
  16. # getting configuration information
  17. zone_folder = cfg.zone_folder
  18. min_max_filename = cfg.min_max_filename_extension
  19. # define all scenes values
  20. scenes_list = cfg.scenes_names
  21. scenes_indices = cfg.scenes_indices
  22. choices = cfg.normalization_choices
  23. path = cfg.generated_folder
  24. zones = cfg.zones_indices
  25. seuil_expe_filename = cfg.seuil_expe_filename
  26. noise_choices = cfg.noise_labels
  27. feature_choices = cfg.features_choices_labels
  28. output_data_folder = cfg.output_data_folder
  29. end_counter_index = cfg.default_number_of_images
  30. generic_output_file_svd = '_random.csv'
  31. picture_step = 10
  32. # avoid calibration data ?
  33. calibration_folder = 'calibration'
  34. def generate_data_svd(data_type, color, mode):
  35. """
  36. @brief Method which generates all .csv files from scenes
  37. @param data_type, feature choice
  38. @param mode, normalization choice
  39. @return nothing
  40. """
  41. scenes = os.listdir(path)
  42. # filter scene
  43. scenes = [s for s in scenes if calibration_folder not in s]
  44. # remove min max file from scenes folder
  45. scenes = [s for s in scenes if min_max_filename not in s]
  46. # keep in memory min and max data found from data_type
  47. min_val_found = sys.maxsize
  48. max_val_found = 0
  49. data_min_max_filename = os.path.join(path, data_type + min_max_filename)
  50. # go ahead each scenes
  51. for id_scene, folder_scene in enumerate(scenes):
  52. print(folder_scene)
  53. scene_path = os.path.join(path, folder_scene)
  54. for noise in noise_choices:
  55. noise_path = os.path.join(scene_path, noise)
  56. # getting output filename
  57. if color:
  58. output_svd_filename = data_type + "_color_" + mode + generic_output_file_svd
  59. else:
  60. output_svd_filename = data_type + "_" + mode + generic_output_file_svd
  61. # construct each zones folder name
  62. zones_folder = []
  63. svd_output_files = []
  64. # get zones list info
  65. for index in zones:
  66. index_str = str(index)
  67. if len(index_str) < 2:
  68. index_str = "0" + index_str
  69. current_zone = "zone"+index_str
  70. zones_folder.append(current_zone)
  71. zone_path = os.path.join(noise_path, current_zone)
  72. if not os.path.exists(zone_path):
  73. os.makedirs(zone_path)
  74. svd_file_path = os.path.join(zone_path, output_svd_filename)
  75. # add writer into list
  76. svd_output_files.append(open(svd_file_path, 'w'))
  77. counter_index = 1
  78. while(counter_index < end_counter_index):
  79. if counter_index % picture_step == 0:
  80. counter_index_str = str(counter_index)
  81. if color:
  82. img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_color_" + counter_index_str + ".png")
  83. else:
  84. img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_" + counter_index_str + ".png")
  85. current_img = Image.open(img_path)
  86. img_blocks = divide_in_blocks(current_img, (200, 200))
  87. for id_block, block in enumerate(img_blocks):
  88. ###########################
  89. # feature computation part #
  90. ###########################
  91. data = get_image_features(data_type, block)
  92. ##################
  93. # Data mode part #
  94. ##################
  95. # modify data depending mode
  96. if mode == 'svdne':
  97. # getting max and min information from min_max_filename
  98. with open(data_min_max_filename, 'r') as f:
  99. min_val = float(f.readline())
  100. max_val = float(f.readline())
  101. data = utils.normalize_arr_with_range(data, min_val, max_val)
  102. if mode == 'svdn':
  103. data = utils.normalize_arr(data)
  104. # save min and max found from dataset in order to normalize data using whole data known
  105. if mode == 'svd':
  106. current_min = data.min()
  107. current_max = data.max()
  108. if current_min < min_val_found:
  109. min_val_found = current_min
  110. if current_max > max_val_found:
  111. max_val_found = current_max
  112. # now write data into current writer
  113. current_file = svd_output_files[id_block]
  114. # add of index
  115. current_file.write(counter_index_str + ';')
  116. for val in data:
  117. current_file.write(str(val) + ";")
  118. current_file.write('\n')
  119. if color:
  120. print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
  121. else:
  122. print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
  123. sys.stdout.write("\033[F")
  124. counter_index += 1
  125. for f in svd_output_files:
  126. f.close()
  127. if color:
  128. print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "Done...")
  129. else:
  130. print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "Done...")
  131. # save current information about min file found
  132. if mode == 'svd':
  133. with open(data_min_max_filename, 'w') as f:
  134. f.write(str(min_val_found) + '\n')
  135. f.write(str(max_val_found) + '\n')
  136. print("%s : end of data generation\n" % mode)
  137. def main():
  138. parser = argparse.ArgumentParser(description="Compute feature on images dataset")
  139. parser.add_argument('--feature', type=str, help='Feature choice (`all` if all features wished)')
  140. parser.add_argument('--color', type=int, help='Specify if image use color or not', default=0)
  141. parser.add_argument('--step', type=int, help='Step of image indices to keep', default=10)
  142. args = parser.parse_args()
  143. param_feature = args.feature
  144. param_color = args.color
  145. param_step = args.step
  146. if param_feature != 'all' and param_feature not in feature_choices:
  147. raise ValueError("Invalid feature choice ", feature_choices)
  148. global picture_step
  149. picture_step = param_step
  150. if picture_step % 10 != 0:
  151. raise ValueError("Picture step variable needs to be divided by ten")
  152. # generate all or specific feature data
  153. if param_feature == 'all':
  154. for m in feature_choices:
  155. generate_data_svd(m, param_color, 'svd')
  156. generate_data_svd(m, param_color, 'svdn')
  157. generate_data_svd(m, param_color, 'svdne')
  158. else:
  159. generate_data_svd(param_feature, param_color, 'svd')
  160. generate_data_svd(param_feature, param_color, 'svdn')
  161. generate_data_svd(param_feature, param_color, 'svdne')
  162. if __name__== "__main__":
  163. main()