generate_all_data_file.py 6.2 KB

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