Generalize coupon overlay rejection for fudai detection

This commit is contained in:
Your Name committed 2026-06-18 12:18:25 +08:00
1 parent b9c5106f62
commit 89fb278988
11 files changed
+310 -36

No files matched your search

+1 -1
View File
@@ -71,7 +71,7 @@
"pixel_detect_live_room_enabled": true,
"ocr_lottery_result_enabled": true,
"ocr_coupon_popup_enabled": true,
"ocr_shopping_redpacket_enabled": true,
"ocr_shopping_redpacket_enabled": false,
"ocr_stage_page_enabled": true,
"fudai_disable_content_recognition": false,
"fudai_disable_task_recognition": false,
+1 -1
View File
@@ -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 = True
self.ocr_shopping_redpacket_enabled = False
self.ocr_stage_page_enabled = True
# 高价值福袋 / 预约相关配置
@@ -628,6 +628,96 @@ class FudaiDetector:
pass
return bool(passed)
@staticmethod
def _largest_component_box(mask: Any) -> Tuple[int, Optional[Tuple[int, int, int, int]]]:
try:
import cv2 # type: ignore[import-untyped]
component_count, _labels, stats, _centroids = cv2.connectedComponentsWithStats(
mask.astype("uint8"),
8,
)
best_area = 0
best_box: Optional[Tuple[int, int, int, int]] = None
for idx in range(1, component_count):
area = int(stats[idx, cv2.CC_STAT_AREA])
if area <= best_area:
continue
best_area = area
best_box = (
int(stats[idx, cv2.CC_STAT_LEFT]),
int(stats[idx, cv2.CC_STAT_TOP]),
int(stats[idx, cv2.CC_STAT_WIDTH]),
int(stats[idx, cv2.CC_STAT_HEIGHT]),
)
return best_area, best_box
except Exception:
area = int(mask.sum())
return area, None
def _is_commercial_coupon_overlay(self, arr: Any) -> Tuple[bool, Dict[str, Any]]:
"""Detect generic coupon/red-packet promo widgets near the fudai hot zone.
This is intentionally structural rather than sample-specific: coupon and
red-packet widgets usually have a large warm ticket/card blob on the
left of the candidate plus a dark/transparent surrounding overlay. Real
fudai badges can share red/pink/purple pixels, so color alone is not
enough to reject them.
"""
try:
import numpy as np # type: ignore[import-untyped]
height, width = arr.shape[:2]
r = arr[:, :, 0]
g = arr[:, :, 1]
b = arr[:, :, 2]
left = arr[:, : max(1, width // 2), :]
lr = left[:, :, 0]
lg = left[:, :, 1]
lb = left[:, :, 2]
warm_left = (
(lr >= 205)
& (lg >= 70)
& (lg <= 210)
& (lb <= 135)
)
dark_bg = (r <= 90) & (g <= 90) & (b <= 105)
red_body = (r >= 190) & (g <= 115) & (b <= 135)
warm_area, warm_box = self._largest_component_box(warm_left)
red_area, _red_box = self._largest_component_box(red_body)
dark_ratio = float(np.mean(dark_bg)) if dark_bg.size else 0.0
warm_ratio_left = float(np.mean(warm_left)) if warm_left.size else 0.0
warm_box_large = False
warm_box_left = False
if warm_box is not None:
wx, _wy, ww, wh = warm_box
warm_box_large = warm_area >= 900 and ww >= 28 and wh >= 45
warm_box_left = wx + ww / 2 <= max(1, width // 2) * 0.72
# A real fudai badge often has a dominant solid red body and almost
# no dark surrounding overlay; coupon widgets are the opposite.
dominant_red_badge = red_area >= 5000 and dark_ratio < 0.08
commercial_like = (
warm_box_large
and warm_box_left
and dark_ratio >= 0.10
and warm_ratio_left >= 0.06
and not dominant_red_badge
)
return bool(commercial_like), {
"warm_left_ratio": round(warm_ratio_left, 4),
"warm_area": warm_area,
"warm_box": warm_box,
"dark_ratio": round(dark_ratio, 4),
"red_area": red_area,
"dominant_red_badge": dominant_red_badge,
}
except Exception as exc:
return False, {"commercial_overlay_error": str(exc)}
def confirm_pixel_candidate_shape(self, pic: Image.Image, detected_x: int) -> bool:
"""Validate that the pixel hit has the compact shape of a fudai badge."""
try:
@@ -645,10 +735,6 @@ 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)
@@ -657,22 +743,13 @@ 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
commercial_overlay, commercial_info = self._is_commercial_coupon_overlay(arr)
passed = (
not coupon_like
not commercial_overlay
and 20 <= total <= 120
and 6 <= valid_cols <= 45
and 2 <= max_col <= 10
@@ -686,9 +763,8 @@ 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,
commercial_overlay=commercial_overlay,
commercial_info=commercial_info,
window=(x1, y1, x2, y2),
)
except Exception:
+30 -9
View File
@@ -211,6 +211,7 @@ def check_shopping_redpacket_popup(analyser) -> bool:
# 步驟0:快速像素檢測中央紅色彈窗背景
pixel_check_start = time.time()
has_popup_pixel = False
pixel_info = {}
if not bool(getattr(analyser, "pixel_detect_shopping_redpacket_enabled", False)):
try:
analyser._log_kv("購物紅包檢測", "購物紅包像素檢測已關閉")
@@ -242,14 +243,14 @@ def check_shopping_redpacket_popup(analyser) -> bool:
)
except Exception:
redpacket_threshold = 0.15
has_popup_pixel = bool(
douyin_pixel.detect_redpacket_popup_background(
pic_rgb,
red_ratio_threshold=redpacket_threshold,
analyser=analyser,
save_debug=False,
)
has_popup_pixel, pixel_info = douyin_pixel.detect_redpacket_popup_background(
pic_rgb,
red_ratio_threshold=redpacket_threshold,
analyser=analyser,
save_debug=False,
return_info=True,
)
has_popup_pixel = bool(has_popup_pixel)
pixel_elapsed = time.time() - pixel_check_start
try:
@@ -257,6 +258,8 @@ def check_shopping_redpacket_popup(analyser) -> bool:
"購物紅包檢測",
f"像素檢測{'成功' if has_popup_pixel else '未檢測到'}",
像素結果=has_popup_pixel,
紅色占比=round(float(pixel_info.get("red_ratio", 0.0)), 4),
閾值=round(float(pixel_info.get("effective_threshold", redpacket_threshold)), 4),
耗時秒=f"{pixel_elapsed:.3f}",
)
except Exception:
@@ -274,10 +277,28 @@ def check_shopping_redpacket_popup(analyser) -> bool:
if not bool(getattr(analyser, "ocr_shopping_redpacket_enabled", False)):
try:
analyser._log_kv("購物紅包檢測", "購物紅包OCR已關閉,跳過OCR確認")
from douyin.stage.douyin_stage_handler_pkg.popup_verification import (
is_redpacket_pixel_high_confidence,
)
high_conf, red_ratio, threshold = is_redpacket_pixel_high_confidence(
analyser=analyser,
pixel_details={"has_redpacket_bg": pixel_info},
)
except Exception:
high_conf, red_ratio, threshold = False, 0.0, 0.30
try:
analyser._log_kv(
"購物紅包檢測",
"購物紅包OCR已關閉,按像素高置信判定",
像素結果=has_popup_pixel,
紅色占比=round(red_ratio, 4),
高置信阈值=round(threshold, 4),
高置信=high_conf,
)
except Exception:
pass
return False
return bool(has_popup_pixel and high_conf)
# 步驟1:截圖檢測區域
cut_start = time.time()
+32 -3
View File
@@ -222,8 +222,37 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
)
redpacket_detected = False
redpacket_reason = "像素检测+OCR确认(购物红包弹窗)"
if not bool(getattr(analyser, "ocr_shopping_redpacket_enabled", False)):
from douyin.stage.douyin_stage_handler_pkg.popup_verification import (
is_redpacket_pixel_high_confidence,
)
high_conf, red_ratio, threshold = is_redpacket_pixel_high_confidence(
analyser=analyser,
pixel_details=pixel_details,
)
_log(
"步骤1.5[检测购物红包弹窗]-OCR关闭,像素高置信判断",
红色占比=round(red_ratio, 4),
高置信阈值=round(threshold, 4),
高置信=high_conf,
)
if high_conf:
redpacket_detected = True
redpacket_reason = "像素高置信确认(购物红包弹窗)"
else:
_log(
"步骤1.5[检测购物红包弹窗]-OCR关闭且像素未达高置信",
说明="不进入OCR,继续后续阶段检测",
)
try:
if time.time() - step_redpacket_start < max_step_time:
if (
not redpacket_detected
and bool(getattr(analyser, "ocr_shopping_redpacket_enabled", False))
and time.time() - step_redpacket_start < max_step_time
):
cut_start = time.time()
analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包")
cut_elapsed = (time.time() - cut_start) * 1000
@@ -275,11 +304,11 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
_log_summary(
"阶段诊断完成(购物红包弹窗)",
阶段="SHOPPING_REDPACKET_POPUP",
原因="检测到购物红包弹窗",
原因=redpacket_reason,
总耗时毫秒=total_elapsed,
红包检测耗时毫秒=redpacket_elapsed,
)
return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(购物红包弹窗)"
return "SHOPPING_REDPACKET_POPUP", redpacket_reason
# 步骤1.5结束计时
step15_elapsed = (time.time() - step_redpacket_start) * 1000
@@ -28,6 +28,7 @@ def finalize_stage_after_pixel(
popup_result = verify_popup_via_ocr(
analyser=analyser,
pixel_state=pixel_state,
pixel_details=pixel_details,
diag_start=diag_start,
max_diag_time=max_diag_time,
max_step_time=max_step_time,
@@ -62,4 +63,3 @@ def finalize_stage_after_pixel(
)
log_separator("最终结果:IN_LIVE_ROOM(默认直播间)")
return "IN_LIVE_ROOM", "像素检测未命中,默认直播间"
@@ -23,9 +23,35 @@ from douyin.fudai.core.coords import (
)
def is_redpacket_pixel_high_confidence(
*,
analyser: Any,
pixel_details: dict,
) -> Tuple[bool, float, float]:
"""Return whether the redpacket pixel signal is strong enough without OCR."""
details = pixel_details.get("has_redpacket_bg", {})
if not isinstance(details, dict):
details = {}
try:
red_ratio = float(details.get("red_ratio") or 0.0)
except Exception:
red_ratio = 0.0
try:
base_threshold = float(
details.get("effective_threshold")
or getattr(analyser, "shopping_redpacket_red_ratio_threshold", 0.15)
or 0.15
)
except Exception:
base_threshold = 0.15
high_conf_threshold = max(base_threshold * 2.0, 0.30)
return red_ratio >= high_conf_threshold, red_ratio, high_conf_threshold
def verify_popup_via_ocr(
analyser: Any,
pixel_state: dict,
pixel_details: dict,
diag_start: float,
max_diag_time: float,
max_step_time: float,
@@ -55,6 +81,33 @@ def verify_popup_via_ocr(
if not has_suspicious_popup_pixel:
return None
if pixel_state.get("has_redpacket_bg", False) and not bool(
getattr(analyser, "ocr_shopping_redpacket_enabled", False)
):
high_conf, red_ratio, threshold = is_redpacket_pixel_high_confidence(
analyser=analyser,
pixel_details=pixel_details,
)
log(
"步骤3[像素确认购物红包]-OCR关闭",
红色占比=round(red_ratio, 4),
高置信阈值=round(threshold, 4),
高置信=high_conf,
)
if high_conf:
total_elapsed = (time.time() - diag_start) * 1000
log_summary(
"阶段诊断完成(购物红包弹窗-像素高置信)",
阶段="SHOPPING_REDPACKET_POPUP",
原因="购物红包OCR关闭,像素高置信确认",
红色占比=round(red_ratio, 4),
高置信阈值=round(threshold, 4),
总耗时毫秒=total_elapsed,
)
return "SHOPPING_REDPACKET_POPUP", "像素高置信确认(购物红包弹窗)"
if not pixel_state.get("has_coupon_bg", False):
return None
try:
from douyin.utils.ocr_preprocess import preprocess_for_ocr # type: ignore
except Exception:
+1 -1
View File
@@ -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": True,
"ocr_shopping_redpacket_enabled": False,
"ocr_stage_page_enabled": True,
}.items():
setattr(analyser, key, bool(config.get(key, default)))
+2 -2
View File
@@ -199,7 +199,7 @@ class TestFudaiDetectionDefaults:
apply_fudai_defaults(analyser)
assert analyser.pixel_detect_shopping_redpacket_enabled is True
assert analyser.ocr_shopping_redpacket_enabled is True
assert analyser.ocr_shopping_redpacket_enabled is False
def test_config_loader_defaults_shopping_redpacket_detection_enabled(self):
from douyin.utils.douyin_config import ConfigManager
@@ -214,7 +214,7 @@ class TestFudaiDetectionDefaults:
os.unlink(tmp_path)
assert analyser.pixel_detect_shopping_redpacket_enabled is True
assert analyser.ocr_shopping_redpacket_enabled is True
assert analyser.ocr_shopping_redpacket_enabled is False
class TestCoreModules:
+52
View File
@@ -79,6 +79,58 @@ class TestPixelPopups:
assert callable(detect_redpacket_popup_background)
def test_redpacket_pixel_only_real_popup_is_high_confidence(self):
from douyin.detection.pixel.popups import detect_redpacket_popup_background
from douyin.stage.douyin_stage_handler_pkg.popup_verification import (
is_redpacket_pixel_high_confidence,
)
sample = next(
iter(
__import__("pathlib").Path(
"pic/debug_captures/2026-06-18"
).glob("*購物紅包*.png")
),
None,
)
if sample is None:
pytest.skip("shopping redpacket debug capture unavailable")
detected, info = detect_redpacket_popup_background(
Image.open(sample).convert("RGB"),
return_info=True,
red_ratio_threshold=0.15,
analyser=SimpleNamespace(shopping_redpacket_red_ratio_threshold=0.15),
)
high_conf, red_ratio, threshold = is_redpacket_pixel_high_confidence(
analyser=SimpleNamespace(shopping_redpacket_red_ratio_threshold=0.15),
pixel_details={"has_redpacket_bg": info},
)
assert detected is True
assert red_ratio >= 0.30
assert red_ratio >= threshold
assert high_conf is True
def test_redpacket_pixel_only_left_coupon_is_not_popup(self):
from pathlib import Path
from douyin.detection.pixel.popups import detect_redpacket_popup_background
sample = Path("tests/fixtures/fudai_icon_regression/false_left_coupon_x162.png")
if not sample.exists():
pytest.skip("coupon fixture unavailable")
detected, info = detect_redpacket_popup_background(
Image.open(sample).convert("RGB"),
return_info=True,
red_ratio_threshold=0.15,
analyser=SimpleNamespace(shopping_redpacket_red_ratio_threshold=0.15),
)
assert detected is False
assert info["red_ratio"] < 0.15
def test_detect_lucky_draw_result_exists(self):
from douyin.detection.pixel.popups import detect_lucky_draw_result_popup
+43
View File
@@ -8,6 +8,7 @@ check_have_fudai_with_ready_screenshot_optimized。
"""
from __future__ import annotations
import time
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -134,6 +135,48 @@ class TestFudaiDetector:
assert detected_x is None
assert method == ""
def test_commercial_coupon_overlay_detector_is_structural(self):
import numpy as np
coupon_sample = FIXTURE_DIR / "false_left_coupon_x162.png"
fudai_sample = FIXTURE_DIR / "real_left_x83.png"
if not coupon_sample.exists() or not fudai_sample.exists():
pytest.skip("fixture unavailable")
d = FudaiDetector(FakeAnalyser())
coupon_img = Image.open(coupon_sample).convert("RGB")
coupon_arr = np.asarray(coupon_img.crop((62, 300, 262, 560)))
coupon_like, coupon_info = d._is_commercial_coupon_overlay(coupon_arr)
fudai_img = Image.open(fudai_sample).convert("RGB")
fudai_arr = np.asarray(fudai_img.crop((0, 300, 183, 560)))
fudai_like, fudai_info = d._is_commercial_coupon_overlay(fudai_arr)
assert coupon_like is True
assert coupon_info["warm_area"] >= 900
assert coupon_info["dark_ratio"] >= 0.10
assert fudai_like is False
assert fudai_info["dominant_red_badge"] is True
def test_commercial_coupon_overlay_detector_is_lightweight(self):
import numpy as np
sample = FIXTURE_DIR / "false_left_coupon_x162.png"
if not sample.exists():
pytest.skip("fixture unavailable")
d = FudaiDetector(FakeAnalyser())
img = Image.open(sample).convert("RGB")
arr = np.asarray(img.crop((62, 300, 262, 560)))
start = time.perf_counter()
for _ in range(50):
d._is_commercial_coupon_overlay(arr)
elapsed_ms = (time.perf_counter() - start) * 1000 / 50
assert elapsed_ms < 5.0
def test_current_icon_template_confirms_clean_countdown_sample(self):
sample = Path(
"artifacts/live_fudai_flow_20260618_0530_valid/snapshots/"