diff --git a/sample_images/1755692193_1__.jpg b/sample_images/1755692193_1__.jpg new file mode 100644 index 0000000..a3ba501 Binary files /dev/null and b/sample_images/1755692193_1__.jpg differ diff --git a/sample_images/1755692193_2__.jpg b/sample_images/1755692193_2__.jpg new file mode 100644 index 0000000..d0f4498 Binary files /dev/null and b/sample_images/1755692193_2__.jpg differ diff --git a/sample_images/1755692193_3__.jpg b/sample_images/1755692193_3__.jpg new file mode 100644 index 0000000..9a5a1b6 Binary files /dev/null and b/sample_images/1755692193_3__.jpg differ diff --git a/sample_images/1755692193_4__.jpg b/sample_images/1755692193_4__.jpg new file mode 100644 index 0000000..1ae598e Binary files /dev/null and b/sample_images/1755692193_4__.jpg differ diff --git a/sample_images/1755692193_5__.jpg b/sample_images/1755692193_5__.jpg new file mode 100644 index 0000000..da8cddd Binary files /dev/null and b/sample_images/1755692193_5__.jpg differ diff --git a/uploads/query_1755692249_2__.jpg b/uploads/query_1755692249_2__.jpg new file mode 100644 index 0000000..d0f4498 Binary files /dev/null and b/uploads/query_1755692249_2__.jpg differ diff --git a/web_app_multigpu.py b/web_app_multigpu.py index e0b4b95..57dbb60 100644 --- a/web_app_multigpu.py +++ b/web_app_multigpu.py @@ -173,9 +173,39 @@ def handle_search(mode): # 执行搜索 if mode == 'text_to_text': - results = retrieval_system.search_text_to_text(query, top_k=top_k) + raw_results = retrieval_system.search_text_to_text(query, top_k=top_k) + # 格式化文本搜索结果 + results = [] + for text, score in raw_results: + results.append({ + 'text': text, + 'score': float(score) + }) else: # text_to_image - results = retrieval_system.search_text_to_image(query, top_k=top_k) + raw_results = retrieval_system.search_text_to_image(query, top_k=top_k) + # 格式化图像搜索结果 + results = [] + for image_path, score in raw_results: + try: + # 读取图像并转换为base64 + with open(image_path, 'rb') as img_file: + image_data = img_file.read() + image_base64 = base64.b64encode(image_data).decode('utf-8') + + results.append({ + 'filename': os.path.basename(image_path), + 'image_path': image_path, + 'image_base64': image_base64, + 'score': float(score) + }) + except Exception as e: + logger.error(f"读取图像失败 {image_path}: {e}") + results.append({ + 'filename': os.path.basename(image_path), + 'image_path': image_path, + 'image_base64': '', + 'score': float(score) + }) return jsonify({ 'success': True, @@ -211,9 +241,39 @@ def handle_search(mode): # 执行搜索 if mode == 'image_to_text': - results = retrieval_system.search_image_to_text(filepath, top_k=top_k) + raw_results = retrieval_system.search_image_to_text(filepath, top_k=top_k) + # 格式化文本搜索结果 + results = [] + for text, score in raw_results: + results.append({ + 'text': text, + 'score': float(score) + }) else: # image_to_image - results = retrieval_system.search_image_to_image(filepath, top_k=top_k) + raw_results = retrieval_system.search_image_to_image(filepath, top_k=top_k) + # 格式化图像搜索结果 + results = [] + for image_path, score in raw_results: + try: + # 读取图像并转换为base64 + with open(image_path, 'rb') as img_file: + image_data = img_file.read() + image_base64 = base64.b64encode(image_data).decode('utf-8') + + results.append({ + 'filename': os.path.basename(image_path), + 'image_path': image_path, + 'image_base64': image_base64, + 'score': float(score) + }) + except Exception as e: + logger.error(f"读取图像失败 {image_path}: {e}") + results.append({ + 'filename': os.path.basename(image_path), + 'image_path': image_path, + 'image_base64': '', + 'score': float(score) + }) # 转换查询图片为base64 query_image_b64 = image_to_base64(filepath)