display_svd_area_scenes.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 14 21:02:42 2018
  5. @author: jbuisine
  6. """
  7. from __future__ import print_function
  8. import sys, os, argparse
  9. import numpy as np
  10. import random
  11. import time
  12. import json
  13. from PIL import Image
  14. from ipfml import processing, metrics, utils
  15. import ipfml.iqa.fr as fr_iqa
  16. from skimage import color
  17. import matplotlib.pyplot as plt
  18. from modules.utils.data import get_svd_data
  19. from modules.utils import config as cfg
  20. # getting configuration information
  21. config_filename = cfg.config_filename
  22. zone_folder = cfg.zone_folder
  23. min_max_filename = cfg.min_max_filename_extension
  24. # define all scenes values
  25. scenes_list = cfg.scenes_names
  26. scenes_indices = cfg.scenes_indices
  27. choices = cfg.normalization_choices
  28. path = cfg.dataset_path
  29. zones = cfg.zones_indices
  30. seuil_expe_filename = cfg.seuil_expe_filename
  31. metric_choices = cfg.metric_choices_labels
  32. max_nb_bits = 8
  33. integral_area_choices = ['trapz', 'simps']
  34. def get_area_under_curve(p_area, p_data):
  35. noise_method = None
  36. function_name = 'integral_area_' + p_area
  37. try:
  38. area_method = getattr(utils, function_name)
  39. except AttributeError:
  40. raise NotImplementedError("Error `{}` not implement `{}`".format(utils.__name__, function_name))
  41. return area_method(p_data, dx=800)
  42. def display_svd_values(p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_area, p_ylim):
  43. """
  44. @brief Method which gives information about svd curves from zone of picture
  45. @param p_scene, scene expected to show svd values
  46. @param p_interval, interval [begin, end] of svd data to display
  47. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  48. @param p_metric, metric computed to show
  49. @param p_mode, normalization's mode
  50. @param p_norm, normalization or not of selected svd data
  51. @param p_area, area method name to compute area under curve
  52. @param p_ylim, ylim choice to better display of data
  53. @return nothing
  54. """
  55. image_indices = []
  56. scenes = os.listdir(path)
  57. # remove min max file from scenes folder
  58. scenes = [s for s in scenes if min_max_filename not in s]
  59. begin_data, end_data = p_interval
  60. begin_index, end_index = p_indices
  61. data_min_max_filename = os.path.join(path, p_metric + min_max_filename)
  62. # Store all informations about scenes
  63. scenes_area_data = []
  64. scenes_images_indices = []
  65. scenes_threshold_mean = []
  66. # go ahead each scenes
  67. for id_scene, folder_scene in enumerate(scenes):
  68. max_value_svd = 0
  69. min_value_svd = sys.maxsize
  70. scene_path = os.path.join(path, folder_scene)
  71. config_file_path = os.path.join(scene_path, config_filename)
  72. with open(config_file_path, "r") as config_file:
  73. last_image_name = config_file.readline().strip()
  74. prefix_image_name = config_file.readline().strip()
  75. start_index_image = config_file.readline().strip()
  76. end_index_image = config_file.readline().strip()
  77. step_counter = int(config_file.readline().strip())
  78. # construct each zones folder name
  79. zones_folder = []
  80. # get zones list info
  81. for index in zones:
  82. index_str = str(index)
  83. if len(index_str) < 2:
  84. index_str = "0" + index_str
  85. current_zone = "zone"+index_str
  86. zones_folder.append(current_zone)
  87. # store data information for current scene
  88. images_data = []
  89. images_indices = []
  90. threshold_learned_zones = []
  91. for id, zone_folder in enumerate(zones_folder):
  92. # get threshold information
  93. zone_path = os.path.join(scene_path, zone_folder)
  94. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  95. # open treshold path and get this information
  96. with open(path_seuil, "r") as seuil_file:
  97. threshold_learned = int(seuil_file.readline().strip())
  98. threshold_learned_zones.append(threshold_learned)
  99. current_counter_index = int(start_index_image)
  100. end_counter_index = int(end_index_image)
  101. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  102. threshold_image_found = False
  103. scenes_threshold_mean.append(int(threshold_mean / p_step))
  104. file_path = os.path.join(scene_path, prefix_image_name + "{}.png")
  105. svd_data = []
  106. while(current_counter_index <= end_counter_index):
  107. current_counter_index_str = str(current_counter_index)
  108. while len(start_index_image) > len(current_counter_index_str):
  109. current_counter_index_str = "0" + current_counter_index_str
  110. image_path = file_path.format(str(current_counter_index_str))
  111. img = Image.open(image_path)
  112. svd_values = get_svd_data(p_metric, img)
  113. if p_norm:
  114. svd_values = svd_values[begin_data:end_data]
  115. # update min max values
  116. min_value = svd_values.min()
  117. max_value = svd_values.max()
  118. if min_value < min_value_svd:
  119. min_value_svd = min_value
  120. if max_value > min_value_svd:
  121. max_value_svd = max_value
  122. # keep in memory used data
  123. if current_counter_index % p_step == 0:
  124. if current_counter_index >= begin_index and current_counter_index <= end_index:
  125. images_indices.append(current_counter_index_str)
  126. svd_data.append(svd_values)
  127. if threshold_mean < int(current_counter_index) and not threshold_image_found:
  128. threshold_image_found = True
  129. threshold_image_zone = current_counter_index_str
  130. current_counter_index += step_counter
  131. print('%.2f%%' % (current_counter_index / end_counter_index * 100))
  132. sys.stdout.write("\033[F")
  133. # all indices of picture to plot
  134. print("Scene %s : %s" % (folder_scene, images_indices))
  135. scenes_images_indices.append(image_indices)
  136. area_data = []
  137. for id, data in enumerate(svd_data):
  138. current_data = data
  139. if not p_norm:
  140. current_data = current_data[begin_data:end_data]
  141. if p_mode == 'svdn':
  142. current_data = utils.normalize_arr(current_data)
  143. if p_mode == 'svdne':
  144. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  145. images_data.append(current_data)
  146. # not use this script for 'sub_blocks_stats'
  147. current_area = get_area_under_curve(p_area, current_data)
  148. area_data.append(current_area)
  149. scenes_area_data.append(area_data)
  150. # display all data using matplotlib (configure plt)
  151. 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)
  152. plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
  153. plt.xlabel('Vector features', fontsize=16)
  154. plt.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2, fontsize=14)
  155. for id, area_data in enumerate(scenes_area_data):
  156. threshold_id = 0
  157. scene_name = scenes[id]
  158. image_indices = scenes_images_indices[id]
  159. threshold_image_zone = scenes_threshold_mean[id]
  160. p_label = scene_name + '_' + str(images_indices[id])
  161. threshold_id = scenes_threshold_mean[id]
  162. print(p_label)
  163. start_ylim, end_ylim = p_ylim
  164. plt.plot(area_data, label=p_label)
  165. #ax2.set_xticks(range(len(images_indices)))
  166. #ax2.set_xticklabels(list(map(int, images_indices)))
  167. if threshold_id != 0:
  168. print("Plot threshold ", threshold_id)
  169. plt.plot([threshold_id, threshold_id], [np.min(area_data), np.max(area_data)], 'k-', lw=2, color='red')
  170. #start_ylim, end_ylim = p_ylim
  171. #plt.ylim(start_ylim, end_ylim)
  172. plt.show()
  173. def main():
  174. parser = argparse.ArgumentParser(description="Display area under curve on scene")
  175. parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  176. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  177. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  178. parser.add_argument('--metric', type=str, help='Metric data choice', choices=metric_choices)
  179. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  180. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  181. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  182. parser.add_argument('--area', type=int, help='Way of computing area under curve', choices=integral_area_choices)
  183. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  184. args = parser.parse_args()
  185. p_scene = scenes_list[scenes_indices.index(args.scene)]
  186. p_indices = list(map(int, args.indices.split(',')))
  187. p_interval = list(map(int, args.interval.split(',')))
  188. p_metric = args.metric
  189. p_mode = args.mode
  190. p_step = args.step
  191. p_norm = args.norm
  192. p_area = args.area
  193. p_ylim = list(map(int, args.ylim.split(',')))
  194. display_svd_values(p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_area, p_ylim)
  195. if __name__== "__main__":
  196. main()