Files
douyin_guaji-main/douyin_pixel.py
T
Your Name 836a31d817 refactor: Remove outdated files and enhance Web GUI functionality
- Deleted obsolete markdown files related to follow tab coordinates testing and results.
- Removed outdated batch scripts for launching the Web GUI.
- Updated `config.json` to improve reservation handling and adjust prize value settings.
- Enhanced logging and error handling in `web_gui.py` and `app.js` for better user experience.
- Added automatic screenshot functionality during Web GUI startup for improved monitoring.
2025-12-19 03:31:08 +08:00

355 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any
from PIL import Image
def _safe_open_screenshot_rgb(pic_path: str) -> Image.Image | None:
try:
if not pic_path or (not os.path.exists(pic_path)):
return None
pic = Image.open(pic_path)
return pic.convert("RGB")
except Exception:
return None
def detect_coupon_popup_background(pic_rgb: Image.Image) -> bool:
"""底部优惠券大弹窗背景(粉/红)像素快速检测。"""
try:
width, height = pic_rgb.size
check_y_base = int(height * 0.5)
check_points = [
(width // 2, height - 200), # 底部中心(按钮区域)
(width // 2, height - 400), # 底部中心偏上(标题区域)
(width // 3, height - 300), # 左侧
(width * 2 // 3, height - 300), # 右侧
(width // 2, check_y_base), # 屏幕中部(弹窗可能延伸到这里)
]
pink_red_count = 0
for x, y in check_points:
if 0 <= x < width and 0 <= y < height:
r, g, b = pic_rgb.getpixel((x, y))
# 与原逻辑一致:R 值较高且明显偏红
if r > 180 and (r - g) > 50 and (r - b) > 50:
pink_red_count += 1
return pink_red_count >= 2
except Exception:
return False
def detect_redpacket_popup_background(pic_rgb: Image.Image) -> bool:
"""中央购物红包弹窗背景(红色信封 + 黄色按钮)像素快速检测。"""
try:
width, height = pic_rgb.size
center_x = width // 2
center_y = height // 2
check_points = [
(center_x, center_y), # 屏幕正中心
(center_x, center_y - 200), # 中心偏上(信封顶部)
(center_x, center_y + 200), # 中心偏下(信封底部)
(center_x - 150, center_y), # 中心偏左
(center_x + 150, center_y), # 中心偏右
]
red_count = 0
yellow_count = 0
for x, y in check_points:
if 0 <= x < width and 0 <= y < height:
r, g, b = pic_rgb.getpixel((x, y))
if r > 200 and g < 100 and b < 100:
red_count += 1
if r > 200 and g > 200 and b < 150:
yellow_count += 1
return (red_count >= 2) or (yellow_count >= 1)
except Exception:
return False
def detect_lucky_draw_result_popup(pic_rgb: Image.Image) -> bool:
"""检测抽奖结果弹窗:大片红色背景 + 中间有"我知道了"按钮区域。
抽奖结果弹窗通常在屏幕中下部,有大片红色背景,中间有白色/浅色的"我知道了"按钮。
检测区域:(380,660)-(720,755) 基准1080x2400
"""
try:
width, height = pic_rgb.size
# 缩放坐标到实际尺寸
scale_x = width / 1080.0
scale_y = height / 2400.0
# 检测区域(基准1080x2400)
x1 = int(380 * scale_x)
y1 = int(660 * scale_y)
x2 = int(720 * scale_x)
y2 = int(755 * 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
# 采样检测点:检测大片红色区域
red_count = 0
sample_points = []
step_x = max(1, (x2 - x1) // 10)
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 = pic_rgb.getpixel((x, y))
# 检测红色:R值高,G和B相对较低
if r > 200 and (r - g) > 50 and (r - b) > 50:
red_count += 1
except Exception:
continue
# 如果红色像素占比足够高(至少30%),认为有大片红色背景
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
return red_ratio >= 0.3
except Exception:
return False
def detect_attend_button_red_area(pic_rgb: Image.Image) -> bool:
"""检测参与按钮:大片红色区域。
参与按钮通常在屏幕底部,有大片红色背景。
检测区域:(306,2030)-(780,2110) 基准1080x2400,但实际可能更宽
扩展检测区域到更宽的范围以覆盖"一键参与评论"等按钮
"""
try:
width, height = pic_rgb.size
# 缩放坐标到实际尺寸
scale_x = width / 1080.0
scale_y = height / 2400.0
# 检测区域(基准1080x2400,扩展宽度以覆盖更宽的按钮)
x1 = int(200 * scale_x) # 扩展左侧
y1 = int(2000 * scale_y) # 稍微上移
x2 = int(880 * scale_x) # 扩展右侧
y2 = int(2120 * 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
# 采样检测点:检测大片红色区域
red_count = 0
sample_points = []
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 = pic_rgb.getpixel((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:
return False
red_ratio = red_count / total_samples if total_samples > 0 else 0
return red_ratio >= 0.25
except Exception:
return False
def detect_fansclub_join_red_area(pic_rgb: Image.Image) -> bool:
"""检测粉丝团加入:大片红色区域。
粉丝团加入按钮通常在屏幕中下部,有大片红色背景。
检测区域:(200,1940)-(880,2140) 基准1080x2400
"""
try:
width, height = pic_rgb.size
# 缩放坐标到实际尺寸
scale_x = width / 1080.0
scale_y = height / 2400.0
# 检测区域(基准1080x2400)
x1 = int(200 * scale_x)
y1 = int(1940 * scale_y)
x2 = int(880 * scale_x)
y2 = int(2140 * 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
# 采样检测点:检测大片红色区域
red_count = 0
sample_points = []
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 = pic_rgb.getpixel((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:
return False
red_ratio = red_count / total_samples if total_samples > 0 else 0
return red_ratio >= 0.25
except Exception:
return False
def detect_live_room_icons_by_pixel(pic_rgb: Image.Image) -> tuple[bool, float]:
"""通过像素检测右上角x和xin图标区域,判断是否在直播间。
检测右上角区域(60%-100%宽度,0%-40%高度)的像素特征:
- x图标:通常是白色或浅色的X形状
- xin图标:通常是粉色/红色的心形图标
返回:
(detected, confidence) 其中 confidence 为置信度(0.0~1.0)
"""
try:
width, height = pic_rgb.size
# 缩放坐标到实际尺寸(基于1080x2400)
scale_x = width / 1080.0
scale_y = height / 2400.0
# 检测区域:右上角(60%-100%宽度,0%-40%高度)
x1 = int(648 * scale_x) # 60%宽度 = 648
y1 = 0
x2 = width # 100%宽度
y2 = int(960 * scale_y) # 40%高度 = 960
# 确保坐标在范围内
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, 0.0
# 采样检测点
step_x = max(1, (x2 - x1) // 20)
step_y = max(1, (y2 - y1) // 20)
# 检测特征:
# 1. 白色/浅色像素(x图标)
# 2. 粉色/红色像素(xin图标)
white_count = 0
pink_red_count = 0
total_samples = 0
for y in range(y1, y2, step_y):
for x in range(x1, x2, step_x):
try:
r, g, b = pic_rgb.getpixel((x, y))
total_samples += 1
# 检测白色/浅色(x图标):RGB值都较高且接近
if r > 200 and g > 200 and b > 200 and abs(r - g) < 30 and abs(g - b) < 30:
white_count += 1
# 检测粉色/红色(xin图标):R值高,G和B相对较低
if r > 200 and (r - g) > 50 and (r - b) > 50:
pink_red_count += 1
except Exception:
continue
if total_samples == 0:
return False, 0.0
# 计算置信度
white_ratio = white_count / total_samples if total_samples > 0 else 0
pink_red_ratio = pink_red_count / total_samples if total_samples > 0 else 0
# 如果白色或粉色/红色像素占比足够高,认为检测到图标
# 阈值:白色>=5% 或 粉色/红色>=3%
detected = white_ratio >= 0.05 or pink_red_ratio >= 0.03
# 计算置信度(基于检测到的像素比例)
confidence = min(1.0, max(white_ratio * 10, pink_red_ratio * 15))
return detected, confidence
except Exception:
return False, 0.0
def read_pixel_stage_state_from_screenshot(pic_path: str) -> dict[str, bool]:
"""从 screenshot.png 读取"阶段诊断"需要的多区域像素特征(不调用 OCR)。
使用多线程并行执行所有检测函数以提高性能。
"""
state: dict[str, bool] = {
"has_coupon_bg": False,
"has_redpacket_bg": False,
"has_lucky_draw_result": False,
"has_attend_button_red": False,
"has_fansclub_join_red": False,
}
pic_rgb = _safe_open_screenshot_rgb(pic_path)
if pic_rgb is None:
return state
# 并行执行所有检测函数
detection_tasks = {
"has_coupon_bg": detect_coupon_popup_background,
"has_redpacket_bg": detect_redpacket_popup_background,
"has_lucky_draw_result": detect_lucky_draw_result_popup,
"has_attend_button_red": detect_attend_button_red_area,
"has_fansclub_join_red": detect_fansclub_join_red_area,
}
# 使用线程池并行执行检测
with ThreadPoolExecutor(max_workers=5) as executor:
# 提交所有任务
future_to_key = {
executor.submit(detect_func, pic_rgb): key
for key, detect_func in detection_tasks.items()
}
# 收集结果
for future in as_completed(future_to_key):
key = future_to_key[future]
try:
result = future.result()
state[key] = bool(result)
except Exception:
# 如果某个检测失败,保持默认值 False
state[key] = False
return state