display_svd_zone_scene.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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, getopt
  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. from skimage import color
  16. import matplotlib.pyplot as plt
  17. from modules.utils.data import get_svd_data
  18. from modules.utils import config as cfg
  19. # getting configuration information
  20. config_filename = cfg.config_filename
  21. zone_folder = cfg.zone_folder
  22. min_max_filename = cfg.min_max_filename_extension
  23. # define all scenes values
  24. scenes_list = cfg.scenes_names
  25. scenes_indices = cfg.scenes_indices
  26. choices = cfg.normalization_choices
  27. path = cfg.dataset_path
  28. zones = cfg.zones_indices
  29. seuil_expe_filename = cfg.seuil_expe_filename
  30. metric_choices = cfg.metric_choices_labels
  31. max_nb_bits = 8
  32. def display_svd_values(p_scene, p_interval, p_indices, p_zone, p_metric, p_mode, p_step, p_norm, p_ylim):
  33. """
  34. @brief Method which gives information about svd curves from zone of picture
  35. @param p_scene, scene expected to show svd values
  36. @param p_interval, interval [begin, end] of svd data to display
  37. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  38. @param p_zone, zone's identifier of picture
  39. @param p_metric, metric computed to show
  40. @param p_mode, normalization's mode
  41. @param p_step, step of images indices
  42. @param p_norm, normalization or not of selected svd data
  43. @param p_ylim, ylim choice to better display of data
  44. @return nothing
  45. """
  46. scenes = os.listdir(path)
  47. # remove min max file from scenes folder
  48. scenes = [s for s in scenes if min_max_filename not in s]
  49. begin_data, end_data = p_interval
  50. begin_index, end_index = p_indices
  51. data_min_max_filename = os.path.join(path, p_metric + min_max_filename)
  52. # go ahead each scenes
  53. for id_scene, folder_scene in enumerate(scenes):
  54. if p_scene == folder_scene:
  55. scene_path = os.path.join(path, folder_scene)
  56. config_file_path = os.path.join(scene_path, config_filename)
  57. with open(config_file_path, "r") as config_file:
  58. last_image_name = config_file.readline().strip()
  59. prefix_image_name = config_file.readline().strip()
  60. start_index_image = config_file.readline().strip()
  61. end_index_image = config_file.readline().strip()
  62. step_counter = int(config_file.readline().strip())
  63. # construct each zones folder name
  64. zones_folder = []
  65. # get zones list info
  66. for index in zones:
  67. index_str = str(index)
  68. if len(index_str) < 2:
  69. index_str = "0" + index_str
  70. current_zone = "zone"+index_str
  71. zones_folder.append(current_zone)
  72. zones_images_data = []
  73. images_indices = []
  74. zone_folder = zones_folder[p_zone]
  75. zone_path = os.path.join(scene_path, zone_folder)
  76. current_counter_index = int(start_index_image)
  77. end_counter_index = int(end_index_image)
  78. # get threshold information
  79. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  80. # open treshold path and get this information
  81. with open(path_seuil, "r") as seuil_file:
  82. seuil_learned = int(seuil_file.readline().strip())
  83. threshold_image_found = False
  84. while(current_counter_index <= end_counter_index):
  85. current_counter_index_str = str(current_counter_index)
  86. while len(start_index_image) > len(current_counter_index_str):
  87. current_counter_index_str = "0" + current_counter_index_str
  88. if current_counter_index % p_step == 0:
  89. if current_counter_index >= begin_index and current_counter_index <= end_index:
  90. images_indices.append(current_counter_index_str)
  91. if seuil_learned < int(current_counter_index) and not threshold_image_found:
  92. threshold_image_found = True
  93. threshold_image_zone = current_counter_index_str
  94. current_counter_index += step_counter
  95. # all indices of picture to plot
  96. print(images_indices)
  97. for index in images_indices:
  98. img_path = os.path.join(scene_path, prefix_image_name + str(index) + ".png")
  99. current_img = Image.open(img_path)
  100. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  101. # getting expected block id
  102. block = img_blocks[p_zone]
  103. # get data from mode
  104. # Here you can add the way you compute data
  105. data = get_svd_data(p_metric, block)
  106. if p_norm:
  107. data = data[begin_data:end_data]
  108. ##################
  109. # Data mode part #
  110. ##################
  111. if p_mode == 'svdne':
  112. # getting max and min information from min_max_filename
  113. with open(data_min_max_filename, 'r') as f:
  114. min_val = float(f.readline())
  115. max_val = float(f.readline())
  116. data = utils.normalize_arr_with_range(data, min_val, max_val)
  117. if p_mode == 'svdn':
  118. data = utils.normalize_arr(data)
  119. if not p_norm:
  120. zones_images_data.append(data[begin_data:end_data])
  121. else:
  122. zones_images_data.append(data)
  123. 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=20)
  124. plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
  125. plt.xlabel('Vector features', fontsize=16)
  126. for id, data in enumerate(zones_images_data):
  127. p_label = p_scene + "_" + images_indices[id]
  128. if images_indices[id] == threshold_image_zone:
  129. plt.plot(data, label=p_label, lw=4, color='red')
  130. else:
  131. plt.plot(data, label=p_label)
  132. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  133. start_ylim, end_ylim = p_ylim
  134. plt.ylim(start_ylim, end_ylim)
  135. plt.show()
  136. def main():
  137. # by default p_step value is 10 to enable all photos
  138. p_step = 10
  139. p_norm = 0
  140. p_ylim = (0, 1)
  141. if len(sys.argv) <= 1:
  142. print('Run with default parameters...')
  143. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  144. sys.exit(2)
  145. try:
  146. opts, args = getopt.getopt(sys.argv[1:], "hs:i:i:z:l:m:s:n:y", ["help=", "scene=", "interval=", "indices=", "zone=", "metric=", "mode=", "step=", "norm=", "ylim="])
  147. except getopt.GetoptError:
  148. # print help information and exit:
  149. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  150. sys.exit(2)
  151. for o, a in opts:
  152. if o == "-h":
  153. print('python display_svd_zone_scene.py --scene A --interval "0,200" --indices "0, 900" --zone 3 --metric lab --mode svdne --step 50 --norm 0 --ylim "0, 0.1"')
  154. sys.exit()
  155. elif o in ("-s", "--scene"):
  156. p_scene = a
  157. if p_scene not in scenes_indices:
  158. assert False, "Invalid scene choice"
  159. else:
  160. p_scene = scenes_list[scenes_indices.index(p_scene)]
  161. elif o in ("-i", "--interval"):
  162. p_interval = list(map(int, a.split(',')))
  163. elif o in ("-i", "--indices"):
  164. p_indices = list(map(int, a.split(',')))
  165. elif o in ("-z", "--zone"):
  166. p_zone = int(a)
  167. elif o in ("-m", "--metric"):
  168. p_metric = a
  169. if p_metric not in metric_choices:
  170. assert False, "Invalid metric choice"
  171. elif o in ("-m", "--mode"):
  172. p_mode = a
  173. if p_mode not in choices:
  174. assert False, "Invalid normalization choice, expected ['svd', 'svdn', 'svdne']"
  175. elif o in ("-s", "--step"):
  176. p_step = int(a)
  177. elif o in ("-n", "--norm"):
  178. p_norm = int(a)
  179. elif o in ("-y", "--ylim"):
  180. p_ylim = list(map(float, a.split(',')))
  181. else:
  182. assert False, "unhandled option"
  183. display_svd_values(p_scene, p_interval, p_indices, p_zone, p_metric, p_mode, p_step, p_norm, p_ylim)
  184. if __name__== "__main__":
  185. main()