Files
douyi_guaji_main/tests/test_detection_pixel.py

579 lines
21 KiB
Python

"""douyin/detection/pixel 模块单元测试。
覆盖:
- pixel/popups: 弹窗检测函数签名验证
- pixel/buttons: 按钮检测函数签名验证
- pixel/stage: 阶段检测函数签名验证
"""
from __future__ import annotations
import inspect
import pytest
from types import SimpleNamespace
from PIL import Image
class TestPixelPopups:
"""pixel/popups 模块测试。"""
def test_detect_coupon_popup_exists(self):
from douyin.detection.pixel.popups import detect_coupon_popup_background
assert callable(detect_coupon_popup_background)
def test_detect_coupon_popup_signature(self):
from douyin.detection.pixel.popups import detect_coupon_popup_background
sig = inspect.signature(detect_coupon_popup_background)
params = list(sig.parameters.keys())
assert "pic_rgb" in params or len(params) >= 1
def test_detect_coupon_success_orange_panel(self):
from douyin.detection.pixel.popups import detect_coupon_popup_background
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
for y in range(560, 1540):
for x in range(180, 900):
img.putpixel((x, y), (250, 135, 40))
for y in range(1320, 1480):
for x in range(250, 830):
img.putpixel((x, y), (245, 75, 55))
detected, info = detect_coupon_popup_background(
img,
return_info=True,
template_threshold=0.99,
)
assert detected is True
assert info["method"] == "coupon_success_orange_panel"
assert info["orange_ratio"] >= 0.18
assert info["button_warm_ratio"] >= 0.20
def test_detect_coupon_bottom_panel_by_pixels(self):
from douyin.detection.pixel.popups import detect_coupon_popup_background
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
for y in range(1173, 2255):
for x in range(9, 1079):
img.putpixel((x, y), (248, 248, 248))
for y in range(1740, 2140):
for x in range(150, 930):
img.putpixel((x, y), (245, 80, 100))
detected, info = detect_coupon_popup_background(
img,
return_info=True,
template_threshold=0.99,
)
assert detected is True
assert info["method"] == "coupon_bottom_panel_pixels"
assert info["white_ratio"] >= 0.40
assert info["warm_ratio"] >= 0.08
def test_detect_redpacket_popup_exists(self):
from douyin.detection.pixel.popups import detect_redpacket_popup_background
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
assert callable(detect_lucky_draw_result_popup)
def test_detect_lucky_draw_result_white_confirm_button(self):
from douyin.detection.pixel.popups import detect_lucky_draw_result_popup
from douyin.fudai.core.coords import LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V2
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
x1, y1, x2, y2 = LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V2
for y in range(y1, y2):
for x in range(x1, x2):
img.putpixel((x, y), (245, 245, 245))
detected, info = detect_lucky_draw_result_popup(
img,
return_info=True,
analyser=SimpleNamespace(fudai_detail_ui_mode="v2_0"),
)
assert detected is True
assert info["white_ratio"] >= 0.8
assert info["ui_version"] == "v2_0"
assert info["pixel_style"] == "white"
def test_detect_lucky_draw_result_red_confirm_button_legacy(self):
from douyin.detection.pixel.popups import detect_lucky_draw_result_popup
from douyin.fudai.core.coords import LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V1
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
x1, y1, x2, y2 = LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V1
for y in range(y1, y2):
for x in range(x1, x2):
img.putpixel((x, y), (255, 60, 100))
detected, info = detect_lucky_draw_result_popup(
img,
return_info=True,
analyser=SimpleNamespace(fudai_detail_ui_mode="legacy"),
)
assert detected is True
assert info["red_ratio"] >= 0.8
assert info["ui_version"] == "legacy"
assert info["pixel_style"] == "red"
def test_detect_lucky_draw_result_legacy_does_not_accept_v2_white_button(self):
from douyin.detection.pixel.popups import detect_lucky_draw_result_popup
from douyin.fudai.core.coords import LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V1
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
x1, y1, x2, y2 = LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V1
for y in range(y1, y2):
for x in range(x1, x2):
img.putpixel((x, y), (245, 245, 245))
detected, info = detect_lucky_draw_result_popup(
img,
return_info=True,
analyser=SimpleNamespace(fudai_detail_ui_mode="legacy"),
)
assert detected is False
assert info["white_ratio"] >= 0.8
assert info["pixel_style"] == "red"
def test_lucky_draw_result_pixel_debug_disabled_by_default(self, monkeypatch):
from douyin.detection.pixel.popups import detect_lucky_draw_result_popup
from douyin.fudai.core.coords import LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V2
calls = []
monkeypatch.setattr(
"douyin.handlers.douyin_screenshot_manager.save_debug_image",
lambda *args, **kwargs: calls.append(kwargs) or "debug.png",
)
img = Image.new("RGB", (1080, 2400), (20, 20, 20))
x1, y1, x2, y2 = LOTTERY_RESULT_CONFIRM_BUTTON_PIXEL_V2
for y in range(y1, y2):
for x in range(x1, x2):
img.putpixel((x, y), (245, 245, 245))
detected, info = detect_lucky_draw_result_popup(
img,
return_info=True,
analyser=SimpleNamespace(fudai_detail_ui_mode="v2_0"),
)
assert detected is True
assert info["pixel_style"] == "white"
assert calls == []
class TestPixelButtons:
"""pixel/buttons 模块测试。"""
def test_detect_attend_button_exists(self):
from douyin.detection.pixel.buttons import detect_attend_button_red_area
assert callable(detect_attend_button_red_area)
def test_detect_attend_button_signature(self):
from douyin.detection.pixel.buttons import detect_attend_button_red_area
sig = inspect.signature(detect_attend_button_red_area)
params = list(sig.parameters.keys())
assert "pic_rgb" in params or len(params) >= 1
def test_detect_attend_button_accepts_rgba_image(self):
from PIL import Image
from douyin.detection.pixel.buttons import detect_attend_button_red_area
img = Image.new("RGBA", (1080, 2400), (0, 0, 0, 255))
for x in range(200, 880):
for y in range(2000, 2120):
img.putpixel((x, y), (255, 80, 120, 255))
detected, info = detect_attend_button_red_area(
img,
return_info=True,
red_ratio_threshold=0.25,
)
assert detected is True
assert info["red_ratio"] >= 0.25
def test_detect_fansclub_join_exists(self):
from douyin.detection.pixel.buttons import detect_fansclub_join_red_area
assert callable(detect_fansclub_join_red_area)
def test_detect_fansclub_join_accepts_rgba_image(self):
from PIL import Image
from douyin.detection.pixel.buttons import detect_fansclub_join_red_area
img = Image.new("RGBA", (1080, 2400), (0, 0, 0, 255))
for x in range(200, 880):
for y in range(1940, 2140):
img.putpixel((x, y), (255, 80, 120, 255))
detected, info = detect_fansclub_join_red_area(
img,
return_info=True,
red_ratio_threshold=0.25,
)
assert detected is True
assert info["red_ratio"] >= 0.25
class TestPixelStage:
"""pixel/stage 模块测试。"""
def test_detect_live_room_icons_exists(self):
from douyin.detection.pixel.stage import detect_live_room_icons_by_pixel
assert callable(detect_live_room_icons_by_pixel)
def test_detect_live_room_icons_signature(self):
from douyin.detection.pixel.stage import detect_live_room_icons_by_pixel
sig = inspect.signature(detect_live_room_icons_by_pixel)
params = list(sig.parameters.keys())
assert "pic_rgb" in params
def test_detect_fudai_detail_page_exists(self):
from douyin.detection.pixel.stage import detect_fudai_detail_page
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["bluepurple_ratio"] < 0.45
def test_read_pixel_stage_state_exists(self):
from douyin.detection.pixel.stage import read_pixel_stage_state_from_screenshot
assert callable(read_pixel_stage_state_from_screenshot)
def test_stage_module_imports_popup_helpers(self):
import douyin.detection.pixel.stage as stage_mod
assert hasattr(stage_mod, "detect_coupon_popup_background")
assert hasattr(stage_mod, "detect_redpacket_popup_background")
assert hasattr(stage_mod, "detect_lucky_draw_result_popup")
assert hasattr(stage_mod, "detect_attend_button_red_area")
assert hasattr(stage_mod, "detect_fansclub_join_red_area")
def test_read_pixel_stage_saves_single_overview_debug_image(self, tmp_path, monkeypatch):
from douyin.detection.pixel.stage import read_pixel_stage_state_from_screenshot
img_path = tmp_path / "screen.png"
Image.new("RGB", (1080, 2400), (20, 20, 20)).save(img_path)
debug_dir = tmp_path / "debug"
debug_dir.mkdir()
(debug_dir / "20260603_像素检测_抽奖结果_检测前.png").write_bytes(b"old")
(debug_dir / "20260603_px_attend_pre.png").write_bytes(b"old")
calls = []
saved_images = []
def fake_save_debug_image(image, **kwargs):
calls.append(kwargs)
saved_images.append(image.copy())
return str(debug_dir / "20260603_px_pxstage_ov_ok_0.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,
)
read_pixel_stage_state_from_screenshot(
str(img_path),
return_details=True,
analyser=SimpleNamespace(
fudai_detail_ui_mode="v2_0",
pixel_detect_coupon_popup_enabled=False,
pixel_detect_shopping_redpacket_enabled=False,
pixel_detect_fudai_detail_enabled=False,
),
)
assert len(calls) == 1
assert calls[0]["operation_type"] == "pixel_detect"
assert calls[0]["picname"] == "pixel_stage"
assert calls[0]["step"] == "overview"
assert calls[0]["annotate"] is False
assert saved_images
assert (debug_dir / "20260603_像素检测_抽奖结果_检测前.png").exists()
assert (debug_dir / "20260603_px_attend_pre.png").exists()
def test_pixel_stage_overview_draws_candidate_boxes(self, tmp_path, monkeypatch):
from douyin.detection.pixel.stage import _save_pixel_stage_overview
from douyin.fudai.core import coords as coords_mod
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 / "20260603_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,
)
monkeypatch.setattr(coords_mod, "_read_runtime_region_config", lambda: {})
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": True,
"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": "detail_body_color", "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]
# Task-status candidate box is visible when the detail page is seen.
assert overview.getpixel((9, 1173)) != (255, 70, 70)
assert overview.getpixel((200, 300)) != (255, 70, 70)
assert overview.getpixel((820, 1815)) == (0, 220, 80)
# 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": "detail_body_color", "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((820, 1815)) == (255, 70, 70)
class TestPixelCommon:
"""pixel/_common 模块测试。"""
def test_safe_open_screenshot_rgb_exists(self):
from douyin.detection.pixel._common import _safe_open_screenshot_rgb
assert callable(_safe_open_screenshot_rgb)
class TestPixelShimConsistency:
"""验证 pixel shim 与子模块导出一致。"""
def test_douyin_pixel_exports_match_submodules(self):
from douyin.detection.douyin_pixel import (
detect_coupon_popup_background as f1,
detect_redpacket_popup_background as f2,
detect_lucky_draw_result_popup as f3,
detect_attend_button_red_area as f4,
detect_fansclub_join_red_area as f5,
detect_live_room_icons_by_pixel as f6,
detect_fudai_detail_page as f7,
read_pixel_stage_state_from_screenshot as f8,
)
from douyin.detection.pixel import (
detect_coupon_popup_background as g1,
detect_redpacket_popup_background as g2,
detect_lucky_draw_result_popup as g3,
detect_attend_button_red_area as g4,
detect_fansclub_join_red_area as g5,
detect_live_room_icons_by_pixel as g6,
detect_fudai_detail_page as g7,
read_pixel_stage_state_from_screenshot as g8,
)
assert f1 is g1
assert f2 is g2
assert f3 is g3
assert f4 is g4
assert f5 is g5
assert f6 is g6
assert f7 is g7
assert f8 is g8
class TestDetectionShimConsistency:
"""验证 detection shim 与子模块导出一致。"""
def test_douyin_detection_exports_match_submodules(self):
from douyin.detection.douyin_detection import (
_ensure_detection_cache as f1,
check_detail_height as f2,
check_have_fudai_with_ready_screenshot as f3,
check_in_fudai_detail as f4,
)
from douyin.detection.detection_pkg import (
_ensure_detection_cache as g1,
check_detail_height as g2,
check_have_fudai_with_ready_screenshot as g3,
check_in_fudai_detail as g4,
)
assert f1 is g1
assert f2 is g2
assert f3 is g3
assert f4 is g4