display_scenes_zones_shifted.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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
  15. from ipfml import metrics
  16. from skimage import color
  17. import matplotlib.pyplot as plt
  18. from modules.utils import config as cfg
  19. config_filename = cfg.config_filename
  20. zone_folder = cfg.zone_folder
  21. min_max_filename = cfg.min_max_filename_extension
  22. # define all scenes values
  23. scenes_list = cfg.scenes_names
  24. scenes_indexes = cfg.scenes_indices
  25. choices = cfg.normalization_choices
  26. path = cfg.dataset_path
  27. zones = cfg.zones_indices
  28. seuil_expe_filename = cfg.seuil_expe_filename
  29. metric_choices = cfg.metric_choices_labels
  30. max_nb_bits = 8
  31. def display_data_scenes(p_scene, p_bits, p_shifted):
  32. """
  33. @brief Method which generates all .csv files from scenes photos
  34. @param p_scene, scene we want to show values
  35. @param nb_bits, number of bits expected
  36. @param p_shifted, number of bits expected to be shifted
  37. @return nothing
  38. """
  39. scenes = os.listdir(path)
  40. # remove min max file from scenes folder
  41. scenes = [s for s in scenes if min_max_filename not in s]
  42. # go ahead each scenes
  43. for id_scene, folder_scene in enumerate(scenes):
  44. if p_scene == folder_scene:
  45. print(folder_scene)
  46. scene_path = os.path.join(path, folder_scene)
  47. config_file_path = os.path.join(scene_path, config_filename)
  48. with open(config_file_path, "r") as config_file:
  49. last_image_name = config_file.readline().strip()
  50. prefix_image_name = config_file.readline().strip()
  51. start_index_image = config_file.readline().strip()
  52. end_index_image = config_file.readline().strip()
  53. step_counter = int(config_file.readline().strip())
  54. # construct each zones folder name
  55. zones_folder = []
  56. # get zones list info
  57. for index in zones:
  58. index_str = str(index)
  59. if len(index_str) < 2:
  60. index_str = "0" + index_str
  61. current_zone = "zone"+index_str
  62. zones_folder.append(current_zone)
  63. zones_images_data = []
  64. threshold_info = []
  65. for id_zone, zone_folder in enumerate(zones_folder):
  66. zone_path = os.path.join(scene_path, zone_folder)
  67. current_counter_index = int(start_index_image)
  68. end_counter_index = int(end_index_image)
  69. # get threshold information
  70. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  71. # open treshold path and get this information
  72. with open(path_seuil, "r") as seuil_file:
  73. seuil_learned = int(seuil_file.readline().strip())
  74. threshold_image_found = False
  75. while(current_counter_index <= end_counter_index and not threshold_image_found):
  76. if seuil_learned < int(current_counter_index):
  77. current_counter_index_str = str(current_counter_index)
  78. while len(start_index_image) > len(current_counter_index_str):
  79. current_counter_index_str = "0" + current_counter_index_str
  80. threshold_image_found = True
  81. threshold_image_zone = current_counter_index_str
  82. threshold_info.append(threshold_image_zone)
  83. current_counter_index += step_counter
  84. # all indexes of picture to plot
  85. images_indexes = [start_index_image, threshold_image_zone, end_index_image]
  86. images_data = []
  87. print(images_indexes)
  88. for index in images_indexes:
  89. img_path = os.path.join(scene_path, prefix_image_name + index + ".png")
  90. current_img = Image.open(img_path)
  91. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  92. # getting expected block id
  93. block = img_blocks[id_zone]
  94. # get data from mode
  95. # Here you can add the way you compute data
  96. low_bits_block = processing.rgb_to_LAB_L_bits(block, (p_shifted + 1, p_shifted + p_bits + 1))
  97. data = metrics.get_SVD_s(low_bits_block)
  98. ##################
  99. # Data mode part #
  100. ##################
  101. # modify data depending mode
  102. data = processing.normalize_arr(data)
  103. images_data.append(data)
  104. zones_images_data.append(images_data)
  105. fig=plt.figure(figsize=(8, 8))
  106. fig.suptitle('Lab SVD ' + str(p_bits) + ' bits shifted by ' + str(p_shifted) + " for " + p_scene + " scene", fontsize=20)
  107. for id, data in enumerate(zones_images_data):
  108. fig.add_subplot(4, 4, (id + 1))
  109. plt.plot(data[0], label='Noisy_' + start_index_image)
  110. plt.plot(data[1], label='Threshold_' + threshold_info[id])
  111. plt.plot(data[2], label='Reference_' + end_index_image)
  112. plt.ylabel('Lab SVD ' + str(p_bits) + ' bits shifted by ' + str(p_shifted) + ', ZONE_' + str(id + 1), fontsize=14)
  113. plt.xlabel('Vector features', fontsize=16)
  114. plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.2, fontsize=14)
  115. plt.ylim(0, 0.1)
  116. plt.show()
  117. def main():
  118. if len(sys.argv) <= 1:
  119. print('Run with default parameters...')
  120. print('python generate_all_data.py --scene A --bits 3 --shifted 3')
  121. sys.exit(2)
  122. try:
  123. opts, args = getopt.getopt(sys.argv[1:], "hs:b:s", ["help=", "scene=", "bits=", "shifted="])
  124. except getopt.GetoptError:
  125. # print help information and exit:
  126. print('python generate_all_data.py --scene A --bits 3 --shifted 3')
  127. sys.exit(2)
  128. for o, a in opts:
  129. if o == "-h":
  130. print('python generate_all_data.py --scene A --bits 3 --shifted 3')
  131. sys.exit()
  132. elif o in ("-b", "--bits"):
  133. p_bits = int(a)
  134. elif o in ("-s", "--scene"):
  135. p_scene = a
  136. if p_scene not in scenes_indexes:
  137. assert False, "Invalid metric choice"
  138. else:
  139. p_scene = scenes_list[scenes_indexes.index(p_scene)]
  140. elif o in ("-f", "--shifted"):
  141. p_shifted = int(a)
  142. else:
  143. assert False, "unhandled option"
  144. if p_bits + p_shifted > max_nb_bits:
  145. assert False, "Invalid parameters, cannot have bits greater than 8 after shift move"
  146. display_data_scenes(p_scene, p_bits, p_shifted)
  147. if __name__== "__main__":
  148. main()