0

I've made an object to call in the main function, called HashSimilarity. It basically compares two images with the dhash method. This comparison is made with a for-loop to compare each image against the searched image. And I think this way is not a time efficient way. If there is a much more graceful method, I'm looking into it.

class HashSimilarity:
.
.
.
    def compare(self):
        try:
            if self.hash0 - self.hash1 < self.cutoff:
                print(f'images are similar and hash score and file name is:{(os.path.basename(self.image2_path))}, {self.hash0 - self.hash1}')
                return self.image2_path
.
.
.

and then I call this object in a main function since I test other methods too. My main function's code block for image hash comparison section is look like this:

def search_through_folder(folder_path, image_id, method, hmethod='dhash', cutoff=5):
    '''
    searches through the folder and finds the similar images
    :param folder_path:
    :param image_id:
    :param method:
    :return: image paths in a list
    '''
    searched_img = os.path.join(folder_path, image_id)
    similar_images = []
    if method == 'hash':
        for file in tqdm(os.listdir(dirpath)):
            compare = HashSimilarity(searched_img, os.path.join(dirpath, file), cutoff)
            compare.set_method(f'{hmethod}')
            img_path = compare.compare()
            if img_path:
                similar_images.append(img_path)

Thanks in advance!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
hanbb
  • 15
  • 5
  • it's a database query. simplest case: linear scan (compare query's feature vector against every known image's feature vector) https://en.wikipedia.org/wiki/Content-based_image_retrieval?useskin=vector – Christoph Rackwitz Jan 22 '23 at 01:31

0 Answers0