Files
douyin_guaji-main/tests/test_finder_numpy_scan.py
T
CodexandClaude Opus 4.7 07cdd662c2 refactor(finder): 抽 numpy 加速扫描段到 finder_numpy_scan (937 → 811)
check_have_fudai_with_ready_screenshot L271-397 是"numpy 加速水平带扫描"
逻辑(126 行):ROI 裁剪 → RGB 颜色掩码 → 列聚合 → 从最优起始列向两侧
交替搜索第一个匹配列 → 写回 last_fudai_x。

抽到 douyin/detection/detection_pkg/finder_numpy_scan.py:
- scan_with_numpy(analyser, pic_new, rx, ry, y_pianyi_val, optimal_x_scaled)
  → (detected, detected_x)
- numpy ImportError / 异常时返回 (False, None),让 caller 走逐像素兜底

主 finder.py 替换为 7 行委托。

新增 8 个单元测试(用真实 PIL.Image + numpy 构造小图):
- 空图返回 False
- 整图目标色命中 + last_fudai_x 写回
- 仅在某段列有目标色,命中位置在该段
- y_pianyi_val 偏移正确传递到扫描带
- ROI 越界返回 False
- optimal_x_scaled 在命中区时优先返回该位置

1321 passed (+8)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 02:15:43 +08:00

158 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""douyin/detection/detection_pkg/finder_numpy_scan 单元测试。
scan_with_numpy 是个有较多 PIL/numpy 交互的纯函数。这里用真实的 PIL.Image
构造小图片做测试,避免 mock 太多。
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
try:
import numpy as np
from PIL import Image
except ImportError: # pragma: no cover
pytest.skip("numpy/PIL unavailable", allow_module_level=True)
def _mk_analyser():
a = MagicMock()
a.last_fudai_x = 0
a.last_find_fudai_time = 0.0
a._log_kv = MagicMock(return_value=None)
return a
def _make_solid_color_image(rgb, w=1080, h=2400):
"""生成纯色图片,方便控制 ROI 内是否有目标颜色。"""
return Image.new("RGB", (w, h), rgb)
def _make_image_with_target_band(target_rgb, x_start, x_end, y_target, w=1080, h=2400):
"""在 (x_start..x_end, y_target..y_target+5) 画上目标色,其它区域为黑。
可控的"目标在某些列出现"的图片,方便测试列扫描。
"""
img = Image.new("RGB", (w, h), (0, 0, 0))
arr = np.array(img)
arr[y_target:y_target + 5, x_start:x_end] = list(target_rgb)
return Image.fromarray(arr)
# 福袋图标颜色掩码:r ∈ [190, 206], g ∈ [173, 196], b ∈ [234, 250]
TARGET_RGB = (200, 185, 240)
NON_TARGET_RGB = (50, 50, 50)
class TestScanWithNumpy:
def test_blank_image_returns_false(self):
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
pic = _make_solid_color_image(NON_TARGET_RGB)
detected, x = scan_with_numpy(a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=200)
assert detected is False
assert x is None
def test_solid_target_color_detects(self):
"""整图都是目标色 → ROI 全部命中 → 应该检测到。"""
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
pic = _make_solid_color_image(TARGET_RGB)
detected, x = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=200
)
assert detected is True
assert x is not None
# optimal_x_scaled=200 落在 ROI 内(157..410),命中应该就在最优位置
assert 157 <= x <= 410
def test_target_in_specific_band(self):
"""只在某段列有目标色 → 命中位置应在该段内。"""
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
# ROI Y 范围:354 - 3 = 351..361
# 在 x ∈ [200, 250], y=355 处画目标色
pic = _make_image_with_target_band(
TARGET_RGB, x_start=200, x_end=250, y_target=353
)
detected, x = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=220
)
assert detected is True
# x 应落在 200..250 范围内(1080 基准坐标)
assert 200 <= x <= 250
def test_writes_back_last_fudai_x_on_hit(self):
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
a.last_fudai_x = 0 # 起初没有
pic = _make_solid_color_image(TARGET_RGB)
detected, x = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=200
)
assert detected is True
assert a.last_fudai_x == x # 命中后写回
def test_does_not_write_back_on_miss(self):
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
a.last_fudai_x = 0
pic = _make_solid_color_image(NON_TARGET_RGB)
detected, _ = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=200
)
assert detected is False
assert a.last_fudai_x == 0 # 未命中不写
def test_y_offset_shifts_scan_band(self):
"""y_pianyi_val 偏移应使扫描带也偏移;目标色画到偏移后位置才命中。"""
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
offset = 100
# ROI 中心 y0 = 354 + 100 = 454,所以 y_top = 451..461
pic = _make_image_with_target_band(
TARGET_RGB, x_start=200, x_end=250, y_target=453
)
detected, x = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=offset, optimal_x_scaled=220
)
assert detected is True
def test_invalid_roi_returns_false(self):
"""rx 极大导致 x1 >= x2 → ROI 无效 → False。"""
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
pic = _make_solid_color_image(TARGET_RGB, w=10, h=10)
# rx=10000 让 157*10000//1080 越界
detected, _ = scan_with_numpy(
a, pic, rx=10000, ry=2400, y_pianyi_val=0, optimal_x_scaled=200
)
# x1 = max(0, 157*10000//1080) = 1453,x2 = min(10, 410*10000//1080) = 10
# x1 > x2 → 返回 False
assert detected is False
def test_optimal_x_search_prefers_optimal_location(self):
"""optimal_x_scaled 在命中区域内时,命中应该正好就在那里。"""
from douyin.detection.detection_pkg.finder_numpy_scan import scan_with_numpy
a = _mk_analyser()
# 在 x ∈ [200, 300] 都画上目标色
pic = _make_image_with_target_band(
TARGET_RGB, x_start=200, x_end=300, y_target=353
)
# optimal_x_scaled=250(恰在中间)
detected, x = scan_with_numpy(
a, pic, rx=1080, ry=2400, y_pianyi_val=0, optimal_x_scaled=250
)
assert detected is True
# 因为 optimal_col 一定命中,x 应该非常接近 250
assert 245 <= x <= 255