refactor(fudai_core): 合并 4 个 renwu 分支截图准备到 cut_regions (1825 → 1651)
get_fudai_contain L160-334 按 renwu (1/2/3/其他) 重复 4 次相似的 175 行 分支,每个分支都做"内容/倒计时(切两段)/价值"4 次 cut_pic + 可选调试截图, 唯一差异只是 countdown_box 常量。 抽到 douyin_fudai_core_pkg/cut_regions.py: - _select_countdown_box(renwu) 选坐标 - cut_detail_regions(analyser, renwu, save_debug_images) 统一切割 原 175 行 4 个重复分支替换为 8 行调用,净减 167 行。 新增 12 个单元测试覆盖: - _select_countdown_box 4 种 renwu - cut_detail_regions 调 cut_pic 4 次 / 名称顺序 / 30%-30% 分割比例 - save_debug_images 开关效果 1272 passed (+12)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
03b471513d
commit
457edcfdc8
3 files changed
+242
-173
No files matched your search
@@ -157,181 +157,15 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
from douyin.fudai.douyin_fudai_core_pkg.heavy_ocr import run_heavy_ocr
|
||||
return run_heavy_ocr(analyser, fields)
|
||||
|
||||
countdown_box = None
|
||||
# 使用集中坐标常量,避免在代码中硬编码
|
||||
from douyin.fudai.core.coords import (
|
||||
VALUE_BOX_TARGET,
|
||||
CONTENT_BOX,
|
||||
COUNTDOWN_1110_1230,
|
||||
COUNTDOWN_1140_1290,
|
||||
COUNTDOWN_1010_1130,
|
||||
COUNTDOWN_1460_1580,
|
||||
TASK_LIST,
|
||||
PARTICIPATE_BUTTON,
|
||||
LIVE_ROOM_TITLE,
|
||||
)
|
||||
|
||||
# 切割直播间标题图片(用于并行OCR)
|
||||
# 切割直播间标题图片(用于并行 OCR)
|
||||
from douyin.fudai.core.coords import LIVE_ROOM_TITLE
|
||||
analyser.cut_pic(*LIVE_ROOM_TITLE, "", "live_room_title_mid", analyser.y_pianyi)
|
||||
|
||||
value_box = VALUE_BOX_TARGET
|
||||
if renwu == 2: # 如果是2个任务的
|
||||
# 优化:扩大Y范围向上,确保包含任务描述(如"观看X分钟"等)
|
||||
# 从1240向上扩展到1100,确保包含任务描述区域
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋内容", CONTENT_BOX, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
*CONTENT_BOX, "", "福袋内容", analyser.y_pianyi
|
||||
) # 福袋内容详情(包含任务描述)
|
||||
# 优化:稍微扩大高度(1220->1230),确保包含完整的"后开奖"文字
|
||||
countdown_box = COUNTDOWN_1110_1230
|
||||
# 直接进行分割裁剪(分钟和秒),不再裁剪完整倒计时区域
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
seg1 = max(1, int(w * 0.3)) # 前30%:分钟数字框
|
||||
seg2 = max(1, int(w * 0.3)) # 中间30%:秒数字框
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋倒计时_分钟", (x1, y1, x1 + seg1, y2), analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1, y1, x1 + seg1, y2, "", "福袋倒计时_分钟", analyser.y_pianyi
|
||||
)
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser,
|
||||
"福袋倒计时_秒",
|
||||
(x1 + seg1, y1, x1 + seg1 + seg2, y2),
|
||||
analyser.y_pianyi,
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1 + seg1, y1, x1 + seg1 + seg2, y2, "", "福袋倒计时_秒", analyser.y_pianyi
|
||||
)
|
||||
# 优化:向上扩大范围(1460->1440),防止识别不完整
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋价值", value_box, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(*value_box, "", "福袋价值", analyser.y_pianyi)
|
||||
elif renwu == 1: # 如果是1个任务的
|
||||
# 优化:扩大Y范围向上,确保包含任务描述
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋内容", CONTENT_BOX, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
*CONTENT_BOX, "", "福袋内容", analyser.y_pianyi
|
||||
) # 福袋内容详情(包含任务描述)
|
||||
# 优化:向上扩大区域,确保完整识别倒计时数字
|
||||
countdown_box = COUNTDOWN_1140_1290
|
||||
# 直接进行分割裁剪(分钟和秒),不再裁剪完整倒计时区域
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
seg1 = max(1, int(w * 0.3)) # 前30%:分钟数字框
|
||||
seg2 = max(1, int(w * 0.3)) # 中间30%:秒数字框
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋倒计时_分钟", (x1, y1, x1 + seg1, y2), analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1, y1, x1 + seg1, y2, "", "福袋倒计时_分钟", analyser.y_pianyi
|
||||
)
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser,
|
||||
"福袋倒计时_秒",
|
||||
(x1 + seg1, y1, x1 + seg1 + seg2, y2),
|
||||
analyser.y_pianyi,
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1 + seg1, y1, x1 + seg1 + seg2, y2, "", "福袋倒计时_秒", analyser.y_pianyi
|
||||
)
|
||||
# 优化:向上扩大范围(1520->1500),防止识别不完整
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋价值", value_box, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(*value_box, "", "福袋价值", analyser.y_pianyi)
|
||||
elif renwu == 3: # 如果是3个任务的
|
||||
# 优化:扩大Y范围向上,确保包含任务描述
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋内容", CONTENT_BOX, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
*CONTENT_BOX, "", "福袋内容", analyser.y_pianyi
|
||||
) # 福袋内容详情(包含任务描述)
|
||||
# 优化:稍微扩大高度
|
||||
countdown_box = COUNTDOWN_1010_1130
|
||||
# 直接进行分割裁剪(分钟和秒),不再裁剪完整倒计时区域
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
seg1 = max(1, int(w * 0.3)) # 前30%:分钟数字框
|
||||
seg2 = max(1, int(w * 0.3)) # 中间30%:秒数字框
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋倒计时_分钟", (x1, y1, x1 + seg1, y2), analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1, y1, x1 + seg1, y2, "", "福袋倒计时_分钟", analyser.y_pianyi
|
||||
)
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser,
|
||||
"福袋倒计时_秒",
|
||||
(x1 + seg1, y1, x1 + seg1 + seg2, y2),
|
||||
analyser.y_pianyi,
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1 + seg1, y1, x1 + seg1 + seg2, y2, "", "福袋倒计时_秒", analyser.y_pianyi
|
||||
)
|
||||
# 优化:向上扩大范围(1380->1360),防止识别不完整
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋价值", value_box, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(*value_box, "", "福袋价值", analyser.y_pianyi)
|
||||
else:
|
||||
# 优化:稍微缩小宽度,增加高度
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋内容", CONTENT_BOX, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
*CONTENT_BOX, "", "福袋内容", analyser.y_pianyi
|
||||
) # 福袋内容详情
|
||||
# 优化:稍微扩大高度
|
||||
countdown_box = COUNTDOWN_1460_1580
|
||||
# 直接进行分割裁剪(分钟和秒),不再裁剪完整倒计时区域
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
seg1 = max(1, int(w * 0.3)) # 前30%:分钟数字框
|
||||
seg2 = max(1, int(w * 0.3)) # 中间30%:秒数字框
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋倒计时_分钟", (x1, y1, x1 + seg1, y2), analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1, y1, x1 + seg1, y2, "", "福袋倒计时_分钟", analyser.y_pianyi
|
||||
)
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser,
|
||||
"福袋倒计时_秒",
|
||||
(x1 + seg1, y1, x1 + seg1 + seg2, y2),
|
||||
analyser.y_pianyi,
|
||||
)
|
||||
analyser.cut_pic(
|
||||
x1 + seg1, y1, x1 + seg1 + seg2, y2, "", "福袋倒计时_秒", analyser.y_pianyi
|
||||
)
|
||||
# 优化:向上扩大范围(1820->1800),防止识别不完整
|
||||
if save_debug_images:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋价值", value_box, analyser.y_pianyi
|
||||
)
|
||||
analyser.cut_pic(*value_box, "", "福袋价值", analyser.y_pianyi)
|
||||
# 切割内容/倒计时/价值(按 renwu 选 countdown_box)
|
||||
from douyin.fudai.douyin_fudai_core_pkg.cut_regions import cut_detail_regions
|
||||
countdown_box = cut_detail_regions(
|
||||
analyser, renwu, save_debug_images=save_debug_images
|
||||
)
|
||||
|
||||
# 准备任务列表区域的截图(用于并行OCR)
|
||||
# 任务列表区域(任务位置:50,1621到1032,1960),用于检测"观看X分钟"任务
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"""douyin_fudai_core_pkg.cut_regions - 福袋详情区域截图准备
|
||||
|
||||
把 get_fudai_contain 中按 renwu 分 4 个分支的"内容/倒计时/价值"截图准备逻辑
|
||||
合并成一个 helper。4 个分支唯一的差异是 countdown_box,其它操作都一样。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Tuple, Optional
|
||||
|
||||
|
||||
def _select_countdown_box(renwu: int) -> Tuple[int, int, int, int]:
|
||||
"""根据任务数选 countdown_box 坐标常量。"""
|
||||
from douyin.fudai.core.coords import (
|
||||
COUNTDOWN_1010_1130,
|
||||
COUNTDOWN_1110_1230,
|
||||
COUNTDOWN_1140_1290,
|
||||
COUNTDOWN_1460_1580,
|
||||
)
|
||||
if renwu == 2:
|
||||
return COUNTDOWN_1110_1230
|
||||
if renwu == 1:
|
||||
return COUNTDOWN_1140_1290
|
||||
if renwu == 3:
|
||||
return COUNTDOWN_1010_1130
|
||||
return COUNTDOWN_1460_1580
|
||||
|
||||
|
||||
def cut_detail_regions(
|
||||
analyser,
|
||||
renwu: int,
|
||||
*,
|
||||
save_debug_images: bool = False,
|
||||
) -> Tuple[int, int, int, int]:
|
||||
"""根据 renwu 切割福袋详情的"内容/倒计时分钟/倒计时秒/价值"4 个区域。
|
||||
|
||||
倒计时区域内部还会按 30%/30% 比例分割成"分钟"和"秒"两段单独切。
|
||||
|
||||
Args:
|
||||
analyser: fudai_analyse 实例(提供 cut_pic / y_pianyi)
|
||||
renwu: 任务数量(1/2/3,其它视为默认)
|
||||
save_debug_images: 是否保存全屏带 OCR 区域标注的调试截图
|
||||
|
||||
Returns:
|
||||
countdown_box: 选中的倒计时区域 (x1, y1, x2, y2),供后续 _read_countdown_split 使用
|
||||
"""
|
||||
from douyin.fudai.core.coords import (
|
||||
VALUE_BOX_TARGET,
|
||||
CONTENT_BOX,
|
||||
)
|
||||
try:
|
||||
from douyin.fudai.core.fudai_debug_utils import (
|
||||
_save_full_screenshot_with_ocr_region,
|
||||
)
|
||||
except Exception:
|
||||
_save_full_screenshot_with_ocr_region = None # type: ignore[assignment]
|
||||
|
||||
countdown_box = _select_countdown_box(renwu)
|
||||
value_box = VALUE_BOX_TARGET
|
||||
|
||||
# 1. 福袋内容
|
||||
if save_debug_images and _save_full_screenshot_with_ocr_region is not None:
|
||||
try:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋内容", CONTENT_BOX, analyser.y_pianyi
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.cut_pic(*CONTENT_BOX, "", "福袋内容", analyser.y_pianyi)
|
||||
|
||||
# 2. 倒计时区域分割(前 30% 分钟 + 中间 30% 秒)
|
||||
x1, y1, x2, y2 = countdown_box
|
||||
w = max(1, int(x2 - x1))
|
||||
seg1 = max(1, int(w * 0.3)) # 前 30%:分钟数字框
|
||||
seg2 = max(1, int(w * 0.3)) # 中间 30%:秒数字框
|
||||
|
||||
if save_debug_images and _save_full_screenshot_with_ocr_region is not None:
|
||||
try:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋倒计时_分钟", (x1, y1, x1 + seg1, y2), analyser.y_pianyi
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.cut_pic(
|
||||
x1, y1, x1 + seg1, y2, "", "福袋倒计时_分钟", analyser.y_pianyi
|
||||
)
|
||||
|
||||
if save_debug_images and _save_full_screenshot_with_ocr_region is not None:
|
||||
try:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser,
|
||||
"福袋倒计时_秒",
|
||||
(x1 + seg1, y1, x1 + seg1 + seg2, y2),
|
||||
analyser.y_pianyi,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.cut_pic(
|
||||
x1 + seg1, y1, x1 + seg1 + seg2, y2, "", "福袋倒计时_秒", analyser.y_pianyi
|
||||
)
|
||||
|
||||
# 3. 福袋价值
|
||||
if save_debug_images and _save_full_screenshot_with_ocr_region is not None:
|
||||
try:
|
||||
_save_full_screenshot_with_ocr_region(
|
||||
analyser, "福袋价值", value_box, analyser.y_pianyi
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.cut_pic(*value_box, "", "福袋价值", analyser.y_pianyi)
|
||||
|
||||
return countdown_box
|
||||
@@ -0,0 +1,124 @@
|
||||
"""douyin_fudai_core_pkg.cut_regions 单元测试。
|
||||
|
||||
覆盖:
|
||||
- _select_countdown_box 4 种 renwu 值返回正确常量
|
||||
- cut_detail_regions 调用 cut_pic 4 次(内容 + 分钟 + 秒 + 价值)
|
||||
- 倒计时分割比例(30% / 30%)正确
|
||||
- save_debug_images 开关效果
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from douyin.fudai.douyin_fudai_core_pkg.cut_regions import (
|
||||
_select_countdown_box,
|
||||
cut_detail_regions,
|
||||
)
|
||||
|
||||
|
||||
def _mk_analyser():
|
||||
a = MagicMock()
|
||||
a.y_pianyi = 0
|
||||
a.cut_pic = MagicMock(return_value=True)
|
||||
return a
|
||||
|
||||
|
||||
# ============================================================
|
||||
# _select_countdown_box
|
||||
# ============================================================
|
||||
|
||||
class TestSelectCountdownBox:
|
||||
def test_renwu_1(self):
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
|
||||
assert _select_countdown_box(1) == COUNTDOWN_1140_1290
|
||||
|
||||
def test_renwu_2(self):
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
||||
assert _select_countdown_box(2) == COUNTDOWN_1110_1230
|
||||
|
||||
def test_renwu_3(self):
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
|
||||
assert _select_countdown_box(3) == COUNTDOWN_1010_1130
|
||||
|
||||
def test_renwu_other_falls_to_default(self):
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1460_1580
|
||||
assert _select_countdown_box(0) == COUNTDOWN_1460_1580
|
||||
assert _select_countdown_box(99) == COUNTDOWN_1460_1580
|
||||
assert _select_countdown_box(-1) == COUNTDOWN_1460_1580
|
||||
|
||||
|
||||
# ============================================================
|
||||
# cut_detail_regions
|
||||
# ============================================================
|
||||
|
||||
class TestCutDetailRegions:
|
||||
def test_returns_countdown_box(self):
|
||||
a = _mk_analyser()
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
||||
result = cut_detail_regions(a, renwu=2)
|
||||
assert result == COUNTDOWN_1110_1230
|
||||
|
||||
def test_calls_cut_pic_4_times(self):
|
||||
"""内容 + 分钟 + 秒 + 价值 = 4 次 cut_pic 调用。"""
|
||||
a = _mk_analyser()
|
||||
cut_detail_regions(a, renwu=2)
|
||||
assert a.cut_pic.call_count == 4
|
||||
|
||||
def test_cut_names_in_order(self):
|
||||
a = _mk_analyser()
|
||||
cut_detail_regions(a, renwu=2)
|
||||
# 第 1 个 positional arg 是坐标,第 6 个是 name
|
||||
# 调用签名:cut_pic(x1, y1, x2, y2, target, name, y_pianyi)
|
||||
names = [c.args[5] for c in a.cut_pic.call_args_list]
|
||||
assert names == ["福袋内容", "福袋倒计时_分钟", "福袋倒计时_秒", "福袋价值"]
|
||||
|
||||
def test_countdown_split_ratio(self):
|
||||
"""倒计时 box 应按 30% 分钟 + 30% 秒 切分。"""
|
||||
a = _mk_analyser()
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1110_1230
|
||||
x1, y1, x2, y2 = COUNTDOWN_1110_1230
|
||||
w = x2 - x1
|
||||
seg = int(w * 0.3)
|
||||
|
||||
cut_detail_regions(a, renwu=2)
|
||||
|
||||
# 倒计时分钟 = 第 2 次 cut_pic 调用
|
||||
mm_call = a.cut_pic.call_args_list[1]
|
||||
assert mm_call.args[0] == x1
|
||||
assert mm_call.args[2] == x1 + seg
|
||||
|
||||
# 倒计时秒 = 第 3 次调用
|
||||
ss_call = a.cut_pic.call_args_list[2]
|
||||
assert ss_call.args[0] == x1 + seg
|
||||
assert ss_call.args[2] == x1 + seg + seg
|
||||
|
||||
def test_save_debug_images_disabled_default(self):
|
||||
a = _mk_analyser()
|
||||
with patch(
|
||||
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
|
||||
) as mock_save:
|
||||
cut_detail_regions(a, renwu=2, save_debug_images=False)
|
||||
mock_save.assert_not_called()
|
||||
|
||||
def test_save_debug_images_enabled_calls_4_times(self):
|
||||
"""启用后每个区域都保存:内容 + 分钟 + 秒 + 价值 = 4 次。"""
|
||||
a = _mk_analyser()
|
||||
with patch(
|
||||
"douyin.fudai.core.fudai_debug_utils._save_full_screenshot_with_ocr_region"
|
||||
) as mock_save:
|
||||
cut_detail_regions(a, renwu=2, save_debug_images=True)
|
||||
assert mock_save.call_count == 4
|
||||
|
||||
def test_renwu_1_uses_correct_box(self):
|
||||
a = _mk_analyser()
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1140_1290
|
||||
result = cut_detail_regions(a, renwu=1)
|
||||
assert result == COUNTDOWN_1140_1290
|
||||
|
||||
def test_renwu_3_uses_correct_box(self):
|
||||
a = _mk_analyser()
|
||||
from douyin.fudai.core.coords import COUNTDOWN_1010_1130
|
||||
result = cut_detail_regions(a, renwu=3)
|
||||
assert result == COUNTDOWN_1010_1130
|
||||
Reference in new issue
Block a user