display_svd_zone_scene.py 7.8 KB

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