display_scenes_zones.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. def display_data_scenes(data_type, p_scene, p_kind):
  31. """
  32. @brief Method which displays data from scene
  33. @param data_type, metric choice
  34. @param scene, scene choice
  35. @param mode, normalization choice
  36. @return nothing
  37. """
  38. scenes = os.listdir(path)
  39. # remove min max file from scenes folder
  40. scenes = [s for s in scenes if min_max_filename not in s]
  41. # go ahead each scenes
  42. for id_scene, folder_scene in enumerate(scenes):
  43. if p_scene == folder_scene:
  44. print(folder_scene)
  45. scene_path = os.path.join(path, folder_scene)
  46. config_file_path = os.path.join(scene_path, config_filename)
  47. with open(config_file_path, "r") as config_file:
  48. last_image_name = config_file.readline().strip()
  49. prefix_image_name = config_file.readline().strip()
  50. start_index_image = config_file.readline().strip()
  51. end_index_image = config_file.readline().strip()
  52. step_counter = int(config_file.readline().strip())
  53. # construct each zones folder name
  54. zones_folder = []
  55. # get zones list info
  56. for index in zones:
  57. index_str = str(index)
  58. if len(index_str) < 2:
  59. index_str = "0" + index_str
  60. current_zone = "zone"+index_str
  61. zones_folder.append(current_zone)
  62. zones_images_data = []
  63. threshold_info = []
  64. for id_zone, zone_folder in enumerate(zones_folder):
  65. zone_path = os.path.join(scene_path, zone_folder)
  66. current_counter_index = int(start_index_image)
  67. end_counter_index = int(end_index_image)
  68. # get threshold information
  69. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  70. # open treshold path and get this information
  71. with open(path_seuil, "r") as seuil_file:
  72. seuil_learned = int(seuil_file.readline().strip())
  73. threshold_image_found = False
  74. while(current_counter_index <= end_counter_index and not threshold_image_found):
  75. if seuil_learned < int(current_counter_index):
  76. current_counter_index_str = str(current_counter_index)
  77. while len(start_index_image) > len(current_counter_index_str):
  78. current_counter_index_str = "0" + current_counter_index_str
  79. threshold_image_found = True
  80. threshold_image_zone = current_counter_index_str
  81. threshold_info.append(threshold_image_zone)
  82. current_counter_index += step_counter
  83. # all indexes of picture to plot
  84. images_indexes = [start_index_image, threshold_image_zone, end_index_image]
  85. images_data = []
  86. print(images_indexes)
  87. for index in images_indexes:
  88. img_path = os.path.join(scene_path, prefix_image_name + index + ".png")
  89. current_img = Image.open(img_path)
  90. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  91. # getting expected block id
  92. block = img_blocks[id_zone]
  93. # get data from mode
  94. # Here you can add the way you compute data
  95. if data_type == 'lab':
  96. block_file_path = '/tmp/lab_img.png'
  97. block.save(block_file_path)
  98. data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
  99. if data_type == 'mscn_revisited':
  100. img_mscn_revisited = processing.rgb_to_mscn(block)
  101. # save tmp as img
  102. img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
  103. mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
  104. img_output.save(mscn_revisited_file_path)
  105. img_block = Image.open(mscn_revisited_file_path)
  106. # extract from temp image
  107. data = metrics.get_SVD_s(img_block)
  108. if data_type == 'mscn':
  109. img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
  110. img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
  111. img_mscn_norm = processing.normalize_2D_arr(img_mscn)
  112. img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
  113. data = metrics.get_SVD_s(img_mscn_gray)
  114. if data_type == 'low_bits_6':
  115. low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 63)
  116. # extract from temp image
  117. data = metrics.get_SVD_s(low_bits_6)
  118. if data_type == 'low_bits_5':
  119. low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 31)
  120. # extract from temp image
  121. data = metrics.get_SVD_s(low_bits_5)
  122. if data_type == 'low_bits_4':
  123. low_bits_4 = processing.rgb_to_LAB_L_low_bits(block)
  124. # extract from temp image
  125. data = metrics.get_SVD_s(low_bits_4)
  126. if data_type == 'low_bits_3':
  127. low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 7)
  128. # extract from temp image
  129. data = metrics.get_SVD_s(low_bits_3)
  130. if data_type == 'low_bits_2':
  131. low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 3)
  132. # extract from temp image
  133. data = metrics.get_SVD_s(low_bits_2)
  134. ##################
  135. # Data mode part #
  136. ##################
  137. # modify data depending mode
  138. if p_kind == 'svdn':
  139. data = processing.normalize_arr(data)
  140. if p_kind == 'svdne':
  141. path_min_max = os.path.join(path, data_type + min_max_filename)
  142. with open(path_min_max, 'r') as f:
  143. min_val = float(f.readline())
  144. max_val = float(f.readline())
  145. data = processing.normalize_arr_with_range(data, min_val, max_val)
  146. # append of data
  147. images_data.append(data)
  148. zones_images_data.append(images_data)
  149. fig=plt.figure(figsize=(8, 8))
  150. fig.suptitle(data_type + " values for " + p_scene + " scene (normalization : " + p_kind + ")", fontsize=20)
  151. for id, data in enumerate(zones_images_data):
  152. fig.add_subplot(4, 4, (id + 1))
  153. plt.plot(data[0], label='Noisy_' + start_index_image)
  154. plt.plot(data[1], label='Threshold_' + threshold_info[id])
  155. plt.plot(data[2], label='Reference_' + end_index_image)
  156. plt.ylabel(data_type + ' SVD, ZONE_' + str(id + 1), fontsize=18)
  157. plt.xlabel('Vector features', fontsize=18)
  158. plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.2, fontsize=18)
  159. plt.ylim(0, 0.1)
  160. plt.show()
  161. def main():
  162. if len(sys.argv) <= 1:
  163. print('Run with default parameters...')
  164. print('python generate_all_data.py --metric all --scene A --kind svdn')
  165. sys.exit(2)
  166. try:
  167. opts, args = getopt.getopt(sys.argv[1:], "hm:s:k", ["help=", "metric=", "scene=", "kind="])
  168. except getopt.GetoptError:
  169. # print help information and exit:
  170. print('python generate_all_data.py --metric all --scene A --kind svdn')
  171. sys.exit(2)
  172. for o, a in opts:
  173. if o == "-h":
  174. print('python generate_all_data.py --metric all --scene A --kind svdn')
  175. sys.exit()
  176. elif o in ("-m", "--metric"):
  177. p_metric = a
  178. if p_metric != 'all' and p_metric not in metric_choices:
  179. assert False, "Invalid metric choice"
  180. elif o in ("-s", "--scene"):
  181. p_scene = a
  182. if p_scene not in scenes_indexes:
  183. assert False, "Invalid metric choice"
  184. else:
  185. p_scene = scenes_list[scenes_indexes.index(p_scene)]
  186. elif o in ("-k", "--kind"):
  187. p_kind = a
  188. if p_kind not in choices:
  189. assert False, "Invalid metric choice"
  190. else:
  191. assert False, "unhandled option"
  192. display_data_scenes(p_metric, p_scene, p_kind)
  193. if __name__== "__main__":
  194. main()