feat: 以图搜图功能升级(跨表UNION检索 + 拍照识图入口 + 批量向量初始化脚本)
This commit is contained in:
@ -100,7 +100,7 @@ def get_image_embedding(image_path: str) -> list:
|
||||
提取图像的 512 维 CLIP embedding 向量
|
||||
|
||||
参数:
|
||||
image_path: 图像文件路径(支持本地路径或 URL)
|
||||
image_path: 图像文件路径
|
||||
|
||||
返回:
|
||||
list: 512 维浮点向量
|
||||
@ -108,25 +108,25 @@ def get_image_embedding(image_path: str) -> list:
|
||||
if ort_session is None:
|
||||
load_clip_model()
|
||||
|
||||
# 加载图像
|
||||
try:
|
||||
image = Image.open(image_path).convert('RGB')
|
||||
except Exception as e:
|
||||
raise ValueError(f"图像加载失败: {image_path}, 错误: {e}")
|
||||
|
||||
# 中心裁剪
|
||||
# 1. 图片预处理
|
||||
image = Image.open(image_path).convert('RGB')
|
||||
image = _center_crop_and_resize(image)
|
||||
|
||||
# 归一化
|
||||
input_data = _normalize(np.array(image))
|
||||
input_data = np.expand_dims(input_data, axis=0) # [1, 3, 224, 224]
|
||||
|
||||
# 添加 batch 维度: (C, H, W) -> (1, C, H, W)
|
||||
input_data = np.expand_dims(input_data, axis=0)
|
||||
# 2. 构造占位符输入 (关键修复)
|
||||
dummy_ids = np.zeros((1, 77), dtype=np.int64)
|
||||
dummy_mask = np.zeros((1, 77), dtype=np.int64)
|
||||
|
||||
# 推理
|
||||
outputs = ort_session.run(None, {'images': input_data.astype(np.float32)})
|
||||
|
||||
# 输出通常是 (1, 512) 的向量,取第一项并展平为 list
|
||||
embedding = outputs[0][0].tolist()
|
||||
|
||||
return embedding
|
||||
# 3. 传入模型进行推理
|
||||
# 注意: 模型输入名在你的模型里必须叫 'pixel_values', 'input_ids', 'attention_mask'
|
||||
# 如果报错找不到输入名,请打印 ort_session.get_inputs()[0].name 确认
|
||||
outputs = ort_session.run(
|
||||
['image_embeds'],
|
||||
{
|
||||
'input_ids': dummy_ids,
|
||||
'pixel_values': input_data.astype(np.float32),
|
||||
'attention_mask': dummy_mask
|
||||
}
|
||||
)
|
||||
return outputs[0][0].tolist()
|
||||
Reference in New Issue
Block a user