def compute_image_features(self, image, print_arr=False):
     image_feature = []
     try:
         image_path = os.path.join(os.path.dirname(__file__), image)
         image = misc.read_image(image_path)
         converted_image = misc.convert2gray(image)
         if self.model_name == 'CM':
             converted_image = misc.convert2yuv(image)
         if self.model_name == 'HOG':
             converted_image = misc.resize_image(converted_image,
                                                 (120, 160))
         if self.split_windows:
             windows = misc.split_into_windows(converted_image, 100, 100)
             for window in windows:
                 window_pattern = self.model.compute(window)
                 if len(image_feature) == 0:
                     image_feature = window_pattern
                 else:
                     image_feature = np.concatenate(
                         [image_feature, window_pattern])
         else:
             image_feature = self.model.compute(converted_image)
     except OSError as e:
         print("Features_image", e.strerror)
         sys.exit()
     finally:
         if not print_arr:
             return image_feature
         else:
             print(image_feature)
Exemplo n.º 2
0
def decode(path):
    """extract hidden message from image"""
    mat = read_image(path)
    height = len(mat)
    width = len(mat[0])
    seed(get_seed(mat))
    gen_coo = gen_no_doubles(gen_random_coo(height, width))
    for i in gen_message(gen_coo, mat):
        yield i
Exemplo n.º 3
0
    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)