This commit is contained in:
HuangHai
2026-01-15 09:51:16 +08:00
parent fe27bc6884
commit 98e890a693
8 changed files with 3 additions and 449 deletions

View File

@@ -10,6 +10,7 @@ if project_root not in sys.path:
sys.path.append(project_root)
from Util.VLMKit import VLMKit
from Util.EasyOcrKit import get_easyocr_reader
from Apps.TeLaiDian.Kit import draw_rectangles, setup_logger, read_image, detect_price_info_container_cv
from Apps.TeLaiDian.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO, MIN_CARD_HEIGHT
@@ -20,166 +21,6 @@ class ReadImageKit:
def __init__(self):
self.vlm = VLMKit()
async def find_price_tab_vlm(self, image_path):
"""
使用 VLM 在详情页顶部标签栏中寻找“价格”标签的点击位置
"""
prompt = """
分析这张特来电充电站详情页截图,找到顶部标签栏中“价格”两个字所在的点击区域中心。
要求:
1. 仅在页面最上方的标签栏里查找,该标签栏通常包含“价格 / 终端 / 电站 / 评论 / 周边”等文字。
2. 不要选择下面“价格信息”模块中的数字(例如 1.0689 元/度)或其它文本。
3. 不要选择最顶端系统状态栏或返回按钮等区域。
输出格式为 JSON:
{
"found": true/false,
"reason": "为什么认为这个位置是顶部“价格”标签",
"point": [x, y] // 归一化坐标,范围 [0-1000]
}
"""
try:
res_text = await self.vlm.analyze_image(image_path, prompt)
json_str = self.vlm.extract_json(res_text)
data = json.loads(json_str)
if data.get("found") and data.get("point"):
p = data["point"]
img = read_image(image_path)
if img is not None:
h, w = img.shape[:2]
actual_p = [int(p[0] * w / 1000), int(p[1] * h / 1000)]
bbox = [actual_p[0]-60, actual_p[1]-30, actual_p[0]+60, actual_p[1]+30]
draw_rectangles(image_path, bboxes=[bbox], click_points=[actual_p])
logger.info(f"已生成价格标签诊断图片: {image_path.replace('.jpg', '_tab_vl.jpg')}")
return data
except Exception as e:
logger.error(f"VLM 寻找价格标签失败: {e}")
return {"found": False}
async def find_price_entrance_vlm(self, image_path):
"""
使用 VLM 在详情页寻找价格入口
重点识别:上方是大字价格(如 1.3435元/度),下方紧跟“全部时段 >”文字的区域
"""
# 1. 尝试使用 CV 检测“价格信息”容器矩形
container_norm = detect_price_info_container_cv(image_path)
container_desc = ""
vlm_image_path = image_path
if container_norm:
# 如果找到了容器,绘制绿框诊断图
img = read_image(image_path)
if img is not None:
h, w = img.shape[:2]
x1 = int(container_norm[0] * w / 1000)
y1 = int(container_norm[1] * h / 1000)
x2 = int(container_norm[2] * w / 1000)
y2 = int(container_norm[3] * h / 1000)
# 在左侧划分出一个子区域(当前价区域)
# 价格信息容器通常左右平分,左侧是当前价,右侧是会员价
current_price_x2 = x1 + (x2 - x1) // 2
# 绘制绿框
# 我们把整个容器标绿,并特别说明左侧是目标
diag_path = image_path.replace(".jpg", "_price_box.jpg")
import cv2
diag_img = img.copy()
cv2.rectangle(diag_img, (x1, y1), (x2, y2), (0, 255, 0), 3)
cv2.line(diag_img, (current_price_x2, y1), (current_price_x2, y2), (0, 255, 0), 2)
from Apps.TeLaiDian.Kit import save_image
save_image(diag_path, diag_img)
logger.info(f"[CV] 已检测到价格容器并保存绿框诊断图: {diag_path}")
# 更新 VLM 使用的图片为带绿框的图片,或者在提示词中说明
vlm_image_path = diag_path
container_desc = f"\n**视觉辅助**:图中已用【绿色矩形框】标出了“价格信息”区域。该区域被中间竖线分为左右两块。请重点分析【左侧半块】(即“当前价”所在的灰色背景区域)。"
prompt = f"""
分析这张特来电充电站详情页截图,找到进入“电价详情”的点击入口。
{container_desc}
**核心识别目标**:
1. 在页面中上部区域(或绿框内的左侧部分)寻找价格显示模块。
2. 该模块包含**红色、加粗的大字价格数字**(例如:1.3435元/度)。
3. **绝对必要标识**:在价格数字的正下方,**必须能够清晰看到**文本“全部时段 >”或“全部时段”。
4. 点击目标是“全部时段 >”文字所在的中心位置。
**严格限制与排除**:
1. **严禁选择 y > 850 的区域**(即屏幕最底部的 15% 区域)。
2. **严禁选择底部的浮动条**:浮动条通常包含“扫码充电”按钮,严禁返回其坐标。
3. 如果绿框存在,请优先在绿框的【左侧区域】寻找。
输出格式为 JSON:
{{
"found": true/false,
"reason": "解释你看到了什么,是否在绿框内找到了‘全部时段 >’",
"point": [x, y] // 归一化坐标 [0-1000]
}}
"""
try:
res_text = await self.vlm.analyze_image(vlm_image_path, prompt)
json_str = self.vlm.extract_json(res_text)
data = json.loads(json_str)
# 保存诊断图片
if data.get("found") and data.get("point"):
p = data["point"]
img = read_image(image_path)
if img is not None:
h, w = img.shape[:2]
actual_p = [int(p[0] * w / 1000), int(p[1] * h / 1000)]
bbox = [actual_p[0]-60, actual_p[1]-40, actual_p[0]+60, actual_p[1]+40]
draw_rectangles(image_path, bboxes=[bbox], click_points=[actual_p])
logger.info(f"已生成价格入口诊断图片: {image_path.replace('.jpg', '_vl.jpg')}")
return data
except Exception as e:
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_entrance_ocr(self, image_path):
"""
使用 OCR 在详情页寻找价格入口 (全部时段 >)