JoJosmin commited on
Commit
bf0ed8f
1 Parent(s): 6d2622d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -78,22 +78,22 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
78
 
79
  return img_with_alpha.convert("RGB"), final_mask, detected_categories
80
 
81
- # 유클리드 거리 기반으로 유사한 이미지 찾기
82
  def find_similar_images(query_embedding, collection, top_k=5):
83
- query_embedding = query_embedding.reshape(1, -1)
 
 
 
 
 
84
 
85
- all_embeddings = np.array(collection.get(include=['embeddings'])['embeddings'])
86
- distances = euclidean_distances(query_embedding, all_embeddings).flatten() # 유클리드 거리 계산
87
-
88
- top_indices = np.argsort(distances)[:top_k]
89
- top_metadatas = [collection.get(ids=[str(idx)])['metadatas'][0] for idx in top_indices]
90
- top_distances = distances[top_indices]
91
 
92
  structured_results = []
93
  for metadata, distance in zip(top_metadatas, top_distances):
94
  structured_results.append({
95
  'info': metadata,
96
- 'similarity': 1 / (1 + distance) # 유클리드 거리가 작을수록 유사도가 높음
97
  })
98
 
99
  return structured_results
 
78
 
79
  return img_with_alpha.convert("RGB"), final_mask, detected_categories
80
 
 
81
  def find_similar_images(query_embedding, collection, top_k=5):
82
+ query_embedding = query_embedding.reshape(1, -1) # Reshape to 2D array for ChromaDB
83
+ results = collection.query(
84
+ query_embeddings=query_embedding,
85
+ n_results=top_k,
86
+ include=['metadatas', 'distances']
87
+ )
88
 
89
+ top_metadatas = results['metadatas'][0]
90
+ top_distances = results['distances'][0]
 
 
 
 
91
 
92
  structured_results = []
93
  for metadata, distance in zip(top_metadatas, top_distances):
94
  structured_results.append({
95
  'info': metadata,
96
+ 'similarity': 1 - distance
97
  })
98
 
99
  return structured_results