generate_all_data.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 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. from data_attributes import get_svd_data
  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_indexes = cfg.scenes_indices
  22. choices = cfg.normalization_choices
  23. path = cfg.dataset_path
  24. zones = cfg.zones_indices
  25. seuil_expe_filename = cfg.seuil_expe_filename
  26. features_choices = cfg.features_choices_labels
  27. output_data_folder = cfg.output_data_folder
  28. generic_output_file_svd = '_random.csv'
  29. def generate_data_svd(data_type, mode):
  30. """
  31. @brief Method which generates all .csv files from scenes
  32. @param data_type, feature choice
  33. @param mode, normalization choice
  34. @return nothing
  35. """
  36. scenes = os.listdir(path)
  37. # remove min max file from scenes folder
  38. scenes = [s for s in scenes if min_max_filename not in s]
  39. # keep in memory min and max data found from data_type
  40. min_val_found = sys.maxsize
  41. max_val_found = 0
  42. data_min_max_filename = os.path.join(path, data_type + min_max_filename)
  43. # go ahead each scenes
  44. for folder_scene in scenes:
  45. print(folder_scene)
  46. scene_path = os.path.join(path, folder_scene)
  47. # getting output filename
  48. output_svd_filename = data_type + "_" + mode + generic_output_file_svd
  49. # construct each zones folder name
  50. zones_folder = []
  51. svd_output_files = []
  52. # get zones list info
  53. for index in zones:
  54. index_str = str(index)
  55. if len(index_str) < 2:
  56. index_str = "0" + index_str
  57. current_zone = "zone"+index_str
  58. zones_folder.append(current_zone)
  59. zone_path = os.path.join(scene_path, current_zone)
  60. svd_file_path = os.path.join(zone_path, output_svd_filename)
  61. # add writer into list
  62. svd_output_files.append(open(svd_file_path, 'w'))
  63. # get all images of folder
  64. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  65. number_scene_image = len(scene_images)
  66. for id_img, img_path in enumerate(scene_images):
  67. current_image_postfix = dt.get_scene_image_postfix(img_path)
  68. current_img = Image.open(img_path)
  69. img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))
  70. for id_block, block in enumerate(img_blocks):
  71. ###########################
  72. # feature computation part #
  73. ###########################
  74. data = get_svd_data(data_type, block)
  75. ##################
  76. # Data mode part #
  77. ##################
  78. # modify data depending mode
  79. if mode == 'svdne':
  80. # getting max and min information from min_max_filename
  81. with open(data_min_max_filename, 'r') as f:
  82. min_val = float(f.readline())
  83. max_val = float(f.readline())
  84. data = utils.normalize_arr_with_range(data, min_val, max_val)
  85. if mode == 'svdn':
  86. data = utils.normalize_arr(data)
  87. # save min and max found from dataset in order to normalize data using whole data known
  88. if mode == 'svd':
  89. current_min = data.min()
  90. current_max = data.max()
  91. if current_min < min_val_found:
  92. min_val_found = current_min
  93. if current_max > max_val_found:
  94. max_val_found = current_max
  95. # now write data into current writer
  96. current_file = svd_output_files[id_block]
  97. # add of index
  98. current_file.write(current_image_postfix + ';')
  99. for val in data:
  100. current_file.write(str(val) + ";")
  101. current_file.write('\n')
  102. print(data_type + "_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((id_img + 1) / number_scene_image * 100.) + "%")
  103. sys.stdout.write("\033[F")
  104. for f in svd_output_files:
  105. f.close()
  106. print('\n')
  107. # save current information about min file found
  108. if mode == 'svd':
  109. with open(data_min_max_filename, 'w') as f:
  110. f.write(str(min_val_found) + '\n')
  111. f.write(str(max_val_found) + '\n')
  112. print("%s_%s : end of data generation\n" % (data_type, mode))
  113. def main():
  114. parser = argparse.ArgumentParser(description="Compute and prepare data of feature of all scenes (keep in memory min and max value found)")
  115. parser.add_argument('--feature', type=str,
  116. help="feature choice in order to compute data (use 'all' if all features are needed)")
  117. args = parser.parse_args()
  118. p_feature = args.feature
  119. # generate all or specific feature data
  120. if p_feature == 'all':
  121. for m in features_choices:
  122. generate_data_svd(m, 'svd')
  123. generate_data_svd(m, 'svdn')
  124. generate_data_svd(m, 'svdne')
  125. else:
  126. if p_feature not in features_choices:
  127. raise ValueError('Unknown feature choice : ', features_choices)
  128. generate_data_svd(p_feature, 'svd')
  129. generate_data_svd(p_feature, 'svdn')
  130. generate_data_svd(p_feature, 'svdne')
  131. if __name__== "__main__":
  132. main()