123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import sys, os, argparse, json
- import numpy as np
- from keras.models import model_from_json
- from sklearn.externals import joblib
- from ipfml import processing, utils
- from PIL import Image
- sys.path.insert(0, '')
- import custom_config as cfg
- from data_attributes import get_image_features
- path = cfg.dataset_path
- min_max_ext = cfg.min_max_filename_extension
- features_choices = cfg.features_choices_labels
- normalization_choices = cfg.normalization_choices
- custom_min_max_folder = cfg.min_max_custom_folder
- def main():
-
- parser = argparse.ArgumentParser(description="Script which detects if an image is noisy or not using specific model")
- parser.add_argument('--image', type=str, help='Image path')
- parser.add_argument('--model', type=str, help='.joblib or .json file (sklearn or keras model)')
- parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=normalization_choices)
- parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
- parser.add_argument('--custom', type=str, help='Name of custom min max file if use of renormalization of data', default=False)
- args = parser.parse_args()
- p_img_file = args.image
- p_model_file = args.model
- p_mode = args.mode
- p_feature = args.feature
- p_custom = args.custom
-
- model = joblib.load(p_model_file)
-
- selected_indices = [i for i in np.arange(len(model.support_)) if model.support_[i] == True]
-
- img = Image.open(p_img_file)
- data = get_image_features(p_feature, img)
-
- if p_custom:
- test_data = data[selected_indices]
- if p_mode == 'svdne':
-
- min_max_file_path = os.path.join(custom_min_max_folder, p_custom)
-
- with open(min_max_file_path, 'r') as f:
- min_val = float(f.readline().replace('\n', ''))
- max_val = float(f.readline().replace('\n', ''))
- test_data = utils.normalize_arr_with_range(test_data, min_val, max_val)
- if p_mode == 'svdn':
- test_data = utils.normalize_arr(test_data)
- else:
-
- if p_mode == 'svdne':
-
- min_max_file_path = os.path.join(path, p_feature + min_max_ext)
-
- with open(min_max_file_path, 'r') as f:
- min_val = float(f.readline().replace('\n', ''))
- max_val = float(f.readline().replace('\n', ''))
- l_values = utils.normalize_arr_with_range(data, min_val, max_val)
- elif p_mode == 'svdn':
- l_values = utils.normalize_arr(data)
- else:
- l_values = data
- test_data = data[selected_indices]
-
- prediction = model.estimator_.predict([test_data])[0]
-
- print(prediction)
- if __name__== "__main__":
- main()
|