generate_all_data.py 8.1 KB

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