predict_noisy_image_svd_mscn.py 2.9 KB

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