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