display_svd_zone_scene.py 7.6 KB

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