def plot_k_similar(self): count = 1 m_image_rank_map = {} for image, rank in self.page_ranking.items(): image_path = os.path.join( Path(os.path.dirname(__file__)).parent, self.test_folder_path, image) m_image_rank_map[image_path] = rank count += 1 if count > self.m: break # print(m_image_rank_map) misc.plot_similar_images(m_image_rank_map, text='\nRanking: ')
def plot_subjects(self, main_subject, sub_sub_list, test_image_directory_path): sub_ids_list = [main_subject] sub_sub_similarity_pairs = {} for subject_pair in sub_sub_list: sub_ids_list += [subject_pair[0]] sub_sub_similarity_pairs[subject_pair[0]] = subject_pair[1] if self.images_metadata is None: self.set_images_metadata() filtered_images_metadata = self.images_metadata if self.test_images_list is not None: filtered_images_metadata = filtered_images_metadata[( filtered_images_metadata['imageName'].isin( self.test_images_list))] subject_map = {} for sub_id in sub_ids_list: is_subject_id = filtered_images_metadata['id'] == sub_id subject_map[sub_id] = filtered_images_metadata[is_subject_id] subject_images_list = {} count = 0 for sub_id, data_frame in subject_map.items(): if sub_id == main_subject: subject_images_list[sub_id] = { 'imageList': [ os.path.join(test_image_directory_path, image) for image in data_frame['imageName'].tolist() ], 'value': 0 } else: subject_images_list[sub_id] = { 'imageList': [ os.path.join(test_image_directory_path, image) for image in data_frame['imageName'].tolist() ], 'value': sub_sub_similarity_pairs[sub_id] } count += 1 if count > 3: break misc.plot_similar_images(subject_images_list, subject_subject_similarity=True)
def get_similar_images(self, test_folder=None, decomposition=None, reduced_dimension=False, metadata_pickle=None): test_folder_path = os.path.join( Path(os.path.dirname(__file__)).parent, test_folder) test_image_path = os.path.join(test_folder_path, self.test_image_id) try: # Image is present misc.read_image(test_image_path) except FileNotFoundError: print('ImageId is not in the folder specified.') return test_image_features, dataset_images_features = self.get_database_image_features( test_folder, decomposition, reduced_dimension, metadata_pickle) test_folder_path = os.path.join( Path(os.path.dirname(__file__)).parent, test_folder) features_images = FeaturesImages(self.model_name) model = features_images.get_model() ranking = {} for image_id, feature_vector in tqdm(dataset_images_features.items()): if image_id != self.test_image_id: distance = model.similarity_fn(test_image_features, feature_vector) ranking[image_id] = distance sorted_results = collections.OrderedDict( sorted(ranking.items(), key=lambda val: val[1], reverse=model.reverse_sort)) top_k_items = { item: sorted_results[item] for item in list(sorted_results)[:self.k + 1] } plot_images = {} for image_id in top_k_items.keys(): if image_id != self.test_image_id: image_path = os.path.join(test_folder_path, image_id) plot_images[image_path] = top_k_items[image_id] print('Plotting Similar Images') misc.plot_similar_images(plot_images)
dataset_features = pickle.load(infile) metadata = Metadata(metadatapath='Data/HandInfo.csv') images_dop_dict = metadata.getimagesdop_dict() for image_id, feature in dataset_features.items(): lsh.add_to_index_structure(input_feature=feature, image_id=image_id) ret_val, no_of_images, unique_images = lsh.query( dataset_features[q_image_id], num_results=t) print('Query Image:', q_image_id, images_dop_dict[q_image_id]) print("Total Number of Images:", no_of_images) print("Unique Images:", unique_images) result = {} for val in ret_val: result[val[0]] = val[1] misc.plot_similar_images(result) lsh.save_result(result) elif task == '6': r = int(input('Number Of Images you would like to label as Relevant:')) ir = int(input('Number of Images you would like to label as Irrelevant:')) final_path = '../Phase2/pickle_files/HOG_SVD_11k.pkl' print('loading from pickle file path', final_path) infile = open(final_path, 'rb') dataset_features = pickle.load(infile) feedback = Feedback() task5_result = feedback.task5_result base_id = None num_image = {} count = 1 rorir_map = {}