Harden fudai icon detection against coupon taps
No files matched your search
@@ -63,7 +63,7 @@
|
||||
"fudai_detail_ocr_live_title_enabled": true,
|
||||
"pixel_detect_fudai_icon_enabled": true,
|
||||
"pixel_detect_coupon_popup_enabled": true,
|
||||
"pixel_detect_shopping_redpacket_enabled": false,
|
||||
"pixel_detect_shopping_redpacket_enabled": true,
|
||||
"pixel_detect_lottery_result_enabled": true,
|
||||
"pixel_detect_attend_button_enabled": true,
|
||||
"pixel_detect_fansclub_join_enabled": true,
|
||||
@@ -71,7 +71,7 @@
|
||||
"pixel_detect_live_room_enabled": true,
|
||||
"ocr_lottery_result_enabled": true,
|
||||
"ocr_coupon_popup_enabled": true,
|
||||
"ocr_shopping_redpacket_enabled": false,
|
||||
"ocr_shopping_redpacket_enabled": true,
|
||||
"ocr_stage_page_enabled": true,
|
||||
"fudai_disable_content_recognition": false,
|
||||
"fudai_disable_task_recognition": false,
|
||||
|
||||
@@ -84,7 +84,7 @@ def apply_fudai_defaults(self: Any) -> None:
|
||||
self.fudai_detail_ocr_live_title_enabled = False
|
||||
self.pixel_detect_fudai_icon_enabled = True
|
||||
self.pixel_detect_coupon_popup_enabled = True
|
||||
self.pixel_detect_shopping_redpacket_enabled = False
|
||||
self.pixel_detect_shopping_redpacket_enabled = True
|
||||
self.pixel_detect_lottery_result_enabled = True
|
||||
self.pixel_detect_attend_button_enabled = True
|
||||
self.pixel_detect_fansclub_join_enabled = True
|
||||
@@ -92,7 +92,7 @@ def apply_fudai_defaults(self: Any) -> None:
|
||||
self.pixel_detect_live_room_enabled = True
|
||||
self.ocr_lottery_result_enabled = True
|
||||
self.ocr_coupon_popup_enabled = True
|
||||
self.ocr_shopping_redpacket_enabled = False
|
||||
self.ocr_shopping_redpacket_enabled = True
|
||||
self.ocr_stage_page_enabled = True
|
||||
|
||||
# 高价值福袋 / 预约相关配置
|
||||
|
||||
@@ -492,6 +492,13 @@ class FudaiDetector:
|
||||
)
|
||||
except Exception:
|
||||
threshold = 0.80
|
||||
try:
|
||||
max_center_dx = int(
|
||||
getattr(self.analyser, "fudai_current_icon_template_max_center_dx", 60)
|
||||
or 60
|
||||
)
|
||||
except Exception:
|
||||
max_center_dx = 60
|
||||
|
||||
try:
|
||||
import cv2 # type: ignore[import-untyped]
|
||||
@@ -598,7 +605,10 @@ class FudaiDetector:
|
||||
continue
|
||||
|
||||
elapsed = (time.time() - start_time) * 1000
|
||||
passed = best_score >= threshold
|
||||
center_aligned = (
|
||||
best_center_dx is not None and abs(int(best_center_dx)) <= max_center_dx
|
||||
)
|
||||
passed = best_score >= threshold and center_aligned
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 图样确认",
|
||||
@@ -609,6 +619,8 @@ class FudaiDetector:
|
||||
threshold=threshold,
|
||||
template=best_name,
|
||||
center_dx=best_center_dx,
|
||||
max_center_dx=max_center_dx,
|
||||
center_aligned=center_aligned,
|
||||
match_loc=best_loc,
|
||||
elapsed_ms=round(elapsed, 2),
|
||||
)
|
||||
@@ -633,6 +645,10 @@ class FudaiDetector:
|
||||
r = arr[:, :, 0]
|
||||
g = arr[:, :, 1]
|
||||
b = arr[:, :, 2]
|
||||
left = arr[:, : max(1, arr.shape[1] // 2), :]
|
||||
lr = left[:, :, 0]
|
||||
lg = left[:, :, 1]
|
||||
lb = left[:, :, 2]
|
||||
mask = (
|
||||
(r >= 190)
|
||||
& (r <= 206)
|
||||
@@ -641,12 +657,23 @@ class FudaiDetector:
|
||||
& (b >= 234)
|
||||
& (b <= 250)
|
||||
)
|
||||
orange_left = (
|
||||
(lr >= 210)
|
||||
& (lg >= 90)
|
||||
& (lg <= 190)
|
||||
& (lb <= 100)
|
||||
)
|
||||
dark = (r <= 80) & (g <= 80) & (b <= 80)
|
||||
cols = np.sum(mask, axis=0)
|
||||
total = int(np.sum(mask))
|
||||
valid_cols = int(np.sum(cols >= 2))
|
||||
max_col = int(np.max(cols)) if cols.size else 0
|
||||
orange_left_ratio = float(np.mean(orange_left)) if orange_left.size else 0.0
|
||||
dark_ratio = float(np.mean(dark)) if dark.size else 0.0
|
||||
coupon_like = orange_left_ratio >= 0.10 and dark_ratio >= 0.10
|
||||
passed = (
|
||||
20 <= total <= 120
|
||||
not coupon_like
|
||||
and 20 <= total <= 120
|
||||
and 6 <= valid_cols <= 45
|
||||
and 2 <= max_col <= 10
|
||||
)
|
||||
@@ -659,6 +686,9 @@ class FudaiDetector:
|
||||
target_pixels=total,
|
||||
valid_cols=valid_cols,
|
||||
max_col=max_col,
|
||||
orange_left_ratio=round(orange_left_ratio, 4),
|
||||
dark_ratio=round(dark_ratio, 4),
|
||||
coupon_like=coupon_like,
|
||||
window=(x1, y1, x2, y2),
|
||||
)
|
||||
except Exception:
|
||||
|
||||
@@ -213,7 +213,7 @@ class ConfigManager:
|
||||
for key, default in {
|
||||
"pixel_detect_fudai_icon_enabled": True,
|
||||
"pixel_detect_coupon_popup_enabled": True,
|
||||
"pixel_detect_shopping_redpacket_enabled": False,
|
||||
"pixel_detect_shopping_redpacket_enabled": True,
|
||||
"pixel_detect_lottery_result_enabled": True,
|
||||
"pixel_detect_attend_button_enabled": True,
|
||||
"pixel_detect_fansclub_join_enabled": True,
|
||||
@@ -221,7 +221,7 @@ class ConfigManager:
|
||||
"pixel_detect_live_room_enabled": True,
|
||||
"ocr_lottery_result_enabled": True,
|
||||
"ocr_coupon_popup_enabled": True,
|
||||
"ocr_shopping_redpacket_enabled": False,
|
||||
"ocr_shopping_redpacket_enabled": True,
|
||||
"ocr_stage_page_enabled": True,
|
||||
}.items():
|
||||
setattr(analyser, key, bool(config.get(key, default)))
|
||||
|
||||
|
After Width: | Height: | Size: 2.2 MiB |
|
After Width: | Height: | Size: 1.5 MiB |
|
After Width: | Height: | Size: 2.4 MiB |
|
After Width: | Height: | Size: 1.9 MiB |
|
After Width: | Height: | Size: 2.2 MiB |
|
After Width: | Height: | Size: 2.4 MiB |
|
After Width: | Height: | Size: 2.3 MiB |
|
After Width: | Height: | Size: 2.1 MiB |
|
After Width: | Height: | Size: 2.2 MiB |
|
After Width: | Height: | Size: 2.2 MiB |
@@ -10,7 +10,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import json
|
||||
import tempfile
|
||||
from types import SimpleNamespace
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -189,6 +191,32 @@ class TestFlowControlShimConsistency:
|
||||
assert f1 is f2
|
||||
|
||||
|
||||
class TestFudaiDetectionDefaults:
|
||||
def test_shopping_redpacket_stage_detection_defaults_enabled(self):
|
||||
from douyin.core.fudai_modules.init_helpers import apply_fudai_defaults
|
||||
|
||||
analyser = SimpleNamespace()
|
||||
apply_fudai_defaults(analyser)
|
||||
|
||||
assert analyser.pixel_detect_shopping_redpacket_enabled is True
|
||||
assert analyser.ocr_shopping_redpacket_enabled is True
|
||||
|
||||
def test_config_loader_defaults_shopping_redpacket_detection_enabled(self):
|
||||
from douyin.utils.douyin_config import ConfigManager
|
||||
|
||||
analyser = SimpleNamespace()
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
|
||||
json.dump({}, f)
|
||||
tmp_path = f.name
|
||||
try:
|
||||
ConfigManager.load_config_from_file(analyser, tmp_path)
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
|
||||
assert analyser.pixel_detect_shopping_redpacket_enabled is True
|
||||
assert analyser.ocr_shopping_redpacket_enabled is True
|
||||
|
||||
|
||||
class TestCoreModules:
|
||||
"""core 子模块存在性测试。"""
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ check_have_fudai_with_ready_screenshot_optimized。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -27,6 +28,22 @@ from douyin.fudai.douyin_fudai_finder import (
|
||||
)
|
||||
|
||||
|
||||
FIXTURE_DIR = Path(__file__).parent / "fixtures" / "fudai_icon_regression"
|
||||
|
||||
ICON_REGRESSION_CASES = [
|
||||
("real_left_x83.png", True, 40, 120),
|
||||
("real_left_x209.png", True, 150, 230),
|
||||
("real_clean_countdown_x175.png", True, 140, 210),
|
||||
("real_light10_x40_120.png", True, 40, 120),
|
||||
("real_light10_x150_220.png", True, 150, 220),
|
||||
("false_banner_edge_x292.png", False, None, None),
|
||||
("false_banner_text_x269.png", False, None, None),
|
||||
("false_left_coupon_x162.png", False, None, None),
|
||||
("false_no_fudai_light10.png", False, None, None),
|
||||
("false_right_coupon_strip_x366.png", False, None, None),
|
||||
]
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Fake analyser 提供构造时需要的最小 attribute 集合
|
||||
# ============================================================
|
||||
@@ -96,9 +113,28 @@ class TestFudaiDetector:
|
||||
):
|
||||
assert callable(getattr(d, name, None)), f"{name} 不可调用"
|
||||
|
||||
def test_current_icon_template_confirms_clean_countdown_sample(self):
|
||||
from pathlib import Path
|
||||
@pytest.mark.parametrize(
|
||||
("sample_name", "should_detect", "min_x", "max_x"),
|
||||
ICON_REGRESSION_CASES,
|
||||
)
|
||||
def test_icon_regression_samples(self, sample_name, should_detect, min_x, max_x):
|
||||
sample = FIXTURE_DIR / sample_name
|
||||
if not sample.exists():
|
||||
pytest.skip(f"fixture unavailable: {sample_name}")
|
||||
|
||||
analyser = FakeAnalyser()
|
||||
d = FudaiDetector(analyser)
|
||||
detected_x, method = d.detect(Image.open(sample).convert("RGB"))
|
||||
|
||||
if should_detect:
|
||||
assert detected_x is not None
|
||||
assert min_x <= detected_x <= max_x
|
||||
assert method in {"pixel", "icon_fallback_template"}
|
||||
else:
|
||||
assert detected_x is None
|
||||
assert method == ""
|
||||
|
||||
def test_current_icon_template_confirms_clean_countdown_sample(self):
|
||||
sample = Path(
|
||||
"artifacts/live_fudai_flow_20260618_0530_valid/snapshots/"
|
||||
"screen_current_for_icon_template.png"
|
||||
@@ -132,6 +168,46 @@ class TestFudaiDetector:
|
||||
292,
|
||||
)
|
||||
|
||||
def test_current_icon_template_rejects_offset_banner_text_sample(self):
|
||||
from pathlib import Path
|
||||
|
||||
sample = Path(
|
||||
"pic/debug_captures/2026-06-18/"
|
||||
"20260618_112103_720256_px_fdicon_ok_x239y373w60h60_"
|
||||
"src_flow_on_detect_click_x_269_click_y_403_detected_x_scaled_269.png"
|
||||
)
|
||||
if not sample.exists():
|
||||
pytest.skip("live debug sample unavailable")
|
||||
|
||||
analyser = FakeAnalyser()
|
||||
analyser.fudai_current_icon_template_threshold = 0.80
|
||||
d = FudaiDetector(analyser)
|
||||
|
||||
assert not d.confirm_current_icon_template(
|
||||
Image.open(sample).convert("RGB"),
|
||||
269,
|
||||
)
|
||||
|
||||
def test_detect_rejects_offset_banner_text_false_candidate(self):
|
||||
from pathlib import Path
|
||||
|
||||
sample = Path(
|
||||
"pic/debug_captures/2026-06-18/"
|
||||
"20260618_112103_720256_px_fdicon_ok_x239y373w60h60_"
|
||||
"src_flow_on_detect_click_x_269_click_y_403_detected_x_scaled_269.png"
|
||||
)
|
||||
if not sample.exists():
|
||||
pytest.skip("live debug sample unavailable")
|
||||
|
||||
analyser = FakeAnalyser()
|
||||
analyser.last_fudai_x = 269
|
||||
d = FudaiDetector(analyser)
|
||||
|
||||
detected_x, method = d.detect(Image.open(sample).convert("RGB"))
|
||||
|
||||
assert detected_x is None
|
||||
assert method == ""
|
||||
|
||||
def test_pixel_candidate_shape_accepts_other_real_icon_style(self):
|
||||
from pathlib import Path
|
||||
|
||||
|
||||