display_scenes_zones.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. import random
  5. import time
  6. import json
  7. # image processing imports
  8. from PIL import Image
  9. from skimage import color
  10. import matplotlib.pyplot as plt
  11. from data_attributes import get_svd_data
  12. from ipfml.processing import segmentation, transform, compression
  13. from ipfml import utils
  14. # modules and config imports
  15. sys.path.insert(0, '') # trick to enable import of main folder module
  16. import custom_config as cfg
  17. from modules.utils import data as dt
  18. # variables and parameters
  19. zone_folder = cfg.zone_folder
  20. min_max_filename = cfg.min_max_filename_extension
  21. # define all scenes values
  22. scenes_list = cfg.scenes_names
  23. scenes_indices = cfg.scenes_indices
  24. norm_choices = cfg.normalization_choices
  25. path = cfg.dataset_path
  26. zones = cfg.zones_indices
  27. seuil_expe_filename = cfg.seuil_expe_filename
  28. features_choices = cfg.features_choices_labels
  29. def display_data_scenes(data_type, p_scene, p_kind):
  30. """
  31. @brief Method which displays data from scene
  32. @param data_type, feature choice
  33. @param scene, scene choice
  34. @param mode, normalization choice
  35. @return nothing
  36. """
  37. scenes = os.listdir(path)
  38. # remove min max file from scenes folder
  39. scenes = [s for s in scenes if min_max_filename not in s]
  40. # go ahead each scenes
  41. for folder_scene in scenes:
  42. if p_scene == folder_scene:
  43. print(folder_scene)
  44. scene_path = os.path.join(path, folder_scene)
  45. # construct each zones folder name
  46. zones_folder = []
  47. # get zones list info
  48. for index in zones:
  49. index_str = str(index)
  50. if len(index_str) < 2:
  51. index_str = "0" + index_str
  52. current_zone = "zone"+index_str
  53. zones_folder.append(current_zone)
  54. zones_images_data = []
  55. threshold_info = []
  56. # get all images of folder
  57. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  58. start_image_path = scene_images[0]
  59. end_image_path = scene_images[-1]
  60. start_quality_image = dt.get_scene_image_quality(scene_images[0])
  61. end_quality_image = dt.get_scene_image_quality(scene_images[-1])
  62. for id_zone, zone_folder in enumerate(zones_folder):
  63. zone_path = os.path.join(scene_path, zone_folder)
  64. # get threshold information
  65. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  66. # open treshold path and get this information
  67. with open(path_seuil, "r") as seuil_file:
  68. threshold_learned = int(seuil_file.readline().strip())
  69. threshold_image_found = False
  70. for img_path in scene_images:
  71. current_quality_image = dt.get_scene_image_quality(img_path)
  72. if threshold_learned < int(current_quality_image) and not threshold_image_found:
  73. threshold_image_found = True
  74. threshold_image_path = img_path
  75. threshold_image = dt.get_scene_image_postfix(img_path)
  76. threshold_info.append(threshold_image)
  77. # all indexes of picture to plot
  78. images_path = [start_image_path, threshold_image_path, end_image_path]
  79. images_data = []
  80. for img_path in images_path:
  81. current_img = Image.open(img_path)
  82. img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))
  83. # getting expected block id
  84. block = img_blocks[id_zone]
  85. data = get_svd_data(data_type, block)
  86. ##################
  87. # Data mode part #
  88. ##################
  89. # modify data depending mode
  90. if p_kind == 'svdn':
  91. data = utils.normalize_arr(data)
  92. if p_kind == 'svdne':
  93. path_min_max = os.path.join(path, data_type + min_max_filename)
  94. with open(path_min_max, 'r') as f:
  95. min_val = float(f.readline())
  96. max_val = float(f.readline())
  97. data = utils.normalize_arr_with_range(data, min_val, max_val)
  98. # append of data
  99. images_data.append(data)
  100. zones_images_data.append(images_data)
  101. fig=plt.figure(figsize=(8, 8))
  102. fig.suptitle(data_type + " values for " + p_scene + " scene (normalization : " + p_kind + ")", fontsize=20)
  103. for id, data in enumerate(zones_images_data):
  104. fig.add_subplot(4, 4, (id + 1))
  105. plt.plot(data[0], label='Noisy_' + start_quality_image)
  106. plt.plot(data[1], label='Threshold_' + threshold_info[id])
  107. plt.plot(data[2], label='Reference_' + end_quality_image)
  108. plt.ylabel(data_type + ' SVD, ZONE_' + str(id + 1), fontsize=18)
  109. plt.xlabel('Vector features', fontsize=18)
  110. plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.2, fontsize=18)
  111. plt.ylim(0, 0.1)
  112. plt.show()
  113. def main():
  114. parser = argparse.ArgumentParser(description="Display zones curves of feature on scene ")
  115. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  116. parser.add_argument('--scene', type=str, help='scene index to use', choices=scenes_indices)
  117. parser.add_argument('--kind', type=str, help='Kind of normalization level wished', choices=norm_choices)
  118. args = parser.parse_args()
  119. p_feature = args.feature
  120. p_kind = args.kind
  121. p_scene = scenes_list[scenes_indices.index(args.scene)]
  122. display_data_scenes(p_feature, p_scene, p_kind)
  123. if __name__== "__main__":
  124. main()