diff --git a/Apps/TeLaiDian/Crawler.py b/Apps/TeLaiDian/Crawler.py index aae575d..abeddd3 100644 --- a/Apps/TeLaiDian/Crawler.py +++ b/Apps/TeLaiDian/Crawler.py @@ -298,86 +298,81 @@ class TeLaiDianCrawler(BaseCrawler): w, h = d.window_size() # 1. 增加等待时间,确保页面加载完成 - logger.info(f"[详情页] 等待 {WAIT_DETAIL_PAGE_LOAD + 2}s 确保页面稳定 (根据要求多等会)...") - await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD + 2) + logger.info(f"[详情页] 等待 {WAIT_DETAIL_PAGE_LOAD + 1}s 确保页面稳定...") + await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD + 1) - # 2. 向上滑动一部分屏 (从屏幕底部的 85% 划到顶部的 15%) - # [优化] 避免滑过头,从全屏改为 0.7 屏幕左右 - logger.info("[详情页] 执行向上滑动操作 (y: 85% -> 15%)") - d.swipe(w // 2, int(h * 0.85), w // 2, int(h * 0.15), duration=0.8) - await asyncio.sleep(2.0) # 滑动后再次等待稳定 + # 2. 向上滑动大一些,确保“全部时段”露出来 + logger.info("[详情页] 执行大范围向上滑动 (y: 90% -> 10%),确保显示全部时段") + d.swipe(w // 2, int(h * 0.9), w // 2, int(h * 0.1), duration=1.0) + await asyncio.sleep(2.5) # 滑动后等待稳定 + + # 3. 截图并识别 (纯 OCR 流程) + final_screen_path = take_screenshot(d, f"tld_detail_ocr_input_{int(time.time())}.jpg") + logger.info(f"[详情页] 已截取 OCR 识别用图: {final_screen_path}") - # 3. 截图并识别 - final_screen_path = take_screenshot(d, f"tld_detail_vlm_input_{int(time.time())}.jpg") - logger.info(f"[详情页] 已截取 VLM 识别用图: {final_screen_path}") - - logger.info("[详情页] 优先使用 VLM 识别顶部“价格”标签和价格入口") - price_tab_screen = final_screen_path entrance_clicked = False - try: - # 1. 尝试使用 VLM 寻找顶部“价格”标签 - tab_data = await self.read_image_kit.find_price_tab_vlm(final_screen_path) + # 1. 使用 OCR 寻找顶部“价格”标签 + tab_data = await self.read_image_kit.find_price_tab_ocr(final_screen_path) tab_x, tab_y = None, None if tab_data.get("found") and tab_data.get("point"): p = tab_data["point"] tab_x = int(p[0] * w / 1000) tab_y = int(p[1] * h / 1000) - logger.info(f"[详情页] VLM 成功找到价格标签: 归一化{p} -> 像素({tab_x}, {tab_y})") + logger.info(f"[详情页] OCR 成功找到价格标签: 归一化{p} -> 像素({tab_x}, {tab_y})") else: tab_x = int(PRICE_TAB_X_NORM * w / 1000) tab_y = int(PRICE_TAB_Y_NORM * h / 1000) - logger.warning(f"[详情页] VLM 未找到价格标签,使用固定坐标兜底: 像素({tab_x}, {tab_y})") + logger.warning(f"[详情页] OCR 未找到价格标签,使用固定坐标兜底: 像素({tab_x}, {tab_y})") logger.info(f"[详情页] 正在点击价格标签...") d.click(tab_x, tab_y) - await asyncio.sleep(2.0) # 点击标签后多等会 + await asyncio.sleep(2.5) # 点击标签后等待 price_tab_screen = take_screenshot(d, f"tld_detail_after_price_tab_{int(time.time())}.jpg") logger.info(f"[详情页] 点击价格标签后的界面截图已保存: {price_tab_screen}") - # 2. 尝试使用 VLM 寻找价格入口 (重点识别“全部时段 >”) - entry_data = await self.read_image_kit.find_price_entrance_vlm(price_tab_screen) + # 2. 寻找并点击“全部时段”入口 (纯 OCR 识别) + entry_data = await self.read_image_kit.find_price_entrance_ocr(price_tab_screen) entry_x, entry_y = None, None if entry_data.get("found") and entry_data.get("point"): p = entry_data["point"] - # 安全校验:严禁点击 y > 850 (85%) 的区域,那是底部浮动条 - if p[1] > 850: - logger.warning(f"[详情页] VLM 虽返回坐标 {p},但 Y 轴过大 (疑似底部浮动条),强制判定为未找到。") + # 安全校验:严禁点击 y > 900 的区域,那是底部浮动条 + if p[1] > 900: + logger.warning(f"[详情页] OCR 返回坐标 {p} 偏低 (疑似底部浮动条),尝试使用固定坐标兜底") entry_x = int(PRICE_ENTRY_X_NORM * w / 1000) entry_y = int(PRICE_ENTRY_Y_NORM * h / 1000) else: entry_x = int(p[0] * w / 1000) entry_y = int(p[1] * h / 1000) - logger.info(f"[详情页] VLM 成功找到价格入口 (匹配全部时段标识): 归一化{p} -> 像素({entry_x}, {entry_y}),原因: {entry_data.get('reason')}") + logger.info(f"[详情页] OCR 成功找到价格入口: 归一化{p} -> 像素({entry_x}, {entry_y})") else: entry_x = int(PRICE_ENTRY_X_NORM * w / 1000) entry_y = int(PRICE_ENTRY_Y_NORM * h / 1000) - logger.warning(f"[详情页] VLM 未找到价格入口,使用固定坐标兜底: 像素({entry_x}, {entry_y})") + logger.warning(f"[详情页] OCR 未找到价格入口,使用固定坐标兜底: 像素({entry_x}, {entry_y})") click_x = max(5, min(w - 5, entry_x)) click_y = max(5, min(h - 5, entry_y)) - debug_click_path = price_tab_screen.replace( - ".jpg", - f"_click_tab_{tab_x}_{tab_y}_entry_{click_x}_{click_y}.jpg" - ) + # 绘制诊断图 + debug_click_path = price_tab_screen.replace(".jpg", f"_click_diag.jpg") try: img = read_image(price_tab_screen) if img is not None: + # 绘制两个点击位置 cv2.circle(img, (tab_x, tab_y), 20, (0, 0, 255), -1) - cv2.circle(img, (click_x, click_y), 20, (0, 0, 255), -1) + cv2.circle(img, (click_x, click_y), 20, (0, 255, 0), -1) save_image(debug_click_path, img) - logger.info(f"[详情页] 已生成价格点击诊断红点图: {debug_click_path}") + logger.info(f"[详情页] 已生成 OCR 点击诊断图 (红点:价格标签, 绿点:入口): {debug_click_path}") except Exception as e: logger.error(f"[详情页] 生成诊断图片失败: {e}") logger.info(f"[详情页] 正在点击电价入口...") d.click(click_x, click_y) entrance_clicked = True - await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD) + await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD + 1) except Exception as e: logger.error(f"[详情页] 识别或点击价格入口失败: {e}") diff --git a/Apps/TeLaiDian/ReadImageKit.py b/Apps/TeLaiDian/ReadImageKit.py index 7fcaf9f..dc24b27 100644 --- a/Apps/TeLaiDian/ReadImageKit.py +++ b/Apps/TeLaiDian/ReadImageKit.py @@ -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 在页面上寻找关闭按钮(用于清理广告弹窗) diff --git a/Apps/TelaiDian/__pycache__/Crawler.cpython-310.pyc b/Apps/TelaiDian/__pycache__/Crawler.cpython-310.pyc index 26e8e2a..a72734a 100644 Binary files a/Apps/TelaiDian/__pycache__/Crawler.cpython-310.pyc and b/Apps/TelaiDian/__pycache__/Crawler.cpython-310.pyc differ diff --git a/Apps/TelaiDian/__pycache__/ReadImageKit.cpython-310.pyc b/Apps/TelaiDian/__pycache__/ReadImageKit.cpython-310.pyc index 9f46d8f..13622b7 100644 Binary files a/Apps/TelaiDian/__pycache__/ReadImageKit.cpython-310.pyc and b/Apps/TelaiDian/__pycache__/ReadImageKit.cpython-310.pyc differ