Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|