JoJosmin commited on
Commit
ed68053
1 Parent(s): e4aea0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -86,23 +86,34 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
86
 
87
  return img_with_alpha.convert("RGB"), final_mask, detected_categories # Return detected categories
88
 
89
-
90
  def find_similar_images(query_embedding, collection, top_k=5):
91
- all_embeddings = collection.get(include=['embeddings'])['embeddings']
92
- database_embeddings = np.array(all_embeddings)
93
- similarities = np.dot(database_embeddings, query_embedding.T).squeeze()
94
- top_indices = np.argsort(similarities)[::-1][:top_k]
95
-
96
- all_data = collection.get(include=['metadatas'])['metadatas']
97
- top_metadatas = [all_data[idx] for idx in top_indices]
98
-
99
- results = []
100
- for idx, metadata in enumerate(top_metadatas):
101
- results.append({
102
- 'info': metadata,
103
- 'similarity': similarities[top_indices[idx]]
104
- })
105
- return results
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
 
108
 
 
86
 
87
  return img_with_alpha.convert("RGB"), final_mask, detected_categories # Return detected categories
88
 
 
89
  def find_similar_images(query_embedding, collection, top_k=5):
90
+ try:
91
+ # 모든 임베딩을 가져옴
92
+ all_embeddings = collection.get(include=['embeddings', 'metadatas'])['embeddings']
93
+ database_embeddings = np.array(all_embeddings)
94
+
95
+ # 유사도 계산
96
+ similarities = cosine_similarity(database_embeddings, query_embedding.reshape(1, -1)).squeeze()
97
+ top_indices = np.argsort(similarities)[::-1][:top_k]
98
+
99
+ # 메타데이터 가져옴
100
+ all_data = collection.get(include=['metadatas'])['metadatas']
101
+ top_metadatas = [all_data[idx] for idx in top_indices]
102
+
103
+ results = []
104
+ for idx, metadata in enumerate(top_metadatas):
105
+ image_urls = metadata['image_url'].split(',')
106
+ representative_image_url = image_urls[0] if image_urls else None
107
+
108
+ results.append({
109
+ 'info': metadata,
110
+ 'similarity': similarities[top_indices[idx]],
111
+ 'image_url': representative_image_url
112
+ })
113
+ return results
114
+ except Exception as e:
115
+ st.error(f"Error during finding similar images: {str(e)}")
116
+ return []
117
 
118
 
119