generate_all_data_file.py 6.3 KB

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