From c2010084cfb6eeabfe02c7063d2de6603ee67882 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 1 Jun 2026 16:37:11 +0800 Subject: [PATCH] fix(fudai): root-fix countdown minute misread with full-line mm:ss priority --- douyin/fudai/douyin_fudai_contain_impl.py | 96 ++++++++++++++++++- .../douyin_fudai_core_pkg/countdown_parse.py | 4 +- .../fudai/douyin_fudai_core_pkg/ocr_tasks.py | 20 ++++ tests/test_countdown_candidate_selection.py | 13 +++ 4 files changed, 128 insertions(+), 5 deletions(-) diff --git a/douyin/fudai/douyin_fudai_contain_impl.py b/douyin/fudai/douyin_fudai_contain_impl.py index e7bd096..12a7a07 100644 --- a/douyin/fudai/douyin_fudai_contain_impl.py +++ b/douyin/fudai/douyin_fudai_contain_impl.py @@ -90,6 +90,26 @@ def _extract_two_digits(text: Any) -> str: return extract_countdown_digits(str(text or ""), max_digits=2) +def _parse_mmss_from_full_text(text: Any) -> Tuple[str, str]: + """从整行倒计时文本中提取 mm:ss,失败返回空。""" + import re + + raw = str(text or "").strip() + if not raw: + return "", "" + m = re.search(r"(\d{1,2})\D+(\d{1,2})", raw) + if not m: + return "", "" + try: + mm_i = int(m.group(1)) + ss_i = int(m.group(2)) + except Exception: + return "", "" + if 0 <= mm_i <= 59 and 0 <= ss_i <= 59: + return f"{mm_i:02d}", f"{ss_i:02d}" + return "", "" + + def _build_countdown_candidate_boxes(renwu: int) -> List[Tuple[int, int, int, int]]: """构建倒计时候选框列表:主候选优先,限制在同一纵向带,避免扫到价值/文案行。""" from douyin.fudai.core.coords import ( @@ -141,6 +161,10 @@ def _cut_countdown_candidate_regions( """按稳定分割比例切分钟/秒候选区域,降低混入“后开奖”等文本的概率。""" x1, y1, x2, y2 = box w = max(1, int(x2 - x1)) + h = max(1, int(y2 - y1)) + # 只截取倒计时数字行,降低把标题文案识别进分钟框的概率。 + digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.34))) + digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.92)))) # 经验分割:分钟在左 0~26%,秒在中部 38%~64%,避开中间冒号与右侧“后开奖”文本。 mm_x1 = x1 @@ -151,16 +175,38 @@ def _cut_countdown_candidate_regions( if save_debug_images: try: _save_full_screenshot_with_ocr_region( - analyser, mm_name, (mm_x1, y1, mm_x2, y2), analyser.y_pianyi + analyser, + mm_name, + (mm_x1, digit_y1, mm_x2, digit_y2), + analyser.y_pianyi, ) _save_full_screenshot_with_ocr_region( - analyser, ss_name, (ss_x1, y1, ss_x2, y2), analyser.y_pianyi + analyser, + ss_name, + (ss_x1, digit_y1, ss_x2, digit_y2), + analyser.y_pianyi, ) except Exception: pass - analyser.cut_pic(mm_x1, y1, mm_x2, y2, "", mm_name, analyser.y_pianyi) - analyser.cut_pic(ss_x1, y1, ss_x2, y2, "", ss_name, analyser.y_pianyi) + analyser.cut_pic( + mm_x1, + digit_y1, + mm_x2, + digit_y2, + "", + mm_name, + analyser.y_pianyi, + ) + analyser.cut_pic( + ss_x1, + digit_y1, + ss_x2, + digit_y2, + "", + ss_name, + analyser.y_pianyi, + ) def _pick_best_countdown_candidate( @@ -371,6 +417,15 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo countdown_box = cut_detail_regions( analyser, renwu, save_debug_images=save_debug_images ) + # 额外整行倒计时图:用于优先解析 mm:ss,降低分钟框被文案污染的风险。 + try: + if save_debug_images: + _save_full_screenshot_with_ocr_region( + analyser, "福袋倒计时_整行", countdown_box, analyser.y_pianyi + ) + analyser.cut_pic(*countdown_box, "", "福袋倒计时_整行", analyser.y_pianyi) + except Exception: + pass # 额外切割倒计时候选区域(用于并行多候选OCR,根治分钟框偏移) countdown_candidate_meta: List[Dict[str, Any]] = [] try: @@ -492,6 +547,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo ocr_content = _ocr_runner.ocr_content ocr_countdown_mm = _ocr_runner.ocr_countdown_mm ocr_countdown_ss = _ocr_runner.ocr_countdown_ss + ocr_countdown_full = _ocr_runner.ocr_countdown_full ocr_value = _ocr_runner.ocr_value ocr_button = _ocr_runner.ocr_button ocr_live_room_title = _ocr_runner.ocr_live_room_title @@ -510,6 +566,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo "task_list": ocr_task_list, "countdown_mm": ocr_countdown_mm, "countdown_ss": ocr_countdown_ss, + "countdown_full": ocr_countdown_full, "value": ocr_value, "button": ocr_button, "live_room_title": ocr_live_room_title, @@ -547,6 +604,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo task_funcs["content"] = ocr_content task_list_raw = fudai_content_raw = countdown_mm_raw = None countdown_ss_raw = value_raw = None + countdown_full_raw = None live_room_title_raw = live_room_title_text_raw = None # 收集结果并记录耗时 @@ -561,6 +619,8 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo countdown_mm_raw = res elif name == "countdown_ss": countdown_ss_raw = res + elif name == "countdown_full": + countdown_full_raw = res elif name == "value": value_raw = res elif name == "button": @@ -579,6 +639,19 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo extra_countdown_results, room_title_hint=str(live_room_title_text_raw or live_room_title_raw or ""), ) + mm_full, ss_full = _parse_mmss_from_full_text(countdown_full_raw) + if mm_full and ss_full: + countdown_mm_raw, countdown_ss_raw = mm_full, ss_full + try: + analyser._log_kv( + "倒计时", + "倒计时整行优先命中,覆盖分割候选结果", + full_text=str(countdown_full_raw or "")[:80], + mm=mm_full, + ss=ss_full, + ) + except Exception: + pass if picked: analyser._log_kv( "倒计时", @@ -634,6 +707,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo has_button=bool(button_text_raw), has_countdown_mm=bool(countdown_mm_raw), has_countdown_ss=bool(countdown_ss_raw), + has_countdown_full=bool(countdown_full_raw), ) # 输出一行紧凑的并行 OCR 汇总,便于日志聚合分析 @@ -688,6 +762,9 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo extra_countdown_results, room_title_hint=str(live_room_title_text_raw or live_room_title_raw or ""), ) + mm_full_h, ss_full_h = _parse_mmss_from_full_text(countdown_full_raw) + if mm_full_h and ss_full_h: + countdown_mm_raw, countdown_ss_raw = mm_full_h, ss_full_h if picked_heavy: analyser._log_kv( "倒计时", @@ -772,6 +849,9 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo "任务列表": task_list_raw[:100] if task_list_raw else "未识别", "倒计时分钟": countdown_mm_raw if countdown_mm_raw else "未识别", "倒计时秒": countdown_ss_raw if countdown_ss_raw else "未识别", + "倒计时整行": ( + str(countdown_full_raw)[:100] if countdown_full_raw else "未识别" + ), "奖品价值": value_raw[:50] if value_raw else "未识别", "按钮文本": button_text_raw if button_text_raw else "未识别", "直播间标题": ( @@ -845,6 +925,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo ocr_content_fallback = _fb_runner.ocr_content ocr_countdown_mm_fallback = _fb_runner.ocr_countdown_mm ocr_countdown_ss_fallback = _fb_runner.ocr_countdown_ss + ocr_countdown_full_fallback = _fb_runner.ocr_countdown_full ocr_value_fallback = _fb_runner.ocr_value ocr_button_fallback = _fb_runner.ocr_button @@ -858,6 +939,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo "task_list": ocr_task_list_fallback, "countdown_mm": ocr_countdown_mm_fallback, "countdown_ss": ocr_countdown_ss_fallback, + "countdown_full": ocr_countdown_full_fallback, "value": ocr_value_fallback, "button": ocr_button_fallback, } @@ -895,6 +977,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo durations = {} task_list_raw = fudai_content_raw = countdown_mm_raw = None countdown_ss_raw = value_raw = button_text_raw = None + countdown_full_raw_fb = None results, durations = run_parallel_tasks(task_funcs, executor) extra_countdown_results_fb: Dict[str, Any] = {} for name, res in results.items(): @@ -906,6 +989,8 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo countdown_mm_raw = res elif name == "countdown_ss": countdown_ss_raw = res + elif name == "countdown_full": + countdown_full_raw_fb = res elif name == "value": value_raw = res elif name == "button": @@ -924,6 +1009,9 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo getattr(analyser, "current_room_title_raw", "") or "" ), ) + mm_fb, ss_fb = _parse_mmss_from_full_text(countdown_full_raw_fb) + if mm_fb and ss_fb: + countdown_mm_raw, countdown_ss_raw = mm_fb, ss_fb if picked_fb: analyser._log_kv( "倒计时", diff --git a/douyin/fudai/douyin_fudai_core_pkg/countdown_parse.py b/douyin/fudai/douyin_fudai_core_pkg/countdown_parse.py index bcb45e4..e0c1042 100644 --- a/douyin/fudai/douyin_fudai_core_pkg/countdown_parse.py +++ b/douyin/fudai/douyin_fudai_core_pkg/countdown_parse.py @@ -131,7 +131,9 @@ def parse_countdown_time_text(analyser, countdown_mm_raw, countdown_ss_raw) -> s mm_int = -1 ss_int = -1 - if 0 <= mm_int <= 59 and 0 <= ss_int <= 59: + # 根修:禁止把疑似噪声位"60"当分钟(典型样例:正601/601)。 + # 合法业务分钟基本在 00~59,但这里对 60 额外收紧,减少错参与风险。 + if 0 <= mm_int <= 59 and 0 <= ss_int <= 59 and mm_int != 60: time_text = f"{mm}:{ss}" else: try: diff --git a/douyin/fudai/douyin_fudai_core_pkg/ocr_tasks.py b/douyin/fudai/douyin_fudai_core_pkg/ocr_tasks.py index 10d3d8b..5f0bc73 100644 --- a/douyin/fudai/douyin_fudai_core_pkg/ocr_tasks.py +++ b/douyin/fudai/douyin_fudai_core_pkg/ocr_tasks.py @@ -173,6 +173,18 @@ class OcrTaskRunner: finally: self._record_duration("ocr_countdown_ss", start) + def ocr_countdown_full(self) -> Optional[str]: + """OCR 识别倒计时整行文本(用于优先提取 mm:ss)。""" + start = time.time() + try: + name_cd = _safe_preprocess(self.analyser, "福袋倒计时_整行") + return self.analyser.operation.analyse_pic_word(name_cd, 0) + except Exception as e: + self._log_warn("OCR识别倒计时整行失败", e) + return None + finally: + self._record_duration("ocr_countdown_full", start) + def ocr_value(self) -> Optional[str]: """OCR 识别福袋价值(普通文本模型,支持小数点)。""" start = time.time() @@ -314,6 +326,14 @@ class OcrTaskRunnerFallback: self._log_warn("备用OCR识别倒计时秒失败", e) return None + def ocr_countdown_full(self): + try: + name_cd = _safe_preprocess(self.analyser, "福袋倒计时_整行") + return self.analyser.operation.analyse_pic_word(name_cd, 0) + except Exception as e: + self._log_warn("备用OCR识别倒计时整行失败", e) + return None + def ocr_value(self): try: return self.analyser.operation.analyse_pic_word("福袋价值", 0) diff --git a/tests/test_countdown_candidate_selection.py b/tests/test_countdown_candidate_selection.py index cab49f1..8fb4e04 100644 --- a/tests/test_countdown_candidate_selection.py +++ b/tests/test_countdown_candidate_selection.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock from douyin.fudai.douyin_fudai_contain_impl import ( _build_countdown_candidate_boxes, + _parse_mmss_from_full_text, _select_countdown_from_candidates, ) from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import ( @@ -161,3 +162,15 @@ def test_build_countdown_candidate_boxes_excludes_far_value_band(): # renwu=2 主候选在 1110~1230,1460~1580 的候选应被过滤,避免扫到“参考价/¥0”。 boxes = _build_countdown_candidate_boxes(2) assert (390, 1460, 690, 1580) not in boxes + + +def test_parse_mmss_from_full_text_extracts_valid_pair(): + mm, ss = _parse_mmss_from_full_text("13:53后开奖") + assert mm == "13" + assert ss == "53" + + +def test_parse_mmss_from_full_text_ignores_non_countdown_text(): + mm, ss = _parse_mmss_from_full_text("直播间,否则会失去抽奖机会") + assert mm == "" + assert ss == ""