Add: show image simelarity
This commit is contained in:
parent
f2c132a6ab
commit
a60a05bab4
1 changed files with 6 additions and 5 deletions
|
@ -92,17 +92,18 @@ else:
|
|||
|
||||
def find_similar_images(query_image_path, index, k=6):
|
||||
query_feature = extract_features(query_image_path, model).reshape(1, -1)
|
||||
_distance, indices = index.search(query_feature, k)
|
||||
return indices.flatten()
|
||||
distance, indices = index.search(query_feature, k)
|
||||
return distance.flatten(), indices.flatten()
|
||||
|
||||
|
||||
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
|
||||
for idx in similar_image_indices:
|
||||
for i, idx in enumerate(similar_image_indices):
|
||||
similar_image_path = image_paths[idx]
|
||||
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.destroyAllWindows()
|
||||
|
|
Loading…
Reference in a new issue