predict_seuil_expe.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import image_processing
  4. from PIL import Image
  5. import sys, os, getopt
  6. import subprocess
  7. config_filename = "config"
  8. scenes_path = './fichiersSVD_light'
  9. min_max_filename = 'min_max_values'
  10. seuil_expe_filename = 'seuilExpe'
  11. tmp_filename = '/tmp/img_to_predict.png'
  12. zones = np.arange(16)
  13. def main():
  14. if len(sys.argv) <= 1:
  15. print('Run with default parameters...')
  16. print('python predict_noisy_image.py --interval "0,20" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  17. sys.exit(2)
  18. try:
  19. opts, args = getopt.getopt(sys.argv[1:], "ht:m:o", ["help=", "interval=", "model=", "mode="])
  20. except getopt.GetoptError:
  21. # print help information and exit:
  22. print('python predict_noisy_image.py --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  23. sys.exit(2)
  24. for o, a in opts:
  25. if o == "-h":
  26. print('python predict_noisy_image.py --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  27. sys.exit()
  28. elif o in ("-t", "--interval"):
  29. p_interval = a
  30. elif o in ("-m", "--model"):
  31. p_model_file = a
  32. elif o in ("-o", "--mode"):
  33. p_mode = a
  34. if p_mode != 'svdn' and p_mode != 'svdne':
  35. assert False, "Mode not recognized"
  36. else:
  37. assert False, "unhandled option"
  38. scenes = os.listdir(scenes_path)
  39. if min_max_filename in scenes:
  40. scenes.remove(min_max_filename)
  41. # go ahead each scenes
  42. for id_scene, folder_scene in enumerate(scenes):
  43. print(folder_scene)
  44. scene_path = scenes_path + "/" + folder_scene
  45. with open(scene_path + "/" + config_filename, "r") as config_file:
  46. last_image_name = config_file.readline().strip()
  47. prefix_image_name = config_file.readline().strip()
  48. start_index_image = config_file.readline().strip()
  49. end_index_image = config_file.readline().strip()
  50. step_counter = int(config_file.readline().strip())
  51. seuil_expes = []
  52. seuil_expes_detected = []
  53. seuil_expes_counter = []
  54. seuil_expes_found = []
  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. zone_folder = "zone"+index_str
  61. with open(scene_path + "/" + zone_folder + "/" + seuil_expe_filename) as f:
  62. seuil_expes.append(int(f.readline()))
  63. # Initialize default data to get detected model seuil found
  64. seuil_expes_detected.append(False)
  65. seuil_expes_counter.append(0)
  66. seuil_expes_found.append(0)
  67. for seuil in seuil_expes:
  68. print(seuil)
  69. current_counter_index = int(start_index_image)
  70. end_counter_index = int(end_index_image)
  71. print(current_counter_index)
  72. while(current_counter_index <= end_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. img_path = scene_path + "/" + prefix_image_name + current_counter_index_str + ".png"
  77. print(img_path)
  78. current_img = Image.open(img_path)
  79. img_blocks = image_processing.divide_in_blocks(current_img, (200, 200))
  80. for id_block, block in enumerate(img_blocks):
  81. block.save(tmp_filename)
  82. python_cmd = "python predict_noisy_image_sdv_lab.py --image " + tmp_filename + \
  83. " --interval '" + p_interval + \
  84. "' --model " + p_model_file + \
  85. " --mode " + p_mode
  86. ## call command ##
  87. p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)
  88. (output, err) = p.communicate()
  89. ## Wait for result ##
  90. p_status = p.wait()
  91. prediction = int(output)
  92. print(str(current_counter_index) + "/" + str(seuil_expes[id_block]) + " => " + str(prediction))
  93. current_counter_index += step_counter
  94. # end of scene => display of results
  95. if __name__== "__main__":
  96. main()