display_svd_data_scene_file.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. import math
  5. # image processing imports
  6. from PIL import Image
  7. import matplotlib.pyplot as plt
  8. import ipfml.iqa.fr as fr_iqa
  9. from ipfml import utils
  10. # modules and config imports
  11. sys.path.insert(0, '') # trick to enable import of main folder module
  12. import custom_config as cfg
  13. from modules.utils import data as dt
  14. from data_attributes import get_image_features
  15. # getting configuration information
  16. zone_folder = cfg.zone_folder
  17. min_max_filename = cfg.min_max_filename_extension
  18. # define all scenes values
  19. scenes_list = cfg.scenes_names
  20. scenes_indices = cfg.scenes_indices
  21. choices = cfg.normalization_choices
  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_thresholds, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_ylim, p_label):
  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. begin_data, end_data = p_interval
  42. begin_index, end_index = p_indices
  43. # go ahead selected scene
  44. scene_path = p_scene
  45. # construct each zones folder name
  46. zones_folder = []
  47. # get zones list info
  48. for index in zones:
  49. index_str = str(index)
  50. if len(index_str) < 2:
  51. index_str = "0" + index_str
  52. current_zone = "zone"+index_str
  53. zones_folder.append(current_zone)
  54. images_data = []
  55. images_indices = []
  56. threshold_learned_zones = []
  57. # get all images of folder
  58. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  59. number_scene_image = len(scene_images)
  60. _, scene_name = os.path.split(p_scene)
  61. threshold_learned_zones = p_thresholds[scene_name]
  62. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  63. threshold_image_found = False
  64. svd_data = []
  65. # for each images
  66. for id_img, img_path in enumerate(scene_images):
  67. current_quality_image = dt.get_scene_image_quality(img_path)
  68. img = Image.open(img_path)
  69. svd_values = get_image_features(p_feature, img)
  70. if p_norm:
  71. svd_values = svd_values[begin_data:end_data]
  72. #svd_values = np.asarray([math.log(x) for x in svd_values])
  73. # update min max values
  74. min_value = svd_values.min()
  75. max_value = svd_values.max()
  76. if min_value < min_value_svd:
  77. min_value_svd = min_value
  78. if max_value > min_value_svd:
  79. max_value_svd = max_value
  80. # keep in memory used data
  81. if current_quality_image % p_step == 0:
  82. if current_quality_image >= begin_index and current_quality_image <= end_index:
  83. images_indices.append(dt.get_scene_image_postfix(img_path))
  84. svd_data.append(svd_values)
  85. if threshold_mean < current_quality_image and not threshold_image_found:
  86. threshold_image_found = True
  87. threshold_image_zone = current_quality_image
  88. print("Quality mean : ", current_quality_image, "\n")
  89. if dt.get_scene_image_postfix(img_path) not in images_indices:
  90. images_indices.append(dt.get_scene_image_postfix(img_path))
  91. print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
  92. sys.stdout.write("\033[F")
  93. # all indices of picture to plot
  94. print(images_indices)
  95. for id, data in enumerate(svd_data):
  96. # current_data = [ math.log10(d + 1.) for d in data ]
  97. # print(current_data)
  98. current_data = data
  99. if not p_norm:
  100. current_data = current_data[begin_data:end_data]
  101. if p_mode == 'svdn':
  102. current_data = utils.normalize_arr(current_data)
  103. if p_mode == 'svdne':
  104. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  105. images_data.append(current_data)
  106. # display all data using matplotlib (configure plt)
  107. fig, ax = plt.subplots(figsize=(30, 22))
  108. ax.set_facecolor('#FFFFFF')
  109. #fig.patch.set_facecolor('#F9F9F9')
  110. ax.tick_params(labelsize=26)
  111. #plt.rc('xtick', labelsize=22)
  112. #plt.rc('ytick', labelsize=22)
  113. #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)
  114. ax.set_ylabel('Component values', fontsize=36)
  115. ax.set_xlabel('Singular value component indices', fontsize=36)
  116. for id, data in enumerate(images_data):
  117. #p_label = p_scene + "_" + images_indices[id]
  118. p_label = images_indices[id] + " samples"
  119. if int(images_indices[id]) == int(threshold_image_zone):
  120. ax.plot(data, label=p_label + " (threshold mean)", lw=8, color='red')
  121. else:
  122. ax.plot(data, label=p_label, lw=4)
  123. plt.legend(bbox_to_anchor=(0.60, 0.98), loc=2, borderaxespad=0.2, fontsize=32)
  124. start_ylim, end_ylim = p_ylim
  125. ax.set_ylim(start_ylim, end_ylim)
  126. plot_name = scene_name + '_' + p_feature + '_' + str(p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
  127. plt.title('Tend of Singular values at different samples of ' + p_label + ' scene', fontsize=40)
  128. plt.savefig(plot_name, transparent=True)
  129. def main():
  130. parser = argparse.ArgumentParser(description="Display SVD data of scene")
  131. parser.add_argument('--scene', type=str, help='scene folder to use', required=True)
  132. parser.add_argument('--thresholds', type=str, help='expected thresholds file', required=True)
  133. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  134. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  135. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  136. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  137. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  138. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  139. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='0,1')
  140. parser.add_argument('--label', type=str, help='output label name', default="")
  141. args = parser.parse_args()
  142. p_scene = args.scene
  143. p_thresholds = args.thresholds
  144. p_indices = list(map(int, args.indices.split(',')))
  145. p_interval = list(map(int, args.interval.split(',')))
  146. p_feature = args.feature
  147. p_mode = args.mode
  148. p_step = args.step
  149. p_norm = args.norm
  150. p_ylim = list(map(float, args.ylim.split(',')))
  151. p_label = args.label
  152. # 1. retrieve human_thresholds
  153. human_thresholds = {}
  154. # extract thresholds
  155. with open(p_thresholds) as f:
  156. thresholds_line = f.readlines()
  157. for line in thresholds_line:
  158. data = line.split(';')
  159. del data[-1] # remove unused last element `\n`
  160. current_scene = data[0]
  161. thresholds_scene = data[1:]
  162. # TODO : check if really necessary
  163. if current_scene != '50_shades_of_grey':
  164. human_thresholds[current_scene] = [ int(threshold) for threshold in thresholds_scene ]
  165. display_svd_values(p_scene, human_thresholds, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_ylim, p_label)
  166. if __name__== "__main__":
  167. main()