generate_all_data.py 5.8 KB

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