display_svd_data_scene.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. import math
  14. from PIL import Image
  15. from ipfml import processing, metrics, utils
  16. import ipfml.iqa.fr as fr_iqa
  17. from skimage import color
  18. import matplotlib as mpl
  19. import matplotlib.pyplot as plt
  20. from modules.utils.data import get_svd_data
  21. from modules.utils import config as cfg
  22. # getting configuration information
  23. config_filename = cfg.config_filename
  24. zone_folder = cfg.zone_folder
  25. min_max_filename = cfg.min_max_filename_extension
  26. # define all scenes values
  27. scenes_list = cfg.scenes_names
  28. scenes_indices = cfg.scenes_indices
  29. choices = cfg.normalization_choices
  30. path = cfg.dataset_path
  31. zones = cfg.zones_indices
  32. seuil_expe_filename = cfg.seuil_expe_filename
  33. metric_choices = cfg.metric_choices_labels
  34. max_nb_bits = 8
  35. display_error = False
  36. def display_svd_values(p_scene, p_interval, p_indices, p_metric, p_mode, p_step, p_norm, 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_ylim, ylim choice to better display of data
  46. @return nothing
  47. """
  48. max_value_svd = 0
  49. min_value_svd = sys.maxsize
  50. image_indices = []
  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. data_min_max_filename = os.path.join(path, p_metric + min_max_filename)
  57. # go ahead each scenes
  58. for id_scene, folder_scene in enumerate(scenes):
  59. if p_scene == folder_scene:
  60. scene_path = os.path.join(path, folder_scene)
  61. config_file_path = os.path.join(scene_path, config_filename)
  62. with open(config_file_path, "r") as config_file:
  63. last_image_name = config_file.readline().strip()
  64. prefix_image_name = config_file.readline().strip()
  65. start_index_image = config_file.readline().strip()
  66. end_index_image = config_file.readline().strip()
  67. step_counter = int(config_file.readline().strip())
  68. # construct each zones folder name
  69. zones_folder = []
  70. # get zones list info
  71. for index in zones:
  72. index_str = str(index)
  73. if len(index_str) < 2:
  74. index_str = "0" + index_str
  75. current_zone = "zone"+index_str
  76. zones_folder.append(current_zone)
  77. images_data = []
  78. images_indices = []
  79. threshold_learned_zones = []
  80. for id, zone_folder in enumerate(zones_folder):
  81. # get threshold information
  82. zone_path = os.path.join(scene_path, zone_folder)
  83. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  84. # open treshold path and get this information
  85. with open(path_seuil, "r") as seuil_file:
  86. threshold_learned = int(seuil_file.readline().strip())
  87. threshold_learned_zones.append(threshold_learned)
  88. current_counter_index = int(start_index_image)
  89. end_counter_index = int(end_index_image)
  90. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  91. threshold_image_found = False
  92. file_path = os.path.join(scene_path, prefix_image_name + "{}.png")
  93. svd_data = []
  94. while(current_counter_index <= end_counter_index):
  95. current_counter_index_str = str(current_counter_index)
  96. while len(start_index_image) > len(current_counter_index_str):
  97. current_counter_index_str = "0" + current_counter_index_str
  98. image_path = file_path.format(str(current_counter_index_str))
  99. img = Image.open(image_path)
  100. svd_values = get_svd_data(p_metric, img)
  101. if p_norm:
  102. svd_values = svd_values[begin_data:end_data]
  103. #svd_values = np.asarray([math.log(x) for x in svd_values])
  104. # update min max values
  105. min_value = svd_values.min()
  106. max_value = svd_values.max()
  107. if min_value < min_value_svd:
  108. min_value_svd = min_value
  109. if max_value > min_value_svd:
  110. max_value_svd = max_value
  111. # keep in memory used data
  112. if current_counter_index % p_step == 0:
  113. if current_counter_index >= begin_index and current_counter_index <= end_index:
  114. images_indices.append(current_counter_index_str)
  115. svd_data.append(svd_values)
  116. if threshold_mean < int(current_counter_index) and not threshold_image_found:
  117. threshold_image_found = True
  118. threshold_image_zone = current_counter_index_str
  119. current_counter_index += step_counter
  120. print('%.2f%%' % (current_counter_index / end_counter_index * 100))
  121. sys.stdout.write("\033[F")
  122. # all indices of picture to plot
  123. print(images_indices)
  124. for id, data in enumerate(svd_data):
  125. current_data = data
  126. if not p_norm:
  127. current_data = current_data[begin_data:end_data]
  128. if p_mode == 'svdn':
  129. current_data = utils.normalize_arr(current_data)
  130. if p_mode == 'svdne':
  131. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  132. images_data.append(current_data)
  133. # display all data using matplotlib (configure plt)
  134. #fig = plt.figure(figsize=(30, 22))
  135. fig, ax = plt.subplots(figsize=(30, 22))
  136. ax.set_facecolor('#F9F9F9')
  137. #fig.patch.set_facecolor('#F9F9F9')
  138. ax.tick_params(labelsize=22)
  139. #plt.rc('xtick', labelsize=22)
  140. #plt.rc('ytick', labelsize=22)
  141. #plt.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=24)
  142. ax.set_ylabel('Component values', fontsize=30)
  143. ax.set_xlabel('Vector features', fontsize=30)
  144. for id, data in enumerate(images_data):
  145. p_label = p_scene + '_' + str(images_indices[id])
  146. if images_indices[id] == threshold_image_zone:
  147. ax.plot(data, label=p_label + " (threshold mean)", lw=4, color='red')
  148. else:
  149. ax.plot(data, label=p_label)
  150. plt.legend(bbox_to_anchor=(0.65, 0.98), loc=2, borderaxespad=0.2, fontsize=24)
  151. start_ylim, end_ylim = p_ylim
  152. #ax.set_ylim(start_ylim, end_ylim)
  153. plot_name = p_scene + '_' + p_metric + '_' + str(p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
  154. plt.savefig(plot_name, facecolor=ax.get_facecolor())
  155. def main():
  156. parser = argparse.ArgumentParser(description="Display SVD data of scene")
  157. parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  158. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  159. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  160. parser.add_argument('--metric', type=str, help='Metric data choice', choices=metric_choices)
  161. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  162. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  163. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  164. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  165. args = parser.parse_args()
  166. p_scene = scenes_list[scenes_indices.index(args.scene)]
  167. p_indices = list(map(int, args.indices.split(',')))
  168. p_interval = list(map(int, args.interval.split(',')))
  169. p_metric = args.metric
  170. p_mode = args.mode
  171. p_step = args.step
  172. p_norm = args.norm
  173. p_ylim = list(map(int, args.ylim.split(',')))
  174. display_svd_values(p_scene, p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_ylim)
  175. if __name__== "__main__":
  176. main()