This commit is contained in:
HuangHai
2026-01-15 09:16:20 +08:00
parent f8af4dd9ae
commit b49dd9bc1b
4 changed files with 130 additions and 33 deletions

View File

@@ -140,6 +140,108 @@ class ReadImageKit:
logger.error(f"VLM 寻找价格入口失败: {e}")
return {"found": False}
async def find_price_tab_ocr(self, image_path):
"""
使用 OCR 在详情页顶部标签栏中寻找“价格”标签
不再使用 VLM
"""
from Apps.TeLaiDian.Kit import draw_rectangles
img = read_image(image_path)
if img is None:
return {"found": False}
h, w = img.shape[:2]
reader = get_easyocr_reader(gpu=True)
# 限制只在顶部 30% 区域查找标签
roi = img[0:int(h*0.3), :]
results = reader.read_text(roi)
for (quad, text, prob) in results:
if '价格' in text and prob > 0.3:
# 转换坐标 (加上 ROI 的偏移,这里 y 偏移是 0)
res = reader.get_normalized_rect(quad, w, h)
center_x = (res[0] + res[2]) // 2
center_y = (res[1] + res[3]) // 2
# 绘制诊断图
actual_p = [int(center_x * w / 1000), int(center_y * h / 1000)]
bbox = [int(res[0] * w / 1000), int(res[1] * h / 1000), int(res[2] * w / 1000), int(res[3] * h / 1000)]
draw_rectangles(image_path, bboxes=[bbox], click_points=[actual_p])
logger.info(f"[OCR] 已定位顶部价格标签并保存诊断图: {image_path.replace('.jpg', '_tab_vl.jpg')}")
return {
"found": True,
"reason": f"OCR 定位到‘{text}’",
"point": [center_x, center_y]
}
logger.warning(f"[OCR] 未能在顶部区域定位到‘价格’标签")
return {"found": False}
async def find_price_tab_ocr(self, image_path):
"""
使用 OCR 在详情页寻找顶部“价格”标签
"""
img = read_image(image_path)
if img is None:
return {"found": False}
h, w = img.shape[:2]
from Util.EasyOcrKit import get_easyocr_reader
reader = get_easyocr_reader(gpu=True)
# 顶部标签栏通常在屏幕上部 10%-30% 区域
# 我们先识别所有文字,然后过滤出“价格”
results = reader.read_text(img)
for (quad, text, prob) in results:
if "价格" in text and prob > 0.3:
# 获取中心点
rect = reader.get_normalized_rect(quad, w, h)
center_x = (rect[0] + rect[2]) // 2
center_y = (rect[1] + rect[3]) // 2
# 校验位置:必须在屏幕上半部分
if center_y < 500:
logger.info(f"[OCR] 找到顶部价格标签: '{text}', 置信度: {prob:.4f}, 坐标: [{center_x}, {center_y}]")
return {
"found": True,
"point": [center_x, center_y]
}
return {"found": False}
async def find_price_entrance_ocr(self, image_path):
"""
使用 OCR 在详情页寻找价格入口 (全部时段 >)
不再使用 VLM 和 掩码 CV
"""
container_norm = detect_price_info_container_cv(image_path)
if container_norm:
# 找到矩形框 [x1, y1, x2, y2]
x1, y1, x2, y2 = container_norm
# 计算中心点
center_x = (x1 + x2) // 2
center_y = (y1 + y2) // 2
# 绘制诊断图 (复用之前的逻辑)
img = read_image(image_path)
if img is not None:
h, w = img.shape[:2]
actual_p = [int(center_x * w / 1000), int(center_y * h / 1000)]
bbox = [int(x1 * w / 1000), int(y1 * h / 1000), int(x2 * w / 1000), int(y2 * h / 1000)]
draw_rectangles(image_path, bboxes=[bbox], click_points=[actual_p])
logger.info(f"[OCR] 已定位价格入口并保存诊断图: {image_path.replace('.jpg', '_vl.jpg')}")
return {
"found": True,
"reason": "OCR 精准定位到‘全部时段’文字",
"point": [center_x, center_y]
}
logger.warning(f"[OCR] 未能在页面中定位到‘全部时段’")
return {"found": False}
async def find_close_button_vlm(self, image_path):
"""
使用 VLM 在页面上寻找关闭按钮(用于清理广告弹窗)