fix: keep parallel OCR stable and add unknown-button fallback
This commit is contained in:
1 parent
a68307d7fd
commit
61dfc1c15c
6 files changed
+149
-2
No files matched your search
@@ -109,9 +109,14 @@ def analyse_pic_word(
|
||||
img_np = np.array(img1)
|
||||
ocr_confidence_info = None
|
||||
|
||||
# 并行模式下优先使用线程私有 OCR 实例,避免共享实例并发导致底层崩溃。
|
||||
try:
|
||||
ocr_obj = getattr(self, "ocr", None)
|
||||
ocr_type = getattr(self, "ocr_type", None)
|
||||
get_ocr = getattr(self, "_get_ocr_for_current_thread", None)
|
||||
if callable(get_ocr):
|
||||
ocr_obj, ocr_type = get_ocr()
|
||||
else:
|
||||
ocr_obj = getattr(self, "ocr", None)
|
||||
ocr_type = getattr(self, "ocr_type", None)
|
||||
except Exception:
|
||||
ocr_obj = None
|
||||
ocr_type = None
|
||||
|
||||
@@ -553,6 +553,30 @@ def attend_choujiang(analyser) -> bool:
|
||||
)
|
||||
return True
|
||||
|
||||
# 并行 OCR 命中文本但状态未知:执行一次固定坐标参与点击兜底。
|
||||
# 这不是 OCR 降级,不会触发额外串行识别;仅避免误识别文案导致整轮放弃。
|
||||
if attend_button_text:
|
||||
analyser.click(*PARTICIPATE_BTN_CLICK)
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"并行OCR文本未知,执行固定坐标兜底点击",
|
||||
action="点击参与",
|
||||
mode="兜底",
|
||||
button_text=(attend_button_text[:100] if attend_button_text else ""),
|
||||
)
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="attempt",
|
||||
reason="unknown_button_text_fallback_click",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
# 未命中任何已知按钮文案时,避免死循环:限次重试并稍作等待
|
||||
click_times += 1
|
||||
if click_times < 2:
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
"""福袋按钮 OCR 状态判定与回写。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
|
||||
def decide_button_status(button_text: str) -> str:
|
||||
txt = str(button_text or "").strip()
|
||||
# 参与按钮 OCR 区域偶发误读到提示文案(例如“直播间,否则会失去抽奖机会”),
|
||||
# 这类文本不代表可点击状态,统一归为未知,交给后续兜底路径处理。
|
||||
if "失去抽奖" in txt or "否则会失去" in txt:
|
||||
return "未知状态"
|
||||
if "参与成功" in txt or "等待开奖" in txt:
|
||||
return "已参与"
|
||||
if "加入粉丝团" in txt or "粉丝团" in txt:
|
||||
return "需要加入粉丝团"
|
||||
if "评论" in txt or "发表评论" in txt or "一键发表评论" in txt:
|
||||
return "需要评论"
|
||||
if "还需看播" in txt or "开始观看" in txt:
|
||||
return "需要观看"
|
||||
if "未达成" in txt:
|
||||
return "未达成要求"
|
||||
if "参与抽奖" in txt or "参与" in txt:
|
||||
return "可以参与"
|
||||
return "未知状态"
|
||||
|
||||
|
||||
def apply_button_ocr_result(
|
||||
analyser,
|
||||
*,
|
||||
button_text_raw: Any,
|
||||
button_status: str,
|
||||
fudai_content_raw: Any,
|
||||
task_list_raw: Any,
|
||||
countdown_mm_raw: Any,
|
||||
countdown_ss_raw: Any,
|
||||
value_raw: Any,
|
||||
live_room_title_raw: Any,
|
||||
log_prefix: str,
|
||||
record_room_pos: bool = False,
|
||||
) -> None:
|
||||
"""将按钮 OCR 判定结果与相关字段回写 analyser。"""
|
||||
button_text = str(button_text_raw or "").strip()
|
||||
analyser._fudai_button_status = button_status
|
||||
analyser._fudai_button_text = button_text
|
||||
analyser.button_text_raw = button_text_raw
|
||||
analyser.fudai_content_raw = fudai_content_raw
|
||||
analyser.task_list_raw = task_list_raw
|
||||
analyser.countdown_mm_raw = countdown_mm_raw
|
||||
analyser.countdown_ss_raw = countdown_ss_raw
|
||||
analyser.value_raw = value_raw
|
||||
analyser.live_room_title_raw = live_room_title_raw
|
||||
analyser._fudai_button_ocr_time = time.time()
|
||||
if record_room_pos:
|
||||
try:
|
||||
analyser._fudai_button_room_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
except Exception:
|
||||
pass
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
f"{log_prefix}按钮状态判断",
|
||||
status=button_status,
|
||||
button_text=button_text if button_text else "",
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from douyin.fudai.core.fudai_attend import attend_choujiang
|
||||
|
||||
|
||||
def test_attend_choujiang_fallback_click_on_unknown_button_text() -> None:
|
||||
analyser = MagicMock()
|
||||
analyser._fudai_button_status = "未知状态"
|
||||
analyser._fudai_button_text = "直播间,否则会失去抽奖机会"
|
||||
analyser._fudai_button_ocr_time = 10**12
|
||||
analyser._fudai_button_room_pos = 0
|
||||
analyser.room_pos = 0
|
||||
analyser._run_id = "test-run"
|
||||
analyser._log_kv = MagicMock()
|
||||
analyser.click = MagicMock()
|
||||
analyser.operation = MagicMock()
|
||||
analyser.operation.delay = MagicMock()
|
||||
analyser.last_attend_fail_reason = ""
|
||||
|
||||
ok = attend_choujiang(analyser)
|
||||
|
||||
assert ok is True
|
||||
analyser.click.assert_called_once()
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from douyin.fudai.douyin_fudai_core_pkg.button_state import decide_button_status
|
||||
|
||||
|
||||
def test_decide_button_status_unknown_for_notice_text() -> None:
|
||||
assert decide_button_status("直播间,否则会失去抽奖机会") == "未知状态"
|
||||
|
||||
@@ -39,6 +39,7 @@ def _mk_self(*, ocr=None, ocr_type=None, save_debug=False):
|
||||
s = MagicMock()
|
||||
s.ocr = ocr
|
||||
s.ocr_type = ocr_type
|
||||
s._get_ocr_for_current_thread = MagicMock(return_value=(ocr, ocr_type))
|
||||
s.save_ocr_debug_images = save_debug
|
||||
s._find_pic_path = MagicMock(return_value="")
|
||||
s._ocr_coords_lock = MagicMock()
|
||||
@@ -95,6 +96,23 @@ class TestEarlyReturns:
|
||||
# ============================================================
|
||||
|
||||
class TestOcrCallPaths:
|
||||
def test_prefers_thread_local_ocr_getter(self, tmp_path):
|
||||
shared_ocr = MagicMock()
|
||||
shared_ocr.predict = MagicMock(return_value=[{"should": "not_used"}])
|
||||
worker_ocr = MagicMock()
|
||||
worker_ocr.predict = MagicMock(return_value=[{"some": "result"}])
|
||||
s = _mk_self(ocr=shared_ocr, ocr_type="paddleocr")
|
||||
s._get_ocr_for_current_thread = MagicMock(
|
||||
return_value=(worker_ocr, "paddleocr")
|
||||
)
|
||||
s.extract_ocr_content = MagicMock(return_value=("thread local text", None))
|
||||
pic_path = _make_real_pic(tmp_path)
|
||||
|
||||
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
|
||||
assert result == "thread local text"
|
||||
worker_ocr.predict.assert_called_once()
|
||||
shared_ocr.predict.assert_not_called()
|
||||
|
||||
def test_easyocr_path(self, tmp_path):
|
||||
ocr = MagicMock()
|
||||
ocr.readtext = MagicMock(return_value=[("bbox", "识别出的文本", 0.95)])
|
||||
|
||||
Reference in new issue
Block a user