predict_noisy_image_svd_mscn.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import image_processing
  4. from ipfml import metrics
  5. from PIL import Image
  6. import sys, os, getopt
  7. min_max_file_path = 'fichiersSVD_light/mscn_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_mscn.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_mscn.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_nopredict_noisy_image_svd_mscnisy_image.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. img_mscn = image_processing.rgb_to_mscn(img)
  40. # save tmp as img
  41. img_output = Image.fromarray(img_mscn.astype('uint8'), 'L')
  42. mscn_file_path = '/tmp/mscn_img.png'
  43. img_output.save(mscn_file_path)
  44. img_block = Image.open(mscn_file_path)
  45. # extract from temp image
  46. SVD_MSCN = metrics.get_SVD_s(img_block)
  47. # check mode to normalize data
  48. if p_mode == 'svdne':
  49. # need to read min_max_file
  50. file_path = os.path.join(os.path.join(os.path.dirname(__file__),'../'), min_max_file_path)
  51. with open(file_path, 'r') as f:
  52. min = float(f.readline().replace('\n', ''))
  53. max = float(f.readline().replace('\n', ''))
  54. l_values = image_processing.normalize_arr_with_range(SVD_MSCN, min, max)
  55. elif p_mode == 'svdn':
  56. l_values = image_processing.normalize_arr(SVD_MSCN)
  57. else:
  58. l_values = SVD_MSCN
  59. # get interval values
  60. begin, end = p_interval
  61. test_data = l_values[begin:end]
  62. # get prediction of model
  63. prediction = model.predict([test_data])[0]
  64. print(prediction)
  65. if __name__== "__main__":
  66. main()