generate_all_data.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. zone_folder = cfg.zone_folder
  21. min_max_filename = cfg.min_max_filename_extension
  22. # define all scenes values
  23. scenes_list = cfg.maxwell_scenes_folders
  24. scenes_indexes = cfg.scenes_indices
  25. choices = cfg.normalization_choices
  26. path = cfg.generated_folder
  27. zones = cfg.zones_indices
  28. seuil_expe_filename = cfg.seuil_expe_filename
  29. metric_choices = cfg.metric_choices_labels
  30. output_data_folder = cfg.output_data_folder
  31. end_counter_index = cfg.default_number_of_images
  32. generic_output_file_svd = '_random.csv'
  33. picture_step = 10
  34. # avoid calibration data ?
  35. calibration_folder = 'calibration'
  36. def generate_data_svd(data_type, mode):
  37. """
  38. @brief Method which generates all .csv files from scenes
  39. @param data_type, metric choice
  40. @param mode, normalization choice
  41. @return nothing
  42. """
  43. scenes = os.listdir(path)
  44. # filter scene
  45. scenes = [s for s in scenes if calibration_folder not in s]
  46. # remove min max file from scenes folder
  47. scenes = [s for s in scenes if min_max_filename not in s]
  48. # keep in memory min and max data found from data_type
  49. min_val_found = sys.maxsize
  50. max_val_found = 0
  51. data_min_max_filename = os.path.join(path, data_type + min_max_filename)
  52. # go ahead each scenes
  53. for id_scene, folder_scene in enumerate(scenes):
  54. print(folder_scene)
  55. scene_path = os.path.join(path, folder_scene)
  56. # getting output filename
  57. output_svd_filename = data_type + "_" + mode + generic_output_file_svd
  58. # construct each zones folder name
  59. zones_folder = []
  60. svd_output_files = []
  61. # get zones list info
  62. for index in zones:
  63. index_str = str(index)
  64. if len(index_str) < 2:
  65. index_str = "0" + index_str
  66. current_zone = "zone"+index_str
  67. zones_folder.append(current_zone)
  68. zone_path = os.path.join(scene_path, current_zone)
  69. if not os.path.exists(zone_path):
  70. os.makedirs(zone_path)
  71. svd_file_path = os.path.join(zone_path, output_svd_filename)
  72. # add writer into list
  73. svd_output_files.append(open(svd_file_path, 'w'))
  74. counter_index = 1
  75. while(counter_index <= end_counter_index):
  76. if counter_index % picture_step == 0:
  77. counter_index_str = str(counter_index)
  78. img_path = os.path.join(scene_path, forlder_scene + "_" + counter_index_str + ".png")
  79. current_img = Image.open(img_path)
  80. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  81. for id_block, block in enumerate(img_blocks):
  82. ###########################
  83. # Metric computation part #
  84. ###########################
  85. data = get_svd_data(data_type, block)
  86. ##################
  87. # Data mode part #
  88. ##################
  89. # modify data depending mode
  90. if mode == 'svdne':
  91. # getting max and min information from min_max_filename
  92. with open(data_min_max_filename, 'r') as f:
  93. min_val = float(f.readline())
  94. max_val = float(f.readline())
  95. data = processing.normalize_arr_with_range(data, min_val, max_val)
  96. if mode == 'svdn':
  97. data = processing.normalize_arr(data)
  98. # save min and max found from dataset in order to normalize data using whole data known
  99. if mode == 'svd':
  100. current_min = data.min()
  101. current_max = data.max()
  102. if current_min < min_val_found:
  103. min_val_found = current_min
  104. if current_max > max_val_found:
  105. max_val_found = current_max
  106. # now write data into current writer
  107. current_file = svd_output_files[id_block]
  108. # add of index
  109. current_file.write(counter_index_str + ';')
  110. for val in data:
  111. current_file.write(str(val) + ";")
  112. current_file.write('\n')
  113. start_index_image_int = int(start_index_image)
  114. print(data_type + "_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
  115. sys.stdout.write("\033[F")
  116. counter_index += 1
  117. for f in svd_output_files:
  118. f.close()
  119. print('\n')
  120. # save current information about min file found
  121. if mode == 'svd':
  122. with open(data_min_max_filename, 'w') as f:
  123. f.write(str(min_val_found) + '\n')
  124. f.write(str(max_val_found) + '\n')
  125. print("%s : end of data generation\n" % mode)
  126. def main():
  127. # default value of p_step
  128. p_step = 10
  129. if len(sys.argv) <= 1:
  130. print('Run with default parameters...')
  131. print('python generate_all_data.py --metric all')
  132. print('python generate_all_data.py --metric lab')
  133. print('python generate_all_data.py --metric lab --step 10')
  134. sys.exit(2)
  135. try:
  136. opts, args = getopt.getopt(sys.argv[1:], "hms", ["help=", "metric=", "step="])
  137. except getopt.GetoptError:
  138. # print help information and exit:
  139. print('python generate_all_data.py --metric all --step 10')
  140. sys.exit(2)
  141. for o, a in opts:
  142. if o == "-h":
  143. print('python generate_all_data.py --metric all --step 10')
  144. sys.exit()
  145. elif o in ("-s", "--step"):
  146. p_step = int(a)
  147. elif o in ("-m", "--metric"):
  148. p_metric = a
  149. if p_metric != 'all' and p_metric not in metric_choices:
  150. assert False, "Invalid metric choice"
  151. else:
  152. assert False, "unhandled option"
  153. global picture_step
  154. picture_step = p_step
  155. if picture_step % 10 != 0:
  156. assert False, "Picture step variable needs to be divided by ten"
  157. # generate all or specific metric data
  158. if p_metric == 'all':
  159. for m in metric_choices:
  160. generate_data_svd(m, 'svd')
  161. generate_data_svd(m, 'svdn')
  162. generate_data_svd(m, 'svdne')
  163. else:
  164. generate_data_svd(p_metric, 'svd')
  165. generate_data_svd(p_metric, 'svdn')
  166. generate_data_svd(p_metric, 'svdne')
  167. if __name__== "__main__":
  168. main()