fix: use ocr live tab and merge stuck room titles
This commit is contained in:
1 parent
e286cd46c8
commit
25f7f9af9e
9 files changed
+582
-16
No files matched your search
@@ -0,0 +1,119 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from douyin.handlers.live_tab_ocr import (
|
||||
OcrTextBox,
|
||||
choose_live_tab_box,
|
||||
click_live_tab_on_recommend_page,
|
||||
parse_ocr_text_boxes,
|
||||
)
|
||||
|
||||
|
||||
def test_parse_easyocr_live_bbox():
|
||||
raw = [([[10, 5], [50, 5], [50, 25], [10, 25]], "直播", 0.93)]
|
||||
boxes = parse_ocr_text_boxes(raw)
|
||||
assert boxes == [OcrTextBox(text="直播", bbox=(10.0, 5.0, 50.0, 25.0), confidence=0.93)]
|
||||
|
||||
|
||||
def test_parse_paddle_dict_boxes():
|
||||
raw = [{"rec_texts": ["推荐", "直播"], "rec_boxes": [[1, 2, 20, 12], [40, 2, 70, 14]], "rec_scores": [0.8, 0.9]}]
|
||||
boxes = parse_ocr_text_boxes(raw)
|
||||
assert boxes[1] == OcrTextBox(text="直播", bbox=(40.0, 2.0, 70.0, 14.0), confidence=0.9)
|
||||
|
||||
|
||||
def test_choose_live_tab_ignores_long_text():
|
||||
selected = choose_live_tab_box(
|
||||
[
|
||||
OcrTextBox(text="直播广场", bbox=(1, 1, 50, 20), confidence=0.99),
|
||||
OcrTextBox(text="直播", bbox=(70, 1, 100, 20), confidence=0.80),
|
||||
]
|
||||
)
|
||||
assert selected is not None
|
||||
assert selected.text == "直播"
|
||||
|
||||
|
||||
def test_click_live_tab_uses_ocr_bbox(monkeypatch, tmp_path):
|
||||
pic = tmp_path / "live_tab.png"
|
||||
from PIL import Image
|
||||
|
||||
Image.new("RGB", (500, 60), (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=[
|
||||
([[300, 10], [340, 10], [340, 35], [300, 35]], "直播", 0.96)
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
x, y, method = click_live_tab_on_recommend_page(analyser)
|
||||
|
||||
assert (x, y, method) == (720, 162, "ocr_live_tab")
|
||||
analyser.operation.click.assert_called_once_with("D", 720, 162)
|
||||
|
||||
|
||||
def test_click_live_tab_falls_back_when_ocr_misses(tmp_path):
|
||||
pic = tmp_path / "live_tab.png"
|
||||
from PIL import Image
|
||||
|
||||
Image.new("RGB", (500, 60), (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=[]))
|
||||
|
||||
x, y, method = click_live_tab_on_recommend_page(analyser)
|
||||
|
||||
assert (x, y, method) == (718, 169, "fixed_fallback")
|
||||
analyser.operation.click.assert_called_once_with("D", 718, 169)
|
||||
|
||||
|
||||
def test_recommend_page_keeps_second_step_entry_mode():
|
||||
from douyin.core.fudai_modules import run_once_basic_helpers as ronce
|
||||
|
||||
s = MagicMock()
|
||||
s.device_id = "D"
|
||||
s.resolution_ratio_x = 1080
|
||||
s.resolution_ratio_y = 2400
|
||||
s.FOLLOW_TAB_CLICK_DELAY = 0
|
||||
s.ENTER_LIVE_CLICK_DELAY = 0
|
||||
s.ENTER_LIVE_FINAL_DELAY = 0
|
||||
s.RETURN_STEP_DELAY_SECONDS = 0
|
||||
s.operation.click = MagicMock()
|
||||
s.operation.delay = MagicMock()
|
||||
s._log_kv = MagicMock()
|
||||
|
||||
def _click_ocr_live_tab(analyser):
|
||||
analyser.operation.click(analyser.device_id, 720, 162)
|
||||
return 720, 162, "ocr_live_tab"
|
||||
|
||||
with patch(
|
||||
"douyin.handlers.live_tab_ocr.click_live_tab_on_recommend_page",
|
||||
side_effect=_click_ocr_live_tab,
|
||||
), patch(
|
||||
"douyin.handlers.douyin_swipe_handler.SwipeHandler.resolve_reenter_live_click_point",
|
||||
return_value=(540, 1200, "screen_center"),
|
||||
):
|
||||
ronce.dispatch_stage_branch(s, "RECOMMEND_PAGE", {}, 0.0)
|
||||
|
||||
calls = [call.args for call in s.operation.click.call_args_list]
|
||||
assert calls == [("D", 720, 162), ("D", 540, 1200), ("D", 540, 1200)]
|
||||
Reference in new issue
Block a user