diff --git a/douyin/fudai/core/coords.py b/douyin/fudai/core/coords.py index 7ec05d2..6ec985f 100644 --- a/douyin/fudai/core/coords.py +++ b/douyin/fudai/core/coords.py @@ -95,9 +95,9 @@ SHOPPING_REDPACKET_AREA = (200, 300, 880, 1200) STAGE_RED_PACKET_OCR = (200, 300, 880, 1200) COUPON_PARTICIPATE_BUTTON_ALT = (200, 1940, 880, 2140) -RECOMMEND_TAB = (400, 140, 900, 200) +RECOMMEND_TAB = (0, 140, 1080, 220) FOLLOW_TAB = (658, 147, 765, 194) -LIVE_LIST_TAB = (400, 140, 900, 200) +LIVE_LIST_TAB = (0, 140, 1080, 220) LIVE_ROOM_TITLE = (130, 120, 560, 225) LIVE_ROOM_ICON_BBOX = (999, 172, 1028, 202) diff --git a/douyin/handlers/live_tab_ocr.py b/douyin/handlers/live_tab_ocr.py index 62e9dc8..0498c3e 100644 --- a/douyin/handlers/live_tab_ocr.py +++ b/douyin/handlers/live_tab_ocr.py @@ -111,32 +111,60 @@ def _to_float(value: Any) -> float | None: return None -def choose_live_tab_box(boxes: Iterable[OcrTextBox]) -> OcrTextBox | None: - """Pick the short top-tab "直播" OCR hit, ignoring longer page text.""" - candidates: list[tuple[int, OcrTextBox]] = [] +@dataclass(frozen=True) +class LiveTabCandidate: + box: OcrTextBox + center_x: float + center_y: float + + +def choose_live_tab_candidate(boxes: Iterable[OcrTextBox]) -> LiveTabCandidate | None: + """Pick the top-tab "直播" OCR hit. + + PaddleOCR often returns the whole tab row, e.g. "商城关注直播推荐", rather than + separate boxes. In that case click the estimated center of the "直播" substring + inside the row bbox. + """ + candidates: list[tuple[int, LiveTabCandidate]] = [] for box in boxes: text = str(box.text or "").strip().replace(" ", "") if "直播" not in text: continue - if len(text) > 4: + if "直播广场" in text: + continue + if len(text) > 12: continue x1, y1, x2, y2 = box.bbox width = max(0.0, x2 - x1) height = max(0.0, y2 - y1) if width <= 0 or height <= 0: continue + live_idx = text.find("直播") + if live_idx < 0: + continue + char_count = max(1, len(text)) + center_x = x1 + width * ((live_idx + 1.0) / char_count) + center_y = (y1 + y2) / 2.0 score = 10 if text == "直播": score += 20 + elif text in ("关注直播推荐", "商城关注直播推荐"): + score += 12 if box.confidence is not None: score += int(max(0.0, min(1.0, box.confidence)) * 10) - candidates.append((score, box)) + candidates.append((score, LiveTabCandidate(box=box, center_x=center_x, center_y=center_y))) if not candidates: return None candidates.sort(key=lambda item: item[0], reverse=True) return candidates[0][1] +def choose_live_tab_box(boxes: Iterable[OcrTextBox]) -> OcrTextBox | None: + """Backward-compatible wrapper returning only the selected OCR box.""" + candidate = choose_live_tab_candidate(boxes) + return candidate.box if candidate else None + + def find_live_tab_click_point_by_ocr(analyser: Any) -> tuple[int, int, dict[str, Any]] | None: """Find the recommendation-page live tab click point using OCR bbox.""" start = time.time() @@ -164,19 +192,18 @@ def find_live_tab_click_point_by_ocr(analyser: Any) -> tuple[int, int, dict[str, raw = [] boxes = parse_ocr_text_boxes(raw) - selected = choose_live_tab_box(boxes) + selected = choose_live_tab_candidate(boxes) if selected is None: return None - bx1, by1, bx2, by2 = selected.bbox rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080) ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400) - click_x = int((x1 + ((bx1 + bx2) / 2.0)) * rx // 1080) - click_y = int((y1 + ((by1 + by2) / 2.0)) * ry // 2400) + click_x = int((x1 + selected.center_x) * rx // 1080) + click_y = int((y1 + selected.center_y) * ry // 2400) return click_x, click_y, { - "ocr_text": selected.text, - "bbox": [round(v, 2) for v in selected.bbox], - "confidence": selected.confidence, + "ocr_text": selected.box.text, + "bbox": [round(v, 2) for v in selected.box.bbox], + "confidence": selected.box.confidence, "ocr_elapsed_ms": round((time.time() - start) * 1000, 1), } except Exception: diff --git a/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py b/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py index c9f4e5d..78bbe81 100644 --- a/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py +++ b/douyin/stage/douyin_stage_handler_pkg/ocr_detection.py @@ -84,7 +84,10 @@ def detect_pages_via_ocr( t_follow = "" t_list = "" try: - log("步骤3[OCR推荐页标签栏]-区域", 区域="(400,140)-(900,200)") + log( + "步骤3[OCR推荐页标签栏]-区域", + 区域="({},{})-({},{})".format(*RECOMMEND_TAB), + ) if time.time() - step_start < max_step_time: cut_start = time.time() analyser.cut_pic(*RECOMMEND_TAB, "", "推荐页标签栏") diff --git a/tests/test_live_tab_ocr.py b/tests/test_live_tab_ocr.py index d6ddf22..438091e 100644 --- a/tests/test_live_tab_ocr.py +++ b/tests/test_live_tab_ocr.py @@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch from douyin.handlers.live_tab_ocr import ( OcrTextBox, + choose_live_tab_candidate, choose_live_tab_box, click_live_tab_on_recommend_page, parse_ocr_text_boxes, @@ -34,6 +35,23 @@ def test_choose_live_tab_ignores_long_text(): assert selected.text == "直播" +def test_choose_live_tab_from_joined_tab_row_estimates_substring_center(): + selected = choose_live_tab_candidate( + [ + OcrTextBox( + text="商城关注直播推荐", + bbox=(0.0, 0.0, 1080.0, 80.0), + confidence=0.92, + ) + ] + ) + + assert selected is not None + assert selected.box.text == "商城关注直播推荐" + assert 660 <= selected.center_x <= 700 + assert selected.center_y == 40.0 + + def test_click_live_tab_uses_ocr_bbox(monkeypatch, tmp_path): pic = tmp_path / "live_tab.png" from PIL import Image @@ -53,7 +71,7 @@ def test_click_live_tab_uses_ocr_bbox(monkeypatch, tmp_path): analyser.operation.ocr = SimpleNamespace( readtext=MagicMock( return_value=[ - ([[300, 10], [340, 10], [340, 35], [300, 35]], "直播", 0.96) + ([[700, 10], [740, 10], [740, 35], [700, 35]], "直播", 0.96) ] ) ) @@ -87,6 +105,40 @@ def test_click_live_tab_falls_back_when_ocr_misses(tmp_path): analyser.operation.click.assert_called_once_with("D", 718, 169) +def test_click_live_tab_uses_joined_row_substring(tmp_path): + pic = tmp_path / "live_tab.png" + from PIL import Image + + Image.new("RGB", (1080, 80), (255, 255, 255)).save(pic) + analyser = SimpleNamespace() + analyser.device_id = "D" + analyser.resolution_ratio_x = 1080 + analyser.resolution_ratio_y = 2400 + analyser.cut_pic = MagicMock() + analyser._log_kv = MagicMock() + analyser.operation = SimpleNamespace() + analyser.operation.click = MagicMock() + analyser.operation._find_pic_path = MagicMock(return_value=str(pic)) + analyser.operation.ocr_type = "easyocr" + analyser.operation.ocr = SimpleNamespace( + readtext=MagicMock( + return_value=[ + ( + [[0, 0], [1080, 0], [1080, 80], [0, 80]], + "商城关注直播推荐", + 0.96, + ) + ] + ) + ) + + x, y, method = click_live_tab_on_recommend_page(analyser) + + assert 660 <= x <= 700 + assert y == 180 + assert method == "ocr_live_tab" + + def test_recommend_page_keeps_second_step_entry_mode(): from douyin.core.fudai_modules import run_once_basic_helpers as ronce