Files
douyi_guaji_main/douyin/detection/pixel/buttons.py
T

359 lines
13 KiB
Python

from __future__ import annotations
# pyright: reportGeneralTypeIssues=false
# pyright: reportAttributeAccessIssue=false
# pyright: reportMissingModuleSource=false
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownVariableType=false
# pylint: disable=no-member
"""pixel.buttons - 按钮区域的红色检测。
- detect_attend_button_red_area 福袋详情页"参与抽奖"按钮
- detect_fansclub_join_red_area 粉丝团加入按钮
"""
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Optional
from ._common import _safe_open_screenshot_rgb
def _read_rgb_pixel(pic_rgb: Image.Image, x: int, y: int) -> tuple[int, int, int]:
pixel = pic_rgb.getpixel((x, y))
if isinstance(pixel, tuple):
return int(pixel[0]), int(pixel[1]), int(pixel[2])
value = int(pixel)
return value, value, value
def detect_attend_button_red_area(
pic_rgb: Image.Image,
return_info: bool = False,
red_ratio_threshold: float | None = None,
analyser: Any = None,
save_debug: bool = False,
) -> bool | tuple[bool, dict]:
"""检测参与按钮:大片红色区域。
参与按钮通常在屏幕底部,有大片红色背景。
检测区域:(306,2030)-(780,2110) 基准1080x2400,但实际可能更宽
扩展检测区域到更宽的范围以覆盖"一键参与评论"等按钮
"""
try:
from douyin.fudai.core.coords import get_pixel_region
width, height = pic_rgb.size
# 缩放坐标到实际尺寸
scale_x = width / 1080.0
scale_y = height / 2400.0
bx1, by1, bx2, by2 = get_pixel_region("attend_button", (200, 2000, 880, 2120))
x1 = int(bx1 * scale_x)
y1 = int(by1 * scale_y)
x2 = int(bx2 * scale_x)
y2 = int(by2 * scale_y)
# 确保坐标在范围内
x1 = max(0, min(x1, width))
y1 = max(0, min(y1, height))
x2 = max(0, min(x2, width))
y2 = max(0, min(y2, height))
if x1 >= x2 or y1 >= y2:
return False
# 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域
if save_debug:
try:
from douyin.handlers import douyin_screenshot_manager
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb, # 使用全屏截图
operation_type="pixel_detect",
picname="attend_button",
step="before_detect",
coords=(x1, y1, x2, y2), # 标注识别区域
extra_info={"region_size": f"{x2-x1}x{y2-y1}"},
)
if debug_path:
try:
import logging
logging.debug("[调试图像] 参与按钮检测前保存: %s", debug_path)
except Exception: # type: ignore[general-exception]
pass
except Exception: # type: ignore[general-exception]
pass
# 采样检测点:检测大片红色区域
red_count = 0
step_x = max(1, (x2 - x1) // 15)
step_y = max(1, (y2 - y1) // 10)
for y in range(y1, y2, step_y):
for x in range(x1, x2, step_x):
try:
r, g, b = _read_rgb_pixel(pic_rgb, x, y)
# 检测红色:R值高,G和B相对较低
if r > 200 and (r - g) > 50 and (r - b) > 50:
red_count += 1
except Exception as e: # type: ignore[general-exception]
continue
# 如果红色像素占比足够高(至少25%),认为有大片红色背景
total_samples = len(list(range(y1, y2, step_y))) * len(
list(range(x1, x2, step_x))
)
if total_samples == 0:
return False
red_ratio = red_count / total_samples if total_samples > 0 else 0
# 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值
if red_ratio_threshold is not None:
threshold = float(red_ratio_threshold)
elif analyser is not None:
try:
threshold = float(
getattr(analyser, "attend_button_red_ratio_threshold", 0.25) or 0.25
)
except Exception as e: # type: ignore[general-exception]
threshold = 0.25
else:
threshold = 0.25
result = red_ratio >= threshold
# 如果请求详细信息,返回包含red_ratio的字典
if return_info:
reason_value: Optional[str] = (
None if result else "red_ratio_below_threshold"
)
return result, {
"method": "pixel_detect",
"red_ratio": red_ratio,
"red_count": red_count,
"total_samples": total_samples,
"effective_threshold": threshold,
"reason": reason_value,
}
# 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域
# 无论成功失败都保存debug截图
if save_debug:
try:
from douyin.handlers import douyin_screenshot_manager
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb, # 使用全屏截图
operation_type="pixel_detect",
picname="attend_button",
step="detected" if result else "not_detected",
coords=(x1, y1, x2, y2), # 标注识别区域
extra_info={
"red_ratio": round(red_ratio, 4),
"red_count": red_count,
"total_samples": total_samples,
"threshold": threshold,
"result": "success" if result else "failed",
},
)
if debug_path:
try:
import logging
logging.debug(
"[调试图像] 参与按钮检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s",
debug_path,
red_ratio,
threshold,
"success" if result else "failed",
)
except Exception as e: # type: ignore[general-exception]
pass
except Exception as e: # type: ignore[general-exception]
pass
return result
except Exception:
if return_info:
return False, {
"method": "pixel_detect",
"red_ratio": None,
"reason": "exception",
}
return False
def detect_fansclub_join_red_area(
pic_rgb: Image.Image,
return_info: bool = False,
red_ratio_threshold: float | None = None,
analyser: Any = None,
save_debug: bool = False,
) -> bool | tuple[bool, dict]:
"""检测粉丝团加入:大片红色区域。
粉丝团加入按钮通常在屏幕中下部,有大片红色背景。
检测区域:(200,1940)-(880,2140) 基准1080x2400
"""
try:
from douyin.fudai.core.coords import get_pixel_region
width, height = pic_rgb.size
# 缩放坐标到实际尺寸
scale_x = width / 1080.0
scale_y = height / 2400.0
bx1, by1, bx2, by2 = get_pixel_region("fansclub_join", (200, 1940, 880, 2140))
x1 = int(bx1 * scale_x)
y1 = int(by1 * scale_y)
x2 = int(bx2 * scale_x)
y2 = int(by2 * scale_y)
# 确保坐标在范围内
x1 = max(0, min(x1, width))
y1 = max(0, min(y1, height))
x2 = max(0, min(x2, width))
y2 = max(0, min(y2, height))
if x1 >= x2 or y1 >= y2:
if return_info:
return False, {
"method": "pixel_detect",
"red_ratio": None,
"reason": "invalid_region",
}
return False
# 保存调试图像(检测前)- 使用全屏截图,用框框选出识别区域
if save_debug:
try:
from douyin.handlers import douyin_screenshot_manager
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb, # 使用全屏截图
operation_type="pixel_detect",
picname="fansclub_join",
step="before_detect",
coords=(x1, y1, x2, y2), # 标注识别区域
extra_info={"region_size": f"{x2-x1}x{y2-y1}"},
)
if debug_path:
try:
import logging
logging.debug(
"[调试图像] 粉丝团加入检测前保存: %s",
debug_path,
)
except Exception as e: # type: ignore[general-exception]
pass
except Exception as e: # type: ignore[general-exception]
pass
# 采样检测点:检测大片红色区域
red_count = 0
step_x = max(1, (x2 - x1) // 15)
step_y = max(1, (y2 - y1) // 10)
for y in range(y1, y2, step_y):
for x in range(x1, x2, step_x):
try:
r, g, b = _read_rgb_pixel(pic_rgb, x, y)
# 检测红色:R值高,G和B相对较低
if r > 200 and (r - g) > 50 and (r - b) > 50:
red_count += 1
except Exception:
continue
# 如果红色像素占比足够高(至少25%),认为有大片红色背景
total_samples = len(list(range(y1, y2, step_y))) * len(
list(range(x1, x2, step_x))
)
if total_samples == 0:
if return_info:
return False, {
"method": "pixel_detect",
"red_ratio": None,
"reason": "no_samples",
}
return False
red_ratio = red_count / total_samples if total_samples > 0 else 0
# 获取阈值:优先使用传入参数,其次从analyser读取,最后使用默认值
if red_ratio_threshold is not None:
threshold = float(red_ratio_threshold)
elif analyser is not None:
try:
threshold = float(
getattr(analyser, "fansclub_join_red_ratio_threshold", 0.25) or 0.25
)
except Exception as e: # type: ignore[general-exception]
threshold = 0.25
else:
threshold = 0.25
result = red_ratio >= threshold
# 保存调试图像(检测结果)- 使用全屏截图,用框框选出识别区域
if save_debug:
try:
from douyin.handlers import douyin_screenshot_manager
# 无论成功失败都保存debug截图
debug_path = douyin_screenshot_manager.save_debug_image(
pic_rgb, # 使用全屏截图
operation_type="pixel_detect",
picname="fansclub_join",
step="detected" if result else "not_detected",
coords=(x1, y1, x2, y2), # 标注识别区域
extra_info={
"red_ratio": round(red_ratio, 4),
"red_count": red_count,
"total_samples": total_samples,
"threshold": threshold,
"result": "success" if result else "failed",
},
)
if debug_path:
try:
import logging
logging.debug(
"[调试图像] 粉丝团加入检测保存: %s, red_ratio=%.4f, threshold=%.4f, result=%s",
debug_path,
red_ratio,
threshold,
"success" if result else "failed",
)
except Exception as e: # type: ignore[general-exception]
pass
except Exception as e: # type: ignore[general-exception]
pass
if return_info:
reason_value: Optional[str] = (
None if result else "red_ratio_below_threshold"
)
return result, {
"method": "pixel_detect",
"red_ratio": red_ratio,
"red_count": red_count,
"total_samples": total_samples,
"effective_threshold": threshold,
"reason": reason_value,
}
return result
except Exception:
if return_info:
return False, {
"method": "pixel_detect",
"red_ratio": None,
"reason": "exception",
}
return False