Rudra Rahul Chothe commited on
Commit
dbe84de
·
verified ·
1 Parent(s): 3cfebcb

Update src/similarity_search.py

Browse files
Files changed (1) hide show
  1. src/similarity_search.py +23 -21
src/similarity_search.py CHANGED
@@ -1,22 +1,24 @@
1
- import faiss
2
- import numpy as np
3
- import pickle
4
- import os
5
-
6
- class SimilaritySearchEngine:
7
- def __init__(self, embeddings_path='data/embeddings.pkl'):
8
- # Load precomputed embeddings
9
- with open(embeddings_path, 'rb') as f:
10
- data = pickle.load(f)
11
- self.embeddings = data['embeddings']
12
- self.image_paths = data['image_paths']
13
-
14
- # Create FAISS index
15
- dimension = len(self.embeddings[0])
16
- self.index = faiss.IndexFlatL2(dimension)
17
- self.index.add(np.array(self.embeddings))
18
-
19
- def search_similar_images(self, query_embedding, top_k=5):
20
- # Perform similarity search
21
- distances, indices = self.index.search(np.array([query_embedding]), top_k)
 
 
22
  return [self.image_paths[idx] for idx in indices[0]], distances[0]
 
1
+ import faiss
2
+ import numpy as np
3
+ import pickle
4
+ import os
5
+
6
+ class SimilaritySearchEngine:
7
+ def __init__(self):
8
+ base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
9
+ embeddings_path = os.path.join(base_dir, 'data', 'embeddings.pkl')
10
+
11
+ with open(embeddings_path, 'rb') as f:
12
+ data = pickle.load(f)
13
+ self.embeddings = data['embeddings']
14
+ # Convert Windows paths to Linux paths
15
+ self.image_paths = [os.path.normpath(path).replace('\\', '/')
16
+ for path in data['image_paths']]
17
+
18
+ dimension = len(self.embeddings[0])
19
+ self.index = faiss.IndexFlatL2(dimension)
20
+ self.index.add(np.array(self.embeddings))
21
+
22
+ def search_similar_images(self, query_embedding, top_k=5):
23
+ distances, indices = self.index.search(np.array([query_embedding]), top_k)
24
  return [self.image_paths[idx] for idx in indices[0]], distances[0]