predict_noisy_image_svd_low_bits_6.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from sklearn.externals import joblib
  2. from ipfml import image_processing
  3. from ipfml import metrics
  4. from PIL import Image
  5. import sys, os, getopt
  6. import numpy as np
  7. min_max_file_path = 'fichiersSVD_light/low_bits_6_min_max_values'
  8. def main():
  9. if len(sys.argv) <= 1:
  10. print('Run with default parameters...')
  11. print('python predict_noisy_image_svd_lab.py --image path/to/xxxx --interval "0,20" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  12. sys.exit(2)
  13. try:
  14. opts, args = getopt.getopt(sys.argv[1:], "hi:t:m:o", ["help=", "image=", "interval=", "model=", "mode="])
  15. except getopt.GetoptError:
  16. # print help information and exit:
  17. print('python predict_noisy_image_svd_lab.py --image path/to/xxxx --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  18. sys.exit(2)
  19. for o, a in opts:
  20. if o == "-h":
  21. print('python predict_noisy_image_svd_lab.py --image path/to/xxxx --interval "xx,xx" --model path/to/xxxx.joblib --mode ["svdn", "svdne"]')
  22. sys.exit()
  23. elif o in ("-i", "--image"):
  24. p_img_file = os.path.join(os.path.join(os.path.dirname(__file__),'../'), a)
  25. elif o in ("-t", "--interval"):
  26. p_interval = list(map(int, a.split(',')))
  27. elif o in ("-m", "--model"):
  28. p_model_file = os.path.join(os.path.join(os.path.dirname(__file__),'../'), a)
  29. elif o in ("-o", "--mode"):
  30. p_mode = a
  31. if p_mode != 'svdn' and p_mode != 'svdne' and p_mode != 'svd':
  32. assert False, "Mode not recognized"
  33. else:
  34. assert False, "unhandled option"
  35. # load of model file
  36. model = joblib.load(p_model_file)
  37. # load image
  38. img = Image.open(p_img_file)
  39. low_bits_6_values = metrics.get_SVD_s(image_processing.rgb_to_LAB_L_low_bits(img, 63))
  40. # check mode to normalize data
  41. if p_mode == 'svdne':
  42. # need to read min_max_file
  43. file_path = os.path.join(os.path.join(os.path.dirname(__file__),'../'), min_max_file_path)
  44. with open(file_path, 'r') as f:
  45. min = float(f.readline().replace('\n', ''))
  46. max = float(f.readline().replace('\n', ''))
  47. l_values = image_processing.normalize_arr_with_range(low_bits_6_values, min, max)
  48. elif p_mode == 'svdn':
  49. l_values = image_processing.normalize_arr(low_bits_6_values)
  50. else:
  51. l_values = low_bits_6_values
  52. # get interval values
  53. begin, end = p_interval
  54. test_data = l_values[begin:end]
  55. # get prediction of model
  56. prediction = model.predict([test_data])[0]
  57. print(prediction)
  58. if __name__== "__main__":
  59. main()