display_scenes_zones.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. metric_choices = ['lab', 'mscn', 'mscn_revisited', 'low_bits_2', 'low_bits_3', 'low_bits_4', 'low_bits_5', 'low_bits_6']
  29. def display_data_scenes(data_type, p_scene, p_kind):
  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. if data_type == 'lab':
  93. block_file_path = '/tmp/lab_img.png'
  94. block.save(block_file_path)
  95. data = image_processing.get_LAB_L_SVD_s(Image.open(block_file_path))
  96. if data_type == 'mscn_revisited':
  97. img_mscn_revisited = image_processing.rgb_to_mscn(block)
  98. # save tmp as img
  99. img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
  100. mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
  101. img_output.save(mscn_revisited_file_path)
  102. img_block = Image.open(mscn_revisited_file_path)
  103. # extract from temp image
  104. data = metrics.get_SVD_s(img_block)
  105. if data_type == 'mscn':
  106. img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
  107. img_mscn = image_processing.calculate_mscn_coefficients(img_gray, 7)
  108. img_mscn_norm = image_processing.normalize_2D_arr(img_mscn)
  109. img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
  110. data = metrics.get_SVD_s(img_mscn_gray)
  111. if data_type == 'low_bits_6':
  112. low_bits_6 = image_processing.rgb_to_LAB_L_low_bits(block, 63)
  113. # extract from temp image
  114. data = metrics.get_SVD_s(low_bits_6)
  115. if data_type == 'low_bits_5':
  116. low_bits_5 = image_processing.rgb_to_LAB_L_low_bits(block, 31)
  117. # extract from temp image
  118. data = metrics.get_SVD_s(low_bits_5)
  119. if data_type == 'low_bits_4':
  120. low_bits_4 = image_processing.rgb_to_LAB_L_low_bits(block)
  121. # extract from temp image
  122. data = metrics.get_SVD_s(low_bits_4)
  123. if data_type == 'low_bits_3':
  124. low_bits_3 = image_processing.rgb_to_LAB_L_low_bits(block, 7)
  125. # extract from temp image
  126. data = metrics.get_SVD_s(low_bits_3)
  127. if data_type == 'low_bits_2':
  128. low_bits_2 = image_processing.rgb_to_LAB_L_low_bits(block, 3)
  129. # extract from temp image
  130. data = metrics.get_SVD_s(low_bits_2)
  131. ##################
  132. # Data mode part #
  133. ##################
  134. # modify data depending mode
  135. if p_kind == 'svdn':
  136. data = image_processing.normalize_arr(data)
  137. if p_kind == 'svdne':
  138. path_min_max = os.path.join(path, data_type + min_max_filename)
  139. with open(path_min_max, 'r') as f:
  140. min_val = float(f.readline())
  141. max_val = float(f.readline())
  142. data = image_processing.normalize_arr_with_range(data, min_val, max_val)
  143. # append of data
  144. images_data.append(data)
  145. zones_images_data.append(images_data)
  146. fig=plt.figure(figsize=(8, 8))
  147. fig.suptitle(data_type + " values for " + p_scene + " scene (normalization : " + p_kind + ")", fontsize=20)
  148. for id, data in enumerate(zones_images_data):
  149. fig.add_subplot(4, 4, (id + 1))
  150. plt.plot(data[0], label='Noisy_' + start_index_image)
  151. plt.plot(data[1], label='Threshold_' + threshold_info[id])
  152. plt.plot(data[2], label='Reference_' + end_index_image)
  153. plt.ylabel(data_type + ' SVD, ZONE_' + str(id + 1), fontsize=18)
  154. plt.xlabel('Vector features', fontsize=18)
  155. plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.2, fontsize=18)
  156. plt.ylim(0, 0.1)
  157. plt.show()
  158. def main():
  159. if len(sys.argv) <= 1:
  160. print('Run with default parameters...')
  161. print('python generate_all_data.py --metric all --scene A --kind svdn')
  162. sys.exit(2)
  163. try:
  164. opts, args = getopt.getopt(sys.argv[1:], "hm:s:k", ["help=", "metric=", "scene=", "kind="])
  165. except getopt.GetoptError:
  166. # print help information and exit:
  167. print('python generate_all_data.py --metric all --scene A --kind svdn')
  168. sys.exit(2)
  169. for o, a in opts:
  170. if o == "-h":
  171. print('python generate_all_data.py --metric all --scene A --kind svdn')
  172. sys.exit()
  173. elif o in ("-m", "--metric"):
  174. p_metric = a
  175. if p_metric != 'all' and p_metric not in metric_choices:
  176. assert False, "Invalid metric choice"
  177. elif o in ("-s", "--scene"):
  178. p_scene = a
  179. if p_scene not in scenes_indexes:
  180. assert False, "Invalid metric choice"
  181. else:
  182. p_scene = scenes_list[scenes_indexes.index(p_scene)]
  183. elif o in ("-k", "--kind"):
  184. p_kind = a
  185. if p_kind not in choices:
  186. assert False, "Invalid metric choice"
  187. else:
  188. assert False, "unhandled option"
  189. display_data_scenes(p_metric, p_scene, p_kind)
  190. if __name__== "__main__":
  191. main()