Update app.py
Browse files
app.py
CHANGED
|
@@ -239,9 +239,32 @@ def process_segmentation(image):
|
|
| 239 |
return []
|
| 240 |
|
| 241 |
def search_similar_items(image, mask=None, top_k=10):
|
| 242 |
-
"""๋ฉํฐ๋ชจ๋ฌ ๊ฒ์ ์ํ"""
|
| 243 |
try:
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
# ๋ง์คํฌ ์ ์ฉ
|
| 247 |
if mask is not None:
|
|
@@ -250,99 +273,113 @@ def search_similar_items(image, mask=None, top_k=10):
|
|
| 250 |
query_image = Image.fromarray(masked_image.astype(np.uint8))
|
| 251 |
else:
|
| 252 |
query_image = image
|
| 253 |
-
|
| 254 |
-
# ๊ฒ์ ์ํ
|
| 255 |
-
results = collection.query(
|
| 256 |
-
query_images=[np.array(query_image)],
|
| 257 |
-
n_results=top_k,
|
| 258 |
-
include=['metadatas', 'distances']
|
| 259 |
-
)
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
logger.error(f"Multimodal search error: {e}")
|
| 284 |
return []
|
| 285 |
|
| 286 |
-
def update_db_with_multimodal():
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
|
| 347 |
def show_similar_items(similar_items):
|
| 348 |
"""Display similar items in a structured format with similarity scores"""
|
|
|
|
| 239 |
return []
|
| 240 |
|
| 241 |
def search_similar_items(image, mask=None, top_k=10):
|
| 242 |
+
"""๋ ๊ฐ์ ๋ฉํฐ๋ชจ๋ฌ ์ปฌ๋ ์
์์ ๊ฒ์ ์ํ"""
|
| 243 |
try:
|
| 244 |
+
client = chromadb.PersistentClient(path="./fashion_multimodal_db")
|
| 245 |
+
embedding_function = CustomFashionEmbeddingFunction()
|
| 246 |
+
data_loader = ImageLoader()
|
| 247 |
+
|
| 248 |
+
# ๋ ์ปฌ๋ ์
๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
|
| 249 |
+
collections = []
|
| 250 |
+
collection_names = ["fashion_multimodal", "fashion_multimodal_v2"]
|
| 251 |
+
|
| 252 |
+
for name in collection_names:
|
| 253 |
+
try:
|
| 254 |
+
collection = client.get_collection(
|
| 255 |
+
name=name,
|
| 256 |
+
embedding_function=embedding_function,
|
| 257 |
+
data_loader=data_loader
|
| 258 |
+
)
|
| 259 |
+
collections.append(collection)
|
| 260 |
+
logger.info(f"Successfully connected to {name} collection")
|
| 261 |
+
except Exception as e:
|
| 262 |
+
logger.error(f"Error getting collection {name}: {e}")
|
| 263 |
+
continue
|
| 264 |
+
|
| 265 |
+
if not collections:
|
| 266 |
+
logger.error("No collections available for search")
|
| 267 |
+
return []
|
| 268 |
|
| 269 |
# ๋ง์คํฌ ์ ์ฉ
|
| 270 |
if mask is not None:
|
|
|
|
| 273 |
query_image = Image.fromarray(masked_image.astype(np.uint8))
|
| 274 |
else:
|
| 275 |
query_image = image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
+
# ๊ฐ ์ปฌ๋ ์
์์ ๊ฒ์ ์ํ
|
| 278 |
+
all_results = []
|
| 279 |
+
|
| 280 |
+
for collection in collections:
|
| 281 |
+
try:
|
| 282 |
+
results = collection.query(
|
| 283 |
+
query_images=[np.array(query_image)],
|
| 284 |
+
n_results=top_k,
|
| 285 |
+
include=['metadatas', 'distances']
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
if results and 'metadatas' in results:
|
| 289 |
+
for metadata, distance in zip(results['metadatas'][0], results['distances'][0]):
|
| 290 |
+
# L2 ๊ฑฐ๋ฆฌ๋ฅผ ์ฝ์ฌ์ธ ์ ์ฌ๋๋ก ๋ณํ
|
| 291 |
+
cosine_similarity = 1 - (distance ** 2 / 2)
|
| 292 |
+
similarity_score = ((cosine_similarity + 1) / 2) * 100
|
| 293 |
+
|
| 294 |
+
item_data = metadata.copy()
|
| 295 |
+
item_data['similarity_score'] = similarity_score
|
| 296 |
+
all_results.append(item_data)
|
| 297 |
+
|
| 298 |
+
except Exception as e:
|
| 299 |
+
logger.error(f"Error searching in collection: {e}")
|
| 300 |
+
continue
|
| 301 |
+
|
| 302 |
+
# ๊ฒฐ๊ณผ ์ ๋ ฌ ๋ฐ ์ค๋ณต ์ ๊ฑฐ
|
| 303 |
+
# URL ๊ธฐ๋ฐ์ผ๋ก ์ค๋ณต ์ ๊ฑฐ
|
| 304 |
+
seen_urls = set()
|
| 305 |
+
unique_results = []
|
| 306 |
+
|
| 307 |
+
for item in sorted(all_results, key=lambda x: x['similarity_score'], reverse=True):
|
| 308 |
+
url = item.get('image_url', '')
|
| 309 |
+
if url not in seen_urls:
|
| 310 |
+
seen_urls.add(url)
|
| 311 |
+
unique_results.append(item)
|
| 312 |
+
|
| 313 |
+
# top_k ๊ฐ์๋งํผ ์์ง๋๋ฉด ์ข
๋ฃ
|
| 314 |
+
if len(unique_results) >= top_k:
|
| 315 |
+
break
|
| 316 |
+
|
| 317 |
+
return unique_results
|
| 318 |
|
| 319 |
except Exception as e:
|
| 320 |
logger.error(f"Multimodal search error: {e}")
|
| 321 |
return []
|
| 322 |
|
| 323 |
+
# def update_db_with_multimodal():
|
| 324 |
+
# """DB๋ฅผ ๋ฉํฐ๋ชจ๋ฌ ๋ฐฉ์์ผ๋ก ์
๋ฐ์ดํธ"""
|
| 325 |
+
# try:
|
| 326 |
+
# # ์ ์ปฌ๋ ์
์์ฑ
|
| 327 |
+
# collection = setup_multimodal_collection()
|
| 328 |
|
| 329 |
+
# # ๊ธฐ์กด ์ปฌ๋ ์
์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
| 330 |
+
# client = chromadb.PersistentClient(path="./clothesDB_11GmarketMusinsa")
|
| 331 |
+
# old_collection = client.get_collection("clothes")
|
| 332 |
+
# old_data = old_collection.get(include=['metadatas'])
|
| 333 |
|
| 334 |
+
# total_items = len(old_data['metadatas'])
|
| 335 |
+
# progress_bar = st.progress(0)
|
| 336 |
+
# status_text = st.empty()
|
| 337 |
|
| 338 |
+
# batch_size = 100
|
| 339 |
+
# successful_updates = 0
|
| 340 |
+
# failed_updates = 0
|
| 341 |
|
| 342 |
+
# for i in range(0, total_items, batch_size):
|
| 343 |
+
# batch = old_data['metadatas'][i:i + batch_size]
|
| 344 |
|
| 345 |
+
# images = []
|
| 346 |
+
# valid_metadatas = []
|
| 347 |
+
# valid_ids = []
|
| 348 |
|
| 349 |
+
# for metadata in batch:
|
| 350 |
+
# try:
|
| 351 |
+
# if 'image_url' in metadata:
|
| 352 |
+
# response = requests.get(metadata['image_url'])
|
| 353 |
+
# img = Image.open(io.BytesIO(response.content)).convert('RGB')
|
| 354 |
+
# images.append(np.array(img))
|
| 355 |
+
# valid_metadatas.append(metadata)
|
| 356 |
+
# valid_ids.append(metadata.get('id', str(hash(metadata['image_url']))))
|
| 357 |
+
# successful_updates += 1
|
| 358 |
+
# except Exception as e:
|
| 359 |
+
# logger.error(f"Error processing image: {e}")
|
| 360 |
+
# failed_updates += 1
|
| 361 |
+
# continue
|
| 362 |
|
| 363 |
+
# if images:
|
| 364 |
+
# collection.add(
|
| 365 |
+
# ids=valid_ids,
|
| 366 |
+
# images=images,
|
| 367 |
+
# metadatas=valid_metadatas
|
| 368 |
+
# )
|
| 369 |
|
| 370 |
+
# # Update progress
|
| 371 |
+
# progress = (i + len(batch)) / total_items
|
| 372 |
+
# progress_bar.progress(progress)
|
| 373 |
+
# status_text.text(f"Processing: {i + len(batch)}/{total_items} items. "
|
| 374 |
+
# f"Success: {successful_updates}, Failed: {failed_updates}")
|
| 375 |
|
| 376 |
+
# status_text.text(f"Update completed. Successfully processed: {successful_updates}, "
|
| 377 |
+
# f"Failed: {failed_updates}")
|
| 378 |
+
# return True
|
| 379 |
|
| 380 |
+
# except Exception as e:
|
| 381 |
+
# logger.error(f"Multimodal DB update error: {e}")
|
| 382 |
+
# return False
|
| 383 |
|
| 384 |
def show_similar_items(similar_items):
|
| 385 |
"""Display similar items in a structured format with similarity scores"""
|