Exemplo n.º 1
0
def compute_catalog_fingerprint(catalog_path: str, verbose=True) -> str:
    """Computes the fingerprint of a given catalog

    Args:
        catalog_path (str): The path of the input catalog.
        verbose (bool, optional): [description]. Defaults to True.

    Returns:
        str: the catalog fingerprint.
    """
    # Init. catalog fingerprint
    catalog_fingerprint = ""

    # Get all paths
    image_paths = get_all_images_from_folder(catalog_path)

    # Iterator
    iterator = get_iterator(image_paths,
                            verbose=verbose,
                            description="Computing fingerprint...")

    # Loop over image_paths
    for image_path in iterator:
        # Hash image_path
        path_hash = sha224(str.encode(image_path)).hexdigest()

        # Update catalog_fingerprint
        catalog_fingerprint = f"{catalog_fingerprint}{path_hash}"

    # Compute final fingerprint
    catalog_fingerprint = sha224(str.encode(catalog_fingerprint)).hexdigest()

    return catalog_fingerprint
Exemplo n.º 2
0
    def predict_batch(self, query_paths: List[str]) -> np.array:
        """Method used to predict a class for a batch of queries.

        Args:
            query_paths (List[str]): The list of all query paths.

        Returns:
            np.array: The scores per class for each query.
        """
        # Init scores
        scores = []

        # Get iterator
        iterator = utils.get_iterator(query_paths,
                                      verbose=self.config.verbose,
                                      description="Prediction of all queries")

        # Loop over all queries
        for query_path in iterator:
            # Predict score of query
            query_scores = self.predict(query_path)

            # Update scores
            scores.append(query_scores)

        # To numpy
        scores = np.array(scores)

        return scores
Exemplo n.º 3
0
    def _get_catalog_descriptors(self):
        # Init descriptors list
        catalog_descriptors = []

        # Init iterator
        iterator = utils.get_iterator(
            utils.get_all_images_from_folder(self.catalog_path),
            verbose=self.config.verbose,
            description="Computing catalog descriptors")

        # Compute all descriptors
        for path in iterator:
            # Read image
            img = utils.read_image(path, size=self.config.image_size)

            # Compute keypoints
            keypoints = utils.compute_keypoints(img,
                                                self.config.keypoint_stride,
                                                self.config.keypoint_sizes)

            # Compute descriptors
            descriptors = utils.compute_descriptors(
                img, keypoints, self.config.feature_descriptor)

            # Update descriptors list
            catalog_descriptors.append(descriptors)

        # Reshape descriptors list
        catalog_descriptors = np.array(catalog_descriptors)
        catalog_descriptors = catalog_descriptors.reshape(
            -1, catalog_descriptors.shape[-1])

        return catalog_descriptors
Exemplo n.º 4
0
def test_verbose():
    """[summary]
    """
    # List
    test_list = range(10)

    # Get iterator
    iterator = get_iterator(test_list, verbose=True)

    # Assert
    assert isinstance(iterator, tqdm)
Exemplo n.º 5
0
def test_no_verbose():
    """[summary]
    """
    # List
    test_list = range(10)

    # Get iterator
    iterator = get_iterator(test_list, verbose=False)

    # Assert
    assert isinstance(iterator, type(test_list))
Exemplo n.º 6
0
    def _create_matcher_index(self):
        # Get descriptors
        catalog_descriptors = self._get_catalog_descriptors()

        # Get iterator
        descriptors_iterator = utils.get_iterator(
            catalog_descriptors,
            verbose=self.config.verbose,
            description="Creating Index...")

        # Config matcher
        for k, descriptor in enumerate(descriptors_iterator):
            self.matcher.add_item(k, descriptor)
        self.matcher.build(self.config.matcher_n_trees)
Exemplo n.º 7
0
def test_with_description():
    """[summary]
    """
    # List
    test_list = range(10)

    # Description
    test_description = "Test Description"

    # Get iterator
    iterator = get_iterator(test_list,
                            verbose=True,
                            description=test_description)

    # Assert
    assert iterator.desc == test_description
Exemplo n.º 8
0
    def compute_catalog_embeddings(self) -> np.array:
        """[summary]

        Returns:
            np.array: [description]
        """
        # Init. catalog embeddings
        self.catalog_embeddings = []

        # Loop over catalog images
        for catalog_img_path in utils.get_iterator(self.catalog_images,
                                                   verbose=self.config.verbose):
            # Read catalog image
            catalog_image = utils.read_image(catalog_img_path,
                                             size=self.config.image_size)
            catalog_image = np.expand_dims(catalog_image, axis=0)

            # Compute embedding
            catalog_emdding = self.triplet_model.predict(catalog_image)[0]

            # Update catalog_emddings
            self.catalog_embeddings.append(catalog_emdding)

        self.catalog_embeddings = np.array(self.catalog_embeddings)