generate_all_data.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, metrics, utils
  16. from skimage import color
  17. from modules.utils import config as cfg
  18. # getting configuration information
  19. zone_folder = cfg.zone_folder
  20. min_max_filename = cfg.min_max_filename_extension
  21. # define all scenes values
  22. scenes_list = cfg.scenes_names
  23. scenes_indices = cfg.scenes_indices
  24. choices = cfg.normalization_choices
  25. path = cfg.generated_folder
  26. zones = cfg.zones_indices
  27. seuil_expe_filename = cfg.seuil_expe_filename
  28. noise_choices = cfg.noise_labels
  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, color, 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. for noise in noise_choices:
  57. noise_path = os.path.join(scene_path, noise)
  58. # getting output filename
  59. if color:
  60. output_svd_filename = data_type + "_color_" + mode + generic_output_file_svd
  61. else:
  62. output_svd_filename = data_type + "_" + mode + generic_output_file_svd
  63. # construct each zones folder name
  64. zones_folder = []
  65. svd_output_files = []
  66. # get zones list info
  67. for index in zones:
  68. index_str = str(index)
  69. if len(index_str) < 2:
  70. index_str = "0" + index_str
  71. current_zone = "zone"+index_str
  72. zones_folder.append(current_zone)
  73. zone_path = os.path.join(noise_path, current_zone)
  74. if not os.path.exists(zone_path):
  75. os.makedirs(zone_path)
  76. svd_file_path = os.path.join(zone_path, output_svd_filename)
  77. # add writer into list
  78. svd_output_files.append(open(svd_file_path, 'w'))
  79. counter_index = 1
  80. while(counter_index < end_counter_index):
  81. if counter_index % picture_step == 0:
  82. counter_index_str = str(counter_index)
  83. if color:
  84. img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_color_" + counter_index_str + ".png")
  85. else:
  86. img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_" + counter_index_str + ".png")
  87. current_img = Image.open(img_path)
  88. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  89. for id_block, block in enumerate(img_blocks):
  90. ###########################
  91. # Metric computation part #
  92. ###########################
  93. data = get_svd_data(data_type, block)
  94. ##################
  95. # Data mode part #
  96. ##################
  97. # modify data depending mode
  98. if mode == 'svdne':
  99. # getting max and min information from min_max_filename
  100. with open(data_min_max_filename, 'r') as f:
  101. min_val = float(f.readline())
  102. max_val = float(f.readline())
  103. data = utils.normalize_arr_with_range(data, min_val, max_val)
  104. if mode == 'svdn':
  105. data = utils.normalize_arr(data)
  106. # save min and max found from dataset in order to normalize data using whole data known
  107. if mode == 'svd':
  108. current_min = data.min()
  109. current_max = data.max()
  110. if current_min < min_val_found:
  111. min_val_found = current_min
  112. if current_max > max_val_found:
  113. max_val_found = current_max
  114. # now write data into current writer
  115. current_file = svd_output_files[id_block]
  116. # add of index
  117. current_file.write(counter_index_str + ';')
  118. for val in data:
  119. current_file.write(str(val) + ";")
  120. current_file.write('\n')
  121. if color:
  122. print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
  123. else:
  124. print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
  125. sys.stdout.write("\033[F")
  126. counter_index += 1
  127. for f in svd_output_files:
  128. f.close()
  129. if color:
  130. print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "Done...")
  131. else:
  132. print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "Done...")
  133. # save current information about min file found
  134. if mode == 'svd':
  135. with open(data_min_max_filename, 'w') as f:
  136. f.write(str(min_val_found) + '\n')
  137. f.write(str(max_val_found) + '\n')
  138. print("%s : end of data generation\n" % mode)
  139. def main():
  140. # default value of p_step
  141. p_step = 10
  142. p_color = 0
  143. if len(sys.argv) <= 1:
  144. print('Run with default parameters...')
  145. print('python generate_all_data.py --metric all --color 0')
  146. print('python generate_all_data.py --metric lab --color 0')
  147. print('python generate_all_data.py --metric lab --color 1 --step 10')
  148. sys.exit(2)
  149. try:
  150. opts, args = getopt.getopt(sys.argv[1:], "hm:s:c", ["help=", "metric=", "step=", "color="])
  151. except getopt.GetoptError:
  152. # print help information and exit:
  153. print('python generate_all_data.py --metric all --color 1 --step 10')
  154. sys.exit(2)
  155. for o, a in opts:
  156. if o == "-h":
  157. print('python generate_all_data.py --metric all --color 1 --step 10')
  158. sys.exit()
  159. elif o in ("-s", "--step"):
  160. p_step = int(a)
  161. elif o in ("-c", "--color"):
  162. p_color = int(a)
  163. elif o in ("-m", "--metric"):
  164. p_metric = a
  165. if p_metric != 'all' and p_metric not in metric_choices:
  166. assert False, "Invalid metric choice"
  167. else:
  168. assert False, "unhandled option"
  169. global picture_step
  170. picture_step = p_step
  171. if picture_step % 10 != 0:
  172. assert False, "Picture step variable needs to be divided by ten"
  173. # generate all or specific metric data
  174. if p_metric == 'all':
  175. for m in metric_choices:
  176. generate_data_svd(m, p_color, 'svd')
  177. generate_data_svd(m, p_color, 'svdn')
  178. generate_data_svd(m, p_color, 'svdne')
  179. else:
  180. generate_data_svd(p_metric, p_color, 'svd')
  181. generate_data_svd(p_metric, p_color, 'svdn')
  182. generate_data_svd(p_metric, p_color, 'svdne')
  183. if __name__== "__main__":
  184. main()