display_svd_zone_scene.py 7.8 KB

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