generate_all_data.py 7.5 KB

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