generate_reconstructed_data.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Jun 19 11:47:42 2019
  5. @author: jbuisine
  6. """
  7. import sys, os, argparse
  8. import numpy as np
  9. import random
  10. import time
  11. import json
  12. from PIL import Image
  13. from ipfml import processing, metrics, utils
  14. from skimage import color
  15. from modules.utils import config as cfg
  16. from preprocessing_functions import svd_reconstruction
  17. # getting configuration information
  18. config_filename = cfg.config_filename
  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_indexes = cfg.scenes_indices
  24. choices = cfg.normalization_choices
  25. path = cfg.dataset_path
  26. zones = cfg.zones_indices
  27. seuil_expe_filename = cfg.seuil_expe_filename
  28. metric_choices = cfg.metric_choices_labels
  29. output_data_folder = cfg.output_data_folder
  30. generic_output_file_svd = '_random.csv'
  31. def generate_data_svd(data_type, interval):
  32. """
  33. @brief Method which generates all .csv files from scenes
  34. @param data_type, metric choice
  35. @param interval, interval choice used by reconstructed method
  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. begin, end = interval
  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. # construct each zones folder name
  54. zones_folder = []
  55. metrics_folder = []
  56. zones_threshold = []
  57. # get zones list info
  58. for index in zones:
  59. index_str = str(index)
  60. if len(index_str) < 2:
  61. index_str = "0" + index_str
  62. current_zone = "zone"+index_str
  63. zones_folder.append(current_zone)
  64. zone_path = os.path.join(scene_path, current_zone)
  65. with open(os.path.join(zone_path, cfg.seuil_expe_filename)) as f:
  66. zones_threshold.append(int(f.readline()))
  67. # custom path for metric
  68. metric_path = os.path.join(zone_path, data_type)
  69. if not os.path.exists(metric_path):
  70. os.makedirs(metric_path)
  71. # custom path for interval of reconstruction and metric
  72. metric_interval_path = os.path.join(metric_path, str(begin) + "_" + str(end))
  73. metrics_folder.append(metric_interval_path)
  74. if not os.path.exists(metric_interval_path):
  75. os.makedirs(metric_interval_path)
  76. # create for each zone the labels folder
  77. labels = [cfg.not_noisy_folder, cfg.noisy_folder]
  78. for label in labels:
  79. label_folder = os.path.join(metric_interval_path, label)
  80. if not os.path.exists(label_folder):
  81. os.makedirs(label_folder)
  82. current_counter_index = int(start_index_image)
  83. end_counter_index = int(end_index_image)
  84. # for each images
  85. while(current_counter_index <= end_counter_index):
  86. current_counter_index_str = str(current_counter_index)
  87. while len(start_index_image) > len(current_counter_index_str):
  88. current_counter_index_str = "0" + current_counter_index_str
  89. img_path = os.path.join(scene_path, prefix_image_name + current_counter_index_str + ".png")
  90. current_img = Image.open(img_path)
  91. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  92. for id_block, block in enumerate(img_blocks):
  93. ##########################
  94. # Image computation part #
  95. ##########################
  96. output_block = svd_reconstruction(block, [begin, end])
  97. output_block = np.array(output_block, 'uint8')
  98. # current output image
  99. output_block_img = Image.fromarray(output_block)
  100. label_path = metrics_folder[id_block]
  101. # get label folder for block
  102. if current_counter_index > zones_threshold[id_block]:
  103. label_path = os.path.join(label_path, cfg.not_noisy_folder)
  104. else:
  105. label_path = os.path.join(label_path, cfg.noisy_folder)
  106. rotations = [0, 90, 180, 270]
  107. # rotate image to increase dataset size
  108. for rotation in rotations:
  109. rotated_output_img = output_block_img.rotate(rotation)
  110. output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block]
  111. output_reconstructed_filename = output_reconstructed_filename + '_' + str(rotation) + '.png'
  112. output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)
  113. rotated_output_img.save(output_reconstructed_path)
  114. start_index_image_int = int(start_index_image)
  115. print(data_type + "_" + 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. print('\n')
  119. print("%s_%s : end of data generation\n" % (data_type, interval))
  120. def main():
  121. parser = argparse.ArgumentParser(description="Compute and prepare data of metric of all scenes using specific interval if necessary")
  122. parser.add_argument('--metric', type=str,
  123. help="metric choice in order to compute data (use 'all' if all metrics are needed)",
  124. choices=metric_choices,
  125. required=True)
  126. parser.add_argument('--interval', type=str,
  127. help="interval choice if needed by the compression method",
  128. default='"100, 200"')
  129. args = parser.parse_args()
  130. p_metric = args.metric
  131. p_interval = list(map(int, args.interval.split(',')))
  132. # generate all or specific metric data
  133. if p_metric == 'all':
  134. for m in metric_choices:
  135. generate_data_svd(m, p_interval)
  136. else:
  137. generate_data_svd(p_metric, p_interval)
  138. if __name__== "__main__":
  139. main()