display_svd_data_scene.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. # image processing imports
  5. from PIL import Image
  6. import matplotlib.pyplot as plt
  7. import ipfml.iqa.fr as fr_iqa
  8. from ipfml import utils
  9. # modules and config imports
  10. sys.path.insert(0, '') # trick to enable import of main folder module
  11. import custom_config as cfg
  12. from modules.utils import data as dt
  13. from data_attributes import get_svd_data
  14. # getting configuration information
  15. zone_folder = cfg.zone_folder
  16. min_max_filename = cfg.min_max_filename_extension
  17. # define all scenes values
  18. scenes_list = cfg.scenes_names
  19. scenes_indices = cfg.scenes_indices
  20. choices = cfg.normalization_choices
  21. path = cfg.dataset_path
  22. zones = cfg.zones_indices
  23. seuil_expe_filename = cfg.seuil_expe_filename
  24. features_choices = cfg.features_choices_labels
  25. max_nb_bits = 8
  26. display_error = False
  27. def display_svd_values(p_scene, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_ylim):
  28. """
  29. @brief Method which gives information about svd curves from zone of picture
  30. @param p_scene, scene expected to show svd values
  31. @param p_interval, interval [begin, end] of svd data to display
  32. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  33. @param p_feature, feature computed to show
  34. @param p_mode, normalization's mode
  35. @param p_norm, normalization or not of selected svd data
  36. @param p_ylim, ylim choice to better display of data
  37. @return nothing
  38. """
  39. max_value_svd = 0
  40. min_value_svd = sys.maxsize
  41. scenes = os.listdir(path)
  42. # remove min max file from scenes folder
  43. scenes = [s for s in scenes if min_max_filename not in s]
  44. begin_data, end_data = p_interval
  45. begin_index, end_index = p_indices
  46. # go ahead each scenes
  47. for folder_scene in scenes:
  48. if p_scene == folder_scene:
  49. scene_path = os.path.join(path, folder_scene)
  50. # construct each zones folder name
  51. zones_folder = []
  52. # get zones list info
  53. for index in zones:
  54. index_str = str(index)
  55. if len(index_str) < 2:
  56. index_str = "0" + index_str
  57. current_zone = "zone"+index_str
  58. zones_folder.append(current_zone)
  59. images_data = []
  60. images_indices = []
  61. threshold_learned_zones = []
  62. # get all images of folder
  63. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  64. number_scene_image = len(scene_images)
  65. for id, zone_folder in enumerate(zones_folder):
  66. # get threshold information
  67. zone_path = os.path.join(scene_path, zone_folder)
  68. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  69. # open treshold path and get this information
  70. with open(path_seuil, "r") as seuil_file:
  71. threshold_learned = int(seuil_file.readline().strip())
  72. threshold_learned_zones.append(threshold_learned)
  73. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  74. threshold_image_found = False
  75. svd_data = []
  76. # for each images
  77. for id_img, img_path in enumerate(scene_images):
  78. current_quality_image = dt.get_scene_image_quality(img_path)
  79. img = Image.open(img_path)
  80. svd_values = get_svd_data(p_feature, img)
  81. if p_norm:
  82. svd_values = svd_values[begin_data:end_data]
  83. #svd_values = np.asarray([math.log(x) for x in svd_values])
  84. # update min max values
  85. min_value = svd_values.min()
  86. max_value = svd_values.max()
  87. if min_value < min_value_svd:
  88. min_value_svd = min_value
  89. if max_value > min_value_svd:
  90. max_value_svd = max_value
  91. # keep in memory used data
  92. if current_quality_image % p_step == 0:
  93. if current_quality_image >= begin_index and current_quality_image <= end_index:
  94. images_indices.append(dt.get_scene_image_postfix(img_path))
  95. svd_data.append(svd_values)
  96. if threshold_mean < current_quality_image and not threshold_image_found:
  97. threshold_image_found = True
  98. threshold_image_zone = current_quality_image
  99. print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
  100. sys.stdout.write("\033[F")
  101. # all indices of picture to plot
  102. print(images_indices)
  103. for id, data in enumerate(svd_data):
  104. current_data = data
  105. if not p_norm:
  106. current_data = current_data[begin_data:end_data]
  107. if p_mode == 'svdn':
  108. current_data = utils.normalize_arr(current_data)
  109. if p_mode == 'svdne':
  110. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  111. images_data.append(current_data)
  112. # display all data using matplotlib (configure plt)
  113. fig, ax = plt.subplots(figsize=(30, 22))
  114. ax.set_facecolor('#F9F9F9')
  115. #fig.patch.set_facecolor('#F9F9F9')
  116. ax.tick_params(labelsize=22)
  117. #plt.rc('xtick', labelsize=22)
  118. #plt.rc('ytick', labelsize=22)
  119. #plt.title(p_scene + ' scene interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + '], ' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=24)
  120. ax.set_ylabel('Component values', fontsize=30)
  121. ax.set_xlabel('Vector features', fontsize=30)
  122. for id, data in enumerate(images_data):
  123. p_label = p_scene + '_' + str(images_indices[id])
  124. if images_indices[id] == threshold_image_zone:
  125. ax.plot(data, label=p_label + " (threshold mean)", lw=4, color='red')
  126. else:
  127. ax.plot(data, label=p_label)
  128. plt.legend(bbox_to_anchor=(0.65, 0.98), loc=2, borderaxespad=0.2, fontsize=24)
  129. #start_ylim, end_ylim = p_ylim
  130. #ax.set_ylim(start_ylim, end_ylim)
  131. plot_name = p_scene + '_' + p_feature + '_' + str(p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
  132. plt.savefig(plot_name, facecolor=ax.get_facecolor())
  133. def main():
  134. parser = argparse.ArgumentParser(description="Display SVD data of scene")
  135. parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  136. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  137. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  138. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  139. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  140. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  141. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  142. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  143. args = parser.parse_args()
  144. p_scene = scenes_list[scenes_indices.index(args.scene)]
  145. p_indices = list(map(int, args.indices.split(',')))
  146. p_interval = list(map(int, args.interval.split(',')))
  147. p_feature = args.feature
  148. p_mode = args.mode
  149. p_step = args.step
  150. p_norm = args.norm
  151. p_ylim = list(map(int, args.ylim.split(',')))
  152. display_svd_values(p_scene, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_ylim)
  153. if __name__== "__main__":
  154. main()