This commit is contained in:
eust-w 2025-08-20 12:20:50 +00:00
parent f6099c312e
commit 4c0bc822cb
7 changed files with 64 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

View File

@ -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)