refactor: 优化代码格式,提升可读性

This commit is contained in:
Your Name committed 2026-01-08 20:39:04 +08:00
1 parent fb0c07c91a
commit 6ecd9fa1ae
7 files changed
+125 -61

No files matched your search

+13 -13
View File
@@ -174,63 +174,63 @@ def detect_coupon_popup_background(
def detect_redpacket_popup_background(pic_rgb: Image.Image) -> bool:
"""中央购物红包弹窗检测:检测中央大红包区域的红色占比。
购物红包特征:
- 中央大型红色弹窗
- 包含黄色圆形"抢"按钮
- 主要颜色为红色(RGB约 255, 100-150, 100-150)
检测区域(基准1080x2400):大约中央区域
- 左上:(300, 400)
- 右下:(780, 1200)
"""
try:
import numpy as np # type: ignore[import-untyped]
width, height = pic_rgb.size
scale_x = width / 1080.0
scale_y = height / 2400.0
# 购物红包通常出现在屏幕中央
# 基准坐标(1080x2400)
x1 = int(200 * scale_x)
y1 = int(300 * scale_y)
x2 = int(880 * scale_x)
y2 = int(1400 * scale_y)
# 边界检查
x1 = max(0, min(x1, width))
x2 = max(0, min(x2, width))
y1 = max(0, min(y1, height))
y2 = max(0, min(y2, height))
if x1 >= x2 or y1 >= y2:
return False
# 裁剪感兴趣区域
roi = pic_rgb.crop((x1, y1, x2, y2))
arr = np.array(roi)
if arr.size == 0:
return False
# 提取RGB通道
r = arr[:, :, 0] # type: ignore[index]
g = arr[:, :, 1] # type: ignore[index]
b = arr[:, :, 2] # type: ignore[index]
# 红色特征:R > 200, G < 180, B < 180
# 购物红包的红色通常很纯,R值很高
mask = (r >= 220) & (g <= 160) & (b <= 160)
# 计算红色像素占比
total_pixels = mask.size
red_pixels = np.sum(mask) # type: ignore[call-overload]
red_ratio = red_pixels / total_pixels if total_pixels > 0 else 0
# 阈值:至少15%的像素满足红色特征
return red_ratio > 0.15
except Exception:
return False