generate_all_data.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 14 21:02:42 2018
  5. @author: jbuisine
  6. """
  7. from __future__ import print_function
  8. import sys, os, getopt
  9. import numpy as np
  10. import random
  11. import time
  12. import json
  13. from modules.utils.data_type import get_svd_data
  14. from PIL import Image
  15. from ipfml import processing
  16. from ipfml import metrics
  17. from skimage import color
  18. from modules.utils import config as cfg
  19. # getting configuration information
  20. config_filename = cfg.config_filename
  21. zone_folder = cfg.zone_folder
  22. min_max_filename = cfg.min_max_filename_extension
  23. # define all scenes values
  24. scenes_list = cfg.scenes_names
  25. scenes_indexes = cfg.scenes_indices
  26. choices = cfg.normalization_choices
  27. path = cfg.dataset_path
  28. zones = cfg.zones_indices
  29. seuil_expe_filename = cfg.seuil_expe_filename
  30. metric_choices = cfg.metric_choices_labels
  31. output_data_folder = cfg.output_data_folder
  32. generic_output_file_svd = '_random.csv'
  33. picture_step = 10
  34. def generate_data_svd(data_type, mode):
  35. """
  36. @brief Method which generates all .csv files from scenes
  37. @param data_type, metric choice
  38. @param mode, normalization choice
  39. @return nothing
  40. """
  41. scenes = os.listdir(path)
  42. # remove min max file from scenes folder
  43. scenes = [s for s in scenes if min_max_filename not in s]
  44. # keep in memory min and max data found from data_type
  45. min_val_found = sys.maxsize
  46. max_val_found = 0
  47. data_min_max_filename = os.path.join(path, data_type + min_max_filename)
  48. # go ahead each scenes
  49. for id_scene, folder_scene in enumerate(scenes):
  50. print(folder_scene)
  51. scene_path = os.path.join(path, folder_scene)
  52. config_file_path = os.path.join(scene_path, config_filename)
  53. with open(config_file_path, "r") as config_file:
  54. last_image_name = config_file.readline().strip()
  55. prefix_image_name = config_file.readline().strip()
  56. start_index_image = config_file.readline().strip()
  57. end_index_image = config_file.readline().strip()
  58. step_counter = int(config_file.readline().strip())
  59. # getting output filename
  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(scene_path, current_zone)
  72. svd_file_path = os.path.join(zone_path, output_svd_filename)
  73. # add writer into list
  74. svd_output_files.append(open(svd_file_path, 'w'))
  75. current_counter_index = int(start_index_image)
  76. end_counter_index = int(end_index_image)
  77. while(current_counter_index <= end_counter_index):
  78. if current_counter_index % picture_step == 0:
  79. current_counter_index_str = str(current_counter_index)
  80. while len(start_index_image) > len(current_counter_index_str):
  81. current_counter_index_str = "0" + current_counter_index_str
  82. img_path = os.path.join(scene_path, prefix_image_name + current_counter_index_str + ".png")
  83. current_img = Image.open(img_path)
  84. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  85. for id_block, block in enumerate(img_blocks):
  86. ###########################
  87. # Metric computation part #
  88. ###########################
  89. data = get_svd_data(data_type, block)
  90. ##################
  91. # Data mode part #
  92. ##################
  93. # modify data depending mode
  94. if mode == 'svdne':
  95. # getting max and min information from min_max_filename
  96. with open(data_min_max_filename, 'r') as f:
  97. min_val = float(f.readline())
  98. max_val = float(f.readline())
  99. data = processing.normalize_arr_with_range(data, min_val, max_val)
  100. if mode == 'svdn':
  101. data = processing.normalize_arr(data)
  102. # save min and max found from dataset in order to normalize data using whole data known
  103. if mode == 'svd':
  104. current_min = data.min()
  105. current_max = data.max()
  106. if current_min < min_val_found:
  107. min_val_found = current_min
  108. if current_max > max_val_found:
  109. max_val_found = current_max
  110. # now write data into current writer
  111. current_file = svd_output_files[id_block]
  112. # add of index
  113. current_file.write(current_counter_index_str + ';')
  114. for val in data:
  115. current_file.write(str(val) + ";")
  116. current_file.write('\n')
  117. start_index_image_int = int(start_index_image)
  118. print(data_type + "_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((current_counter_index - start_index_image_int) / (end_counter_index - start_index_image_int)* 100.) + "%")
  119. sys.stdout.write("\033[F")
  120. current_counter_index += step_counter
  121. for f in svd_output_files:
  122. f.close()
  123. print('\n')
  124. # save current information about min file found
  125. if mode == 'svd':
  126. with open(data_min_max_filename, 'w') as f:
  127. f.write(str(min_val_found) + '\n')
  128. f.write(str(max_val_found) + '\n')
  129. print("%s : end of data generation\n" % mode)
  130. def main():
  131. # default value of p_step
  132. p_step = 10
  133. if len(sys.argv) <= 1:
  134. print('Run with default parameters...')
  135. print('python generate_all_data.py --metric all')
  136. print('python generate_all_data.py --metric lab')
  137. print('python generate_all_data.py --metric lab --step 10')
  138. sys.exit(2)
  139. try:
  140. opts, args = getopt.getopt(sys.argv[1:], "hms", ["help=", "metric=", "step="])
  141. except getopt.GetoptError:
  142. # print help information and exit:
  143. print('python generate_all_data.py --metric all --step 10')
  144. sys.exit(2)
  145. for o, a in opts:
  146. if o == "-h":
  147. print('python generate_all_data.py --metric all --step 10')
  148. sys.exit()
  149. elif o in ("-s", "--step"):
  150. p_step = int(a)
  151. elif o in ("-m", "--metric"):
  152. p_metric = a
  153. if p_metric != 'all' and p_metric not in metric_choices:
  154. assert False, "Invalid metric choice"
  155. else:
  156. assert False, "unhandled option"
  157. global picture_step
  158. picture_step = p_step
  159. if picture_step % 10 != 0:
  160. assert False, "Picture step variable needs to be divided by ten"
  161. # generate all or specific metric data
  162. if p_metric == 'all':
  163. for m in metric_choices:
  164. generate_data_svd(m, 'svd')
  165. generate_data_svd(m, 'svdn')
  166. generate_data_svd(m, 'svdne')
  167. else:
  168. generate_data_svd(p_metric, 'svd')
  169. generate_data_svd(p_metric, 'svdn')
  170. generate_data_svd(p_metric, 'svdne')
  171. if __name__== "__main__":
  172. main()