fix(countdown): harden minute OCR selection and normalization
This commit is contained in:
1 parent
3ce3a03f62
commit
d696f2c974
2 files changed
+112
-13
No files matched your search
@@ -91,7 +91,7 @@ def _extract_two_digits(text: Any) -> str:
|
||||
|
||||
|
||||
def _build_countdown_candidate_boxes(renwu: int) -> List[Tuple[int, int, int, int]]:
|
||||
"""构建倒计时候选框列表:主候选优先,其余候选按稳定顺序补充。"""
|
||||
"""构建倒计时候选框列表:主候选优先,限制在同一纵向带,避免扫到价值/文案行。"""
|
||||
from douyin.fudai.core.coords import (
|
||||
COUNTDOWN_1010_1130,
|
||||
COUNTDOWN_1110_1230,
|
||||
@@ -108,6 +108,13 @@ def _build_countdown_candidate_boxes(renwu: int) -> List[Tuple[int, int, int, in
|
||||
else:
|
||||
primary = COUNTDOWN_1460_1580
|
||||
|
||||
def _center_y(box: Tuple[int, int, int, int]) -> int:
|
||||
return int((int(box[1]) + int(box[3])) / 2)
|
||||
|
||||
# 经验阈值:仅在主候选上下约 220px 内补充候选,避免混入下方“参考价/价值”行。
|
||||
primary_cy = _center_y(primary)
|
||||
max_center_delta = 220
|
||||
|
||||
ordered: List[Tuple[int, int, int, int]] = []
|
||||
for box in (
|
||||
primary,
|
||||
@@ -116,6 +123,8 @@ def _build_countdown_candidate_boxes(renwu: int) -> List[Tuple[int, int, int, in
|
||||
COUNTDOWN_1010_1130,
|
||||
COUNTDOWN_1460_1580,
|
||||
):
|
||||
if box != primary and abs(_center_y(box) - primary_cy) > max_center_delta:
|
||||
continue
|
||||
if box not in ordered:
|
||||
ordered.append(box)
|
||||
return ordered
|
||||
@@ -229,6 +238,22 @@ def _pick_best_countdown_candidate(
|
||||
return best
|
||||
|
||||
|
||||
def _normalize_countdown_pair(mm_raw: Any, ss_raw: Any) -> Tuple[str, str]:
|
||||
"""统一净化倒计时分秒,保证输出仅为 00~59 两位数字;非法则返回空。"""
|
||||
mm = _extract_two_digits(mm_raw)
|
||||
ss = _extract_two_digits(ss_raw)
|
||||
if not mm or not ss:
|
||||
return "", ""
|
||||
try:
|
||||
mm_i = int(mm)
|
||||
ss_i = int(ss)
|
||||
except Exception:
|
||||
return "", ""
|
||||
if 0 <= mm_i <= 59 and 0 <= ss_i <= 59:
|
||||
return f"{mm_i:02d}", f"{ss_i:02d}"
|
||||
return "", ""
|
||||
|
||||
|
||||
def _select_countdown_from_candidates(
|
||||
analyser: Any,
|
||||
candidate_meta: List[Dict[str, Any]],
|
||||
@@ -291,11 +316,14 @@ def _select_countdown_from_candidates(
|
||||
except Exception:
|
||||
pass
|
||||
if best and bool(best.get("valid")):
|
||||
mm_sel = str(best.get("mm") or "")
|
||||
ss_sel = str(best.get("ss") or "")
|
||||
mm_sel, ss_sel = _normalize_countdown_pair(best.get("mm"), best.get("ss"))
|
||||
if mm_sel and ss_sel:
|
||||
return mm_sel, ss_sel, best
|
||||
return primary_mm_raw, primary_ss_raw, best
|
||||
|
||||
mm_primary, ss_primary = _normalize_countdown_pair(primary_mm_raw, primary_ss_raw)
|
||||
if mm_primary and ss_primary:
|
||||
return mm_primary, ss_primary, best
|
||||
return "", "", best
|
||||
|
||||
|
||||
def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, float]:
|
||||
@@ -554,7 +582,13 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
if picked:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"倒计时候选选择结果",
|
||||
(
|
||||
"倒计时候选选择结果"
|
||||
f" idx={int(picked.get('idx', -1) or -1)}"
|
||||
f" mm={str(picked.get('mm') or '')}"
|
||||
f" ss={str(picked.get('ss') or '')}"
|
||||
f" valid={bool(picked.get('valid'))}"
|
||||
),
|
||||
chosen_idx=int(picked.get("idx", -1) or -1),
|
||||
chosen_is_primary=bool(picked.get("is_primary")),
|
||||
chosen_mm=str(picked.get("mm") or ""),
|
||||
@@ -563,6 +597,15 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
chosen_valid=bool(picked.get("valid")),
|
||||
candidates=len(countdown_candidate_meta),
|
||||
)
|
||||
# 回写连续性状态:供下一次候选评分使用(同直播间递减约束)。
|
||||
try:
|
||||
room_for_state = str(live_room_title_text_raw or live_room_title_raw or "").strip()
|
||||
mm_state, ss_state = _normalize_countdown_pair(countdown_mm_raw, countdown_ss_raw)
|
||||
if room_for_state and mm_state and ss_state:
|
||||
analyser._countdown_last_room_title = room_for_state
|
||||
analyser._last_countdown_total_seconds = int(mm_state) * 60 + int(ss_state)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as e_pick:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
@@ -645,13 +688,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 ""),
|
||||
)
|
||||
if picked_heavy:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"重OCR后倒计时候选复选结果",
|
||||
chosen_idx=int(picked_heavy.get("idx", -1) or -1),
|
||||
chosen_is_primary=bool(picked_heavy.get("is_primary")),
|
||||
chosen_mm=str(picked_heavy.get("mm") or ""),
|
||||
if picked_heavy:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
(
|
||||
"重OCR后倒计时候选复选结果"
|
||||
f" idx={int(picked_heavy.get('idx', -1) or -1)}"
|
||||
f" mm={str(picked_heavy.get('mm') or '')}"
|
||||
f" ss={str(picked_heavy.get('ss') or '')}"
|
||||
f" valid={bool(picked_heavy.get('valid'))}"
|
||||
),
|
||||
chosen_idx=int(picked_heavy.get("idx", -1) or -1),
|
||||
chosen_is_primary=bool(picked_heavy.get("is_primary")),
|
||||
chosen_mm=str(picked_heavy.get("mm") or ""),
|
||||
chosen_ss=str(picked_heavy.get("ss") or ""),
|
||||
chosen_score=int(picked_heavy.get("score", 0) or 0),
|
||||
chosen_valid=bool(picked_heavy.get("valid")),
|
||||
@@ -878,7 +927,13 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
if picked_fb:
|
||||
analyser._log_kv(
|
||||
"倒计时",
|
||||
"备用并行倒计时候选选择结果",
|
||||
(
|
||||
"备用并行倒计时候选选择结果"
|
||||
f" idx={int(picked_fb.get('idx', -1) or -1)}"
|
||||
f" mm={str(picked_fb.get('mm') or '')}"
|
||||
f" ss={str(picked_fb.get('ss') or '')}"
|
||||
f" valid={bool(picked_fb.get('valid'))}"
|
||||
),
|
||||
chosen_idx=int(picked_fb.get("idx", -1) or -1),
|
||||
chosen_is_primary=bool(picked_fb.get("is_primary")),
|
||||
chosen_mm=str(picked_fb.get("mm") or ""),
|
||||
@@ -887,6 +942,16 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
chosen_valid=bool(picked_fb.get("valid")),
|
||||
candidates=len(countdown_candidate_meta),
|
||||
)
|
||||
try:
|
||||
mm_state, ss_state = _normalize_countdown_pair(countdown_mm_raw, countdown_ss_raw)
|
||||
room_for_state = str(
|
||||
getattr(analyser, "current_room_title_raw", "") or ""
|
||||
).strip()
|
||||
if room_for_state and mm_state and ss_state:
|
||||
analyser._countdown_last_room_title = room_for_state
|
||||
analyser._last_countdown_total_seconds = int(mm_state) * 60 + int(ss_state)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from douyin.fudai.douyin_fudai_contain_impl import (
|
||||
_build_countdown_candidate_boxes,
|
||||
_select_countdown_from_candidates,
|
||||
)
|
||||
from douyin.fudai.douyin_fudai_core_pkg.countdown_parse import (
|
||||
@@ -127,3 +128,36 @@ def test_parse_countdown_rejects_out_of_range_mm_ss():
|
||||
# 分钟与秒都越界时,必须返回空字符串,避免误参与
|
||||
time_text = parse_countdown_time_text(analyser, "99", "88")
|
||||
assert time_text == ""
|
||||
|
||||
|
||||
def test_select_countdown_returns_empty_when_primary_invalid_and_no_valid_candidate():
|
||||
analyser = _mk_analyser()
|
||||
candidate_meta = [
|
||||
{
|
||||
"idx": 0,
|
||||
"box": (390, 1110, 690, 1230),
|
||||
"is_primary": True,
|
||||
"mm_result_key": "countdown_mm",
|
||||
"ss_result_key": "countdown_ss",
|
||||
}
|
||||
]
|
||||
|
||||
mm, ss, best = _select_countdown_from_candidates(
|
||||
analyser,
|
||||
candidate_meta,
|
||||
primary_mm_raw="99",
|
||||
primary_ss_raw="88",
|
||||
extra_results={},
|
||||
room_title_hint="直播间A",
|
||||
)
|
||||
|
||||
assert mm == ""
|
||||
assert ss == ""
|
||||
assert best is not None
|
||||
assert bool(best.get("valid")) is False
|
||||
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user