fix false fudai detail pixel detection

This commit is contained in:
Your Name committed 2026-06-04 11:51:12 +08:00
1 parent 1d564000ca
commit f07fbb305b
3 files changed
+144 -12

No files matched your search

+10 -7
View File
@@ -15,7 +15,7 @@ from typing import Any
from ._common import _ensure_detection_cache
def check_detail_height(analyser: Any) -> int:
def check_detail_height(analyser: Any, pic_rgb: Any = None) -> int:
"""Detect how many tasks are required in the fudai detail popup.
Returns:
@@ -33,7 +33,7 @@ def check_detail_height(analyser: Any) -> int:
if ui_mode == "legacy":
return _check_detail_task_count_legacy_pixels(analyser)
detected_v2_template = _check_detail_task_count_v2(analyser)
detected_v2_template = _check_detail_task_count_v2(analyser, pic_rgb=pic_rgb)
if detected_v2_template:
return detected_v2_template
@@ -432,7 +432,7 @@ def _check_detail_task_count_v2_pixels(analyser: Any) -> int:
return detected
def _check_detail_task_count_v2(analyser: Any) -> int:
def _check_detail_task_count_v2(analyser: Any, pic_rgb: Any = None) -> int:
"""Fast v2 detail task-count recognition by matching the task-count digit only."""
try:
import cv2 # type: ignore[import-untyped]
@@ -441,10 +441,13 @@ def _check_detail_task_count_v2(analyser: Any) -> int:
from douyin.core import path_config
from douyin.fudai.core.coords import DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK
pic_path = path_config.get_screenshot_path()
if not os.path.exists(pic_path):
return 0
pic = Image.open(pic_path).convert("RGB")
if pic_rgb is not None:
pic = pic_rgb.convert("RGB") if hasattr(pic_rgb, "convert") else Image.fromarray(pic_rgb).convert("RGB")
else:
pic_path = path_config.get_screenshot_path()
if not os.path.exists(pic_path):
return 0
pic = Image.open(pic_path).convert("RGB")
pic_rgb = np.asarray(pic)
pic_gray = cv2.cvtColor(pic_rgb, cv2.COLOR_RGB2GRAY)
+51 -5
View File
@@ -110,6 +110,18 @@ def _save_pixel_stage_overview(
get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0.get("task_count") or (860, 1770, 1035, 1875))),
)
else:
if "has_fudai_detail" in details:
from douyin.detection.detection_pkg.detail import get_detail_task_count_digit_box
from douyin.fudai.core.coords import DETAIL_UI_V2_0, DETAIL_UI_V2_0_ONE_TASK
regions["task_count_one"] = (
"has_fudai_detail",
get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0_ONE_TASK.get("task_count") or (890, 1855, 1040, 1960))),
)
regions["task_count_two"] = (
"has_fudai_detail",
get_detail_task_count_digit_box(tuple(DETAIL_UI_V2_0.get("task_count") or (860, 1770, 1035, 1875))),
)
if details.get("has_coupon_bg", {}).get("method") != "disabled":
regions["coupon"] = ("has_coupon_bg", get_pixel_region("coupon_popup", (9, 1173, 1079, 2255)))
if details.get("has_redpacket_bg", {}).get("method") != "disabled":
@@ -361,6 +373,38 @@ def detect_fudai_detail_page(
- method: 检测方法("task_count")
- task_count: 任务数量(0/1/2/3)
"""
def _has_detail_panel_chrome(pic_rgb: Image.Image) -> tuple[bool, dict]:
"""Confirm the v2 detail popup's dark blue-purple panel is present."""
try:
import numpy as np # type: ignore[import-untyped]
arr = np.asarray(pic_rgb.convert("RGB")).astype("int16")
height, width = arr.shape[:2]
rx = max(1, int(_resolution_ratio_x or width or 1080))
ry = max(1, int(_resolution_ratio_y or height or 2400))
x1 = max(0, 30 * rx // 1080)
x2 = min(width, 1050 * rx // 1080)
y1 = max(0, 1200 * ry // 2400)
y2 = min(height, 1700 * ry // 2400)
if x1 >= x2 or y1 >= y2:
return False, {"panel_reason": "invalid_panel_box"}
roi = arr[y1:y2, x1:x2]
r, g, b = roi[:, :, 0], roi[:, :, 1], roi[:, :, 2]
bluepurple_ratio = float(
(
(r < 95)
& (g < 85)
& (b > 45)
& (b > r * 0.8)
).mean()
)
return bluepurple_ratio >= 0.45, {
"panel_box": (x1, y1, x2, y2),
"panel_bluepurple_ratio": round(bluepurple_ratio, 4),
}
except Exception as exc:
return False, {"panel_reason": f"panel_exception: {exc}"}
# ---------- 任务数量检测 ----------
if analyser is None:
# analyser 参数必需
@@ -376,17 +420,17 @@ def detect_fudai_detail_page(
from douyin.detection.douyin_detection import check_detail_height
# 执行任务数量检测
task_count = int(check_detail_height(analyser) or 0)
task_count = int(check_detail_height(analyser, pic_rgb=_pic_rgb) or 0)
# 判断是否在福袋详情页
# 如果任务数量 <= 0,则认为不在福袋详情页
# 如果任务数量 >= 1,则认为在福袋详情页
is_in_detail = task_count >= 1
panel_ok, panel_info = _has_detail_panel_chrome(_pic_rgb)
is_in_detail = task_count >= 1 and panel_ok
if return_info:
return is_in_detail, {
"method": "task_count",
"task_count": task_count,
**panel_info,
"reason": None if is_in_detail else "task_count_or_panel_not_matched",
}
return is_in_detail
except Exception as e: # type: ignore[general-exception]
@@ -602,6 +646,8 @@ def read_pixel_stage_state_from_screenshot(
"confidence"
), # 添加confidence支持(用于像素检测)
"task_count": info.get("task_count"), # 添加task_count支持
"panel_bluepurple_ratio": info.get("panel_bluepurple_ratio"),
"panel_box": info.get("panel_box"),
"red_ratio": info.get("red_ratio"), # 添加red_ratio支持
"white_ratio": info.get("white_ratio"),
"effective_threshold": info.get("effective_threshold"),
+83
View File
@@ -170,6 +170,36 @@ class TestPixelStage:
assert callable(detect_fudai_detail_page)
def test_detect_fudai_detail_page_uses_current_frame(self, tmp_path, monkeypatch):
from PIL import ImageDraw
from douyin.detection.pixel.stage import detect_fudai_detail_page
from douyin.core import path_config
stale_screenshot = tmp_path / "stale_detail_like.png"
stale = Image.new("RGB", (1080, 2400), (10, 15, 70))
draw = ImageDraw.Draw(stale)
draw.text((976, 1871), "1", fill=(255, 90, 140))
stale.save(stale_screenshot)
monkeypatch.setattr(path_config, "get_screenshot_path", lambda: str(stale_screenshot))
current_live_room = Image.new("RGB", (1080, 2400), (240, 240, 240))
analyser = SimpleNamespace(
fudai_detail_ui_mode="v2_0",
resolution_ratio_x=1080,
resolution_ratio_y=2400,
_log_kv=lambda *_args, **_kwargs: None,
)
detected, info = detect_fudai_detail_page(
current_live_room,
return_info=True,
analyser=analyser,
)
assert detected is False
assert info["task_count"] == 0
assert info["panel_bluepurple_ratio"] < 0.45
def test_read_pixel_stage_state_exists(self):
from douyin.detection.pixel.stage import read_pixel_stage_state_from_screenshot
@@ -296,6 +326,59 @@ class TestPixelStage:
# Successful live-room icon detection is still annotated.
assert overview.getpixel((1002, 172)) == (0, 220, 80)
def test_pixel_stage_overview_draws_task_count_candidates_when_not_detail(self, tmp_path, monkeypatch):
from douyin.detection.pixel.stage import _save_pixel_stage_overview
debug_dir = tmp_path / "debug"
debug_dir.mkdir()
saved_images = []
def fake_save_debug_image(image, **kwargs):
saved_images.append(image.copy())
return str(debug_dir / "20260604_px_pxstage_ov_ok_1.png")
monkeypatch.setattr(
"douyin.handlers.screenshot_manager.save_image_helpers.is_debug_captures_enabled",
lambda: True,
)
monkeypatch.setattr(
"douyin.handlers.screenshot_manager.paths.get_debug_captures_dir",
lambda: str(debug_dir),
)
monkeypatch.setattr(
"douyin.handlers.douyin_screenshot_manager.save_debug_image",
fake_save_debug_image,
)
image = Image.new("RGB", (1080, 2400), (20, 20, 20))
_save_pixel_stage_overview(
image,
{
"has_coupon_bg": False,
"has_redpacket_bg": False,
"has_lucky_draw_result": False,
"has_attend_button_red": False,
"has_fansclub_join_red": False,
"has_fudai_detail": False,
"has_live_room_pixel": True,
},
{
"has_coupon_bg": {"method": "template_match", "template_score": 0.1},
"has_redpacket_bg": {"method": "disabled"},
"has_lucky_draw_result": {"method": "pixel_detect", "red_ratio": 0.0},
"has_attend_button_red": {"method": "pixel_detect", "red_ratio": 0.0},
"has_fansclub_join_red": {"method": "pixel_detect", "red_ratio": 0.0},
"has_fudai_detail": {"method": "task_count", "task_count": 0},
"has_live_room_pixel": {"method": "pixel_detection", "confidence": 0.95},
},
analyser=SimpleNamespace(fudai_detail_ui_mode="v2_0"),
)
assert saved_images
overview = saved_images[0]
assert overview.getpixel((977, 1874)) == (255, 70, 70)
assert overview.getpixel((975, 1817)) == (255, 70, 70)
class TestPixelCommon:
"""pixel/_common 模块测试。"""