display_svd_area_scenes.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_image_features
  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. integral_area_choices = ['trapz', 'simps']
  27. def get_area_under_curve(p_area, p_data):
  28. function_name = 'integral_area_' + p_area
  29. try:
  30. area_method = getattr(utils, function_name)
  31. except AttributeError:
  32. raise NotImplementedError("Error `{}` not implement `{}`".format(utils.__name__, function_name))
  33. return area_method(p_data, dx=800)
  34. def display_svd_values(p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_area, p_ylim):
  35. """
  36. @brief Method which gives information about svd curves from zone of picture
  37. @param p_interval, interval [begin, end] of svd data to display
  38. @param p_indices, indices to display
  39. @param p_feature, feature computed to show
  40. @param p_mode, normalization's mode
  41. @param p_norm, normalization or not of selected svd data
  42. @param p_area, area method name to compute area under curve
  43. @param p_ylim, ylim choice to better display of data
  44. @return nothing
  45. """
  46. image_indices = []
  47. scenes = os.listdir(path)
  48. # remove min max file from scenes folder
  49. scenes = [s for s in scenes if min_max_filename not in s]
  50. begin_data, end_data = p_interval
  51. begin_index, end_index = p_indices
  52. # Store all informations about scenes
  53. scenes_area_data = []
  54. scenes_images_indices = []
  55. scenes_threshold_mean = []
  56. # go ahead each scenes
  57. for folder_scene in scenes:
  58. max_value_svd = 0
  59. min_value_svd = sys.maxsize
  60. scene_path = os.path.join(path, folder_scene)
  61. # construct each zones folder name
  62. zones_folder = []
  63. # get zones list info
  64. for index in zones:
  65. index_str = str(index)
  66. if len(index_str) < 2:
  67. index_str = "0" + index_str
  68. current_zone = "zone"+index_str
  69. zones_folder.append(current_zone)
  70. # store data information for current scene
  71. images_data = []
  72. images_indices = []
  73. threshold_learned_zones = []
  74. # get all images of folder
  75. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  76. number_scene_image = len(scene_images)
  77. for id, zone_folder in enumerate(zones_folder):
  78. # get threshold information
  79. zone_path = os.path.join(scene_path, zone_folder)
  80. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  81. # open treshold path and get this information
  82. with open(path_seuil, "r") as seuil_file:
  83. threshold_learned = int(seuil_file.readline().strip())
  84. threshold_learned_zones.append(threshold_learned)
  85. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  86. threshold_image_found = False
  87. scenes_threshold_mean.append(int(threshold_mean / p_step))
  88. svd_data = []
  89. # for each images
  90. for id_img, img_path in enumerate(scene_images):
  91. current_quality_image = dt.get_scene_image_quality(img_path)
  92. img = Image.open(img_path)
  93. svd_values = get_image_features(p_metric, img)
  94. if p_norm:
  95. svd_values = svd_values[begin_data:end_data]
  96. # update min max values
  97. min_value = svd_values.min()
  98. max_value = svd_values.max()
  99. if min_value < min_value_svd:
  100. min_value_svd = min_value
  101. if max_value > min_value_svd:
  102. max_value_svd = max_value
  103. # keep in memory used data
  104. if current_quality_image % p_step == 0:
  105. if current_quality_image >= begin_index and current_quality_image <= end_index:
  106. images_indices.append(dt.get_scene_image_postfix(img_path))
  107. svd_data.append(svd_values)
  108. if threshold_mean < current_quality_image and not threshold_image_found:
  109. threshold_image_found = True
  110. print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
  111. sys.stdout.write("\033[F")
  112. # all indices of picture to plot
  113. print("Scene %s : %s" % (folder_scene, images_indices))
  114. scenes_images_indices.append(image_indices)
  115. area_data = []
  116. for id, data in enumerate(svd_data):
  117. current_data = data
  118. if not p_norm:
  119. current_data = current_data[begin_data:end_data]
  120. if p_mode == 'svdn':
  121. current_data = utils.normalize_arr(current_data)
  122. if p_mode == 'svdne':
  123. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  124. images_data.append(current_data)
  125. # not use this script for 'sub_blocks_stats'
  126. current_area = get_area_under_curve(p_area, current_data)
  127. area_data.append(current_area)
  128. scenes_area_data.append(area_data)
  129. # display all data using matplotlib (configure plt)
  130. plt.title('Scenes area interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + ']' + p_metric + ' metric, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
  131. plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
  132. plt.xlabel('Vector features', fontsize=16)
  133. plt.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2, fontsize=14)
  134. for id, area_data in enumerate(scenes_area_data):
  135. threshold_id = 0
  136. scene_name = scenes[id]
  137. image_indices = scenes_images_indices[id]
  138. p_label = scene_name + '_' + str(images_indices[id])
  139. threshold_id = scenes_threshold_mean[id]
  140. print(p_label)
  141. plt.plot(area_data, label=p_label)
  142. #ax2.set_xticks(range(len(images_indices)))
  143. #ax2.set_xticklabels(list(map(int, images_indices)))
  144. if threshold_id != 0:
  145. print("Plot threshold ", threshold_id)
  146. plt.plot([threshold_id, threshold_id], [np.min(area_data), np.max(area_data)], 'k-', lw=2, color='red')
  147. start_ylim, end_ylim = p_ylim
  148. plt.ylim(start_ylim, end_ylim)
  149. plt.show()
  150. def main():
  151. parser = argparse.ArgumentParser(description="Display area under curve on scene")
  152. #parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  153. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  154. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  155. parser.add_argument('--feature', type=str, help='Metric data choice', choices=features_choices)
  156. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  157. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  158. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  159. parser.add_argument('--area', type=int, help='Way of computing area under curve', choices=integral_area_choices)
  160. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  161. args = parser.parse_args()
  162. #p_scene = scenes_list[scenes_indices.index(args.scene)]
  163. p_indices = list(map(int, args.indices.split(',')))
  164. p_interval = list(map(int, args.interval.split(',')))
  165. p_feature = args.feature
  166. p_mode = args.mode
  167. p_step = args.step
  168. p_norm = args.norm
  169. p_area = args.area
  170. p_ylim = list(map(int, args.ylim.split(',')))
  171. display_svd_values(p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_area, p_ylim)
  172. if __name__== "__main__":
  173. main()