display_svd_zone_scene.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. # image processing imports
  5. from PIL import Image
  6. import matplotlib.pyplot as plt
  7. from ipfml.processing import segmentation
  8. import ipfml.iqa.fr as fr_iqa
  9. from ipfml import utils
  10. # modules and config imports
  11. sys.path.insert(0, '') # trick to enable import of main folder module
  12. import custom_config as cfg
  13. from modules.utils import data as dt
  14. from data_attributes import get_image_features
  15. # getting configuration information
  16. zone_folder = cfg.zone_folder
  17. min_max_filename = cfg.min_max_filename_extension
  18. # define all scenes values
  19. scenes_list = cfg.scenes_names
  20. scenes_indices = cfg.scenes_indices
  21. choices = cfg.normalization_choices
  22. path = cfg.dataset_path
  23. zones = cfg.zones_indices
  24. seuil_expe_filename = cfg.seuil_expe_filename
  25. features_choices = cfg.features_choices_labels
  26. generic_output_file_svd = '_random.csv'
  27. max_nb_bits = 8
  28. min_value_interval = sys.maxsize
  29. max_value_interval = 0
  30. def get_min_max_value_interval(_scene, _interval, _feature):
  31. global min_value_interval, max_value_interval
  32. scenes = os.listdir(path)
  33. # remove min max file from scenes folder
  34. scenes = [s for s in scenes if min_max_filename not in s]
  35. for folder_scene in scenes:
  36. # only take care of current scene
  37. if folder_scene == _scene:
  38. scene_path = os.path.join(path, folder_scene)
  39. zones_folder = []
  40. # create zones list
  41. for index in zones:
  42. index_str = str(index)
  43. if len(index_str) < 2:
  44. index_str = "0" + index_str
  45. zones_folder.append("zone"+index_str)
  46. for zone_folder in zones_folder:
  47. zone_path = os.path.join(scene_path, zone_folder)
  48. data_filename = _feature + "_svd" + generic_output_file_svd
  49. data_file_path = os.path.join(zone_path, data_filename)
  50. # getting number of line and read randomly lines
  51. f = open(data_file_path)
  52. lines = f.readlines()
  53. # check if user select current scene and zone to be part of training data set
  54. for line in lines:
  55. begin, end = _interval
  56. line_data = line.split(';')
  57. features = line_data[begin+1:end+1]
  58. features = [float(m) for m in features]
  59. min_value = min(features)
  60. max_value = max(features)
  61. if min_value < min_value_interval:
  62. min_value_interval = min_value
  63. if max_value > max_value_interval:
  64. max_value_interval = max_value
  65. def display_svd_values(p_scene, p_interval, p_indices, p_zone, p_feature, p_mode, p_step, p_norm, p_ylim):
  66. """
  67. @brief Method which gives information about svd curves from zone of picture
  68. @param p_scene, scene expected to show svd values
  69. @param p_interval, interval [begin, end] of svd data to display
  70. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  71. @param p_zone, zone's identifier of picture
  72. @param p_feature, feature computed to show
  73. @param p_mode, normalization's mode
  74. @param p_step, step of images indices
  75. @param p_norm, normalization or not of selected svd data
  76. @param p_ylim, ylim choice to better display of data
  77. @return nothing
  78. """
  79. scenes = os.listdir(path)
  80. # remove min max file from scenes folder
  81. scenes = [s for s in scenes if min_max_filename not in s]
  82. begin_data, end_data = p_interval
  83. begin_index, end_index = p_indices
  84. data_min_max_filename = os.path.join(path, p_feature + min_max_filename)
  85. # go ahead each scenes
  86. for folder_scene in scenes:
  87. if p_scene == folder_scene:
  88. scene_path = os.path.join(path, folder_scene)
  89. # construct each zones folder name
  90. zones_folder = []
  91. # get zones list info
  92. for index in zones:
  93. index_str = str(index)
  94. if len(index_str) < 2:
  95. index_str = "0" + index_str
  96. current_zone = "zone"+index_str
  97. zones_folder.append(current_zone)
  98. zones_images_data = []
  99. images_path = []
  100. zone_folder = zones_folder[p_zone]
  101. zone_path = os.path.join(scene_path, zone_folder)
  102. # get threshold information
  103. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  104. # open treshold path and get this information
  105. with open(path_seuil, "r") as seuil_file:
  106. seuil_learned = int(seuil_file.readline().strip())
  107. threshold_image_found = False
  108. # get all images of folder
  109. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  110. # for each images
  111. for img_path in scene_images:
  112. current_quality_image = dt.get_scene_image_quality(img_path)
  113. if current_quality_image % p_step == 0:
  114. if current_quality_image >= begin_index and current_quality_image <= end_index:
  115. images_path.append(dt.get_scene_image_postfix(img_path))
  116. if seuil_learned < current_quality_image and not threshold_image_found:
  117. threshold_image_found = True
  118. threshold_image_zone = dt.get_scene_image_postfix(img_path)
  119. for img_path in images_path:
  120. current_img = Image.open(img_path)
  121. img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))
  122. # getting expected block id
  123. block = img_blocks[p_zone]
  124. # get data from mode
  125. # Here you can add the way you compute data
  126. data = get_image_features(p_feature, block)
  127. # TODO : improve part of this code to get correct min / max values
  128. if p_norm:
  129. data = data[begin_data:end_data]
  130. ##################
  131. # Data mode part #
  132. ##################
  133. if p_mode == 'svdne':
  134. # getting max and min information from min_max_filename
  135. if not p_norm:
  136. with open(data_min_max_filename, 'r') as f:
  137. min_val = float(f.readline())
  138. max_val = float(f.readline())
  139. else:
  140. min_val = min_value_interval
  141. max_val = max_value_interval
  142. data = utils.normalize_arr_with_range(data, min_val, max_val)
  143. if p_mode == 'svdn':
  144. data = utils.normalize_arr(data)
  145. if not p_norm:
  146. zones_images_data.append(data[begin_data:end_data])
  147. else:
  148. zones_images_data.append(data)
  149. plt.title(p_scene + ' scene interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + ']' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
  150. plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
  151. plt.xlabel('Vector features', fontsize=16)
  152. for id, data in enumerate(zones_images_data):
  153. p_label = p_scene + "_" + images_path[id]
  154. if images_path[id] == threshold_image_zone:
  155. plt.plot(data, label=p_label, lw=4, color='red')
  156. else:
  157. plt.plot(data, label=p_label)
  158. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  159. start_ylim, end_ylim = p_ylim
  160. plt.ylim(start_ylim, end_ylim)
  161. plt.show()
  162. def main():
  163. parser = argparse.ArgumentParser(description="Display SVD data of scene zone")
  164. parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  165. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  166. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  167. parser.add_argument('--zone', type=int, help='Zone to display', choices=list(range(0, 16)))
  168. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  169. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  170. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  171. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  172. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  173. args = parser.parse_args()
  174. p_scene = scenes_list[scenes_indices.index(args.scene)]
  175. p_indices = list(map(int, args.indices.split(',')))
  176. p_interval = list(map(int, args.interval.split(',')))
  177. p_zone = args.zone
  178. p_feature = args.feature
  179. p_mode = args.mode
  180. p_step = args.step
  181. p_norm = args.norm
  182. p_ylim = list(map(int, args.ylim.split(',')))
  183. display_svd_values(p_scene, p_interval, p_indices, p_zone, p_feature, p_mode, p_step, p_norm, p_ylim)
  184. if __name__== "__main__":
  185. main()