1
0
Fork 0

Add: show image simelarity

This commit is contained in:
Aroy-Art 2024-09-02 21:39:28 +02:00
parent f2c132a6ab
commit a60a05bab4
Signed by: Aroy
GPG key ID: DB9689E9391DD156

View file

@ -92,17 +92,18 @@ else:
def find_similar_images(query_image_path, index, k=6): def find_similar_images(query_image_path, index, k=6):
query_feature = extract_features(query_image_path, model).reshape(1, -1) query_feature = extract_features(query_image_path, model).reshape(1, -1)
_distance, indices = index.search(query_feature, k) distance, indices = index.search(query_feature, k)
return indices.flatten() return distance.flatten(), indices.flatten()
query_image_path = "query_image.jpg" query_image_path = "query_image.jpg"
similar_image_indices = find_similar_images(query_image_path, index) similar_image_distance, similar_image_indices = find_similar_images(query_image_path, index)
# display the results # display the results
for idx in similar_image_indices: for i, idx in enumerate(similar_image_indices):
similar_image_path = image_paths[idx] similar_image_path = image_paths[idx]
img = cv2.imread(similar_image_path) img = cv2.imread(similar_image_path)
cv2.imshow("Similar Image", img) similarity = 1 - similar_image_distance[i] / np.max(similar_image_distance)
cv2.imshow(f"Similarity: {similarity * 100:.2f}% ({i+1}/{len(similar_image_indices)})", img)
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()