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!