display_scenes_zones_shifted.py 6.8 KB

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