Update app.py
Browse files
app.py
CHANGED
@@ -255,19 +255,19 @@ def search_similar_items(image, mask=None, top_k=10):
|
|
255 |
results = collection.query(
|
256 |
query_images=[np.array(query_image)],
|
257 |
n_results=top_k,
|
258 |
-
include=['metadatas', 'distances'
|
259 |
)
|
260 |
|
261 |
if not results or 'metadatas' not in results:
|
262 |
return []
|
263 |
|
264 |
similar_items = []
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
#
|
269 |
-
#
|
270 |
-
cosine_similarity =
|
271 |
|
272 |
# -1~1 ๋ฒ์์ ์ฝ์ฌ์ธ ์ ์ฌ๋๋ฅผ 0~100 ๋ฒ์๋ก ๋ณํ
|
273 |
similarity_score = ((cosine_similarity + 1) / 2) * 100
|
|
|
255 |
results = collection.query(
|
256 |
query_images=[np.array(query_image)],
|
257 |
n_results=top_k,
|
258 |
+
include=['metadatas', 'distances']
|
259 |
)
|
260 |
|
261 |
if not results or 'metadatas' not in results:
|
262 |
return []
|
263 |
|
264 |
similar_items = []
|
265 |
+
for metadata, distance in zip(results['metadatas'][0], results['distances'][0]):
|
266 |
+
# L2 ๊ฑฐ๋ฆฌ๋ฅผ ์ฝ์ฌ์ธ ์ ์ฌ๋๋ก ๋ณํ
|
267 |
+
# ์ ๊ทํ๋ ๋ฒกํฐ ๊ฐ์ L2 ๊ฑฐ๋ฆฌ(d)์ ์ฝ์ฌ์ธ ์ ์ฌ๋(cos_sim) ๊ด๊ณ:
|
268 |
+
# d^2 = 2(1 - cos_sim)
|
269 |
+
# cos_sim = 1 - (d^2/2)
|
270 |
+
cosine_similarity = 1 - (distance ** 2 / 2)
|
271 |
|
272 |
# -1~1 ๋ฒ์์ ์ฝ์ฌ์ธ ์ ์ฌ๋๋ฅผ 0~100 ๋ฒ์๋ก ๋ณํ
|
273 |
similarity_score = ((cosine_similarity + 1) / 2) * 100
|