'commit'
This commit is contained in:
@@ -7,8 +7,7 @@ import uuid
|
||||
|
||||
import uiautomator2 as u2
|
||||
|
||||
from Apps.XinDianTu.Kit import take_screenshot, detect_black_agree_button, detect_any_ad_close, \
|
||||
detect_bottom_close_circle, is_background_dimmed, click_image_template
|
||||
from Apps.XinDianTu.Kit import take_screenshot
|
||||
from Apps.XinDianTu.ReadImageKit import ReadImageKit
|
||||
from Config.Config import TEMP_IMAGE_DIR
|
||||
|
||||
@@ -25,9 +24,9 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
async def check_and_close_ad(d):
|
||||
"""
|
||||
检测并关闭广告弹窗(优先使用计算机图形学方案,节省成本和时间)
|
||||
检测并关闭广告弹窗(仅使用 VL 视觉模型)
|
||||
"""
|
||||
logger.info("开始检测广告弹窗...")
|
||||
logger.info("开始检测广告弹窗 (VL方案)...")
|
||||
|
||||
# 1. 拍摄截图
|
||||
t1 = time.time()
|
||||
@@ -37,59 +36,7 @@ async def check_and_close_ad(d):
|
||||
screenshot_path = take_screenshot(d, image_uuid, save_dir=save_dir)
|
||||
logger.info(f"Step [广告检测截图] 耗时: {time.time() - t1:.4f}s")
|
||||
|
||||
# 1.1 特征预检:检测背景蒙板
|
||||
is_dimmed = is_background_dimmed(screenshot_path)
|
||||
if is_dimmed:
|
||||
logger.info("检测到背景变暗(蒙板),极大概率存在广告弹窗。")
|
||||
|
||||
# 2. 图形学检测方案 (Local CV)
|
||||
t_cv = time.time()
|
||||
|
||||
# 方案 A: 检测黑色的"同意"按钮 (隐私协议)
|
||||
# 这一步不需要模板,直接通过颜色和形状特征检测
|
||||
agree_pos = detect_black_agree_button(screenshot_path, debug_dir=save_dir)
|
||||
if agree_pos:
|
||||
x, y = agree_pos
|
||||
logger.info(f"通过图形学算法检测到隐私协议'同意'按钮: ({x}, {y})")
|
||||
d.click(x, y)
|
||||
logger.info(f"Step [图形学检测-同意按钮] 耗时: {time.time() - t_cv:.4f}s")
|
||||
# 清理截图
|
||||
if os.path.exists(screenshot_path):
|
||||
os.remove(screenshot_path)
|
||||
return True
|
||||
|
||||
# 方案 B: 多模板匹配检测 "关闭(X)" 按钮 (支持多种广告样式)
|
||||
template_dir = os.path.join(BASE_DIR, "Templates")
|
||||
|
||||
if os.path.exists(template_dir):
|
||||
logger.info(f"正在尝试多模板匹配关闭按钮: {template_dir}")
|
||||
# 如果检测到蒙板,可以适当调低匹配阈值
|
||||
match_threshold = 0.6 if is_dimmed else 0.7
|
||||
close_pos = detect_ad_close_x_with_threshold(screenshot_path, template_dir, save_dir, match_threshold)
|
||||
|
||||
if close_pos:
|
||||
x, y = close_pos
|
||||
logger.info(f"通过多模板匹配检测到广告关闭按钮: ({x}, {y})")
|
||||
d.click(x, y)
|
||||
logger.info(f"Step [图形学检测-关闭按钮] 耗时: {time.time() - t_cv:.4f}s")
|
||||
if os.path.exists(screenshot_path):
|
||||
os.remove(screenshot_path)
|
||||
return True
|
||||
|
||||
# 方案 C: 底部圆形关闭按钮检测 (无模板,基于几何特征)
|
||||
# 针对插屏广告底部中间的圆形关闭按钮
|
||||
circle_pos = detect_bottom_close_circle(screenshot_path, debug_dir=save_dir)
|
||||
if circle_pos:
|
||||
x, y = circle_pos
|
||||
logger.info(f"通过几何特征检测到底部圆形关闭按钮: ({x}, {y})")
|
||||
d.click(x, y)
|
||||
logger.info(f"Step [图形学检测-底部圆形按钮] 耗时: {time.time() - t_cv:.4f}s")
|
||||
if os.path.exists(screenshot_path):
|
||||
os.remove(screenshot_path)
|
||||
return True
|
||||
|
||||
# 3. 视觉大模型检测方案 (VL Model) - 作为兜底
|
||||
logger.info("本地图形学检测未发现广告,尝试调用视觉大模型检测...")
|
||||
# 2. 视觉大模型检测方案 (VL Model)
|
||||
try:
|
||||
window_size = d.window_size()
|
||||
device_info = {
|
||||
@@ -98,9 +45,10 @@ async def check_and_close_ad(d):
|
||||
"productName": d.info.get('productName', 'unknown')
|
||||
}
|
||||
|
||||
ad_result = await ReadImageKit.detect_ad(screenshot_path, device_info=device_info)
|
||||
if ad_result.get("has_ad") and ad_result.get("uia_center_x") is not None:
|
||||
x, y = ad_result["uia_center_x"], ad_result["uia_center_y"]
|
||||
# 使用最新的 detect_ad_popup 方法
|
||||
ad_result = await ReadImageKit.detect_ad_popup(screenshot_path, device_info=device_info)
|
||||
if ad_result:
|
||||
x, y = ad_result["x"], ad_result["y"]
|
||||
logger.info(f"通过视觉大模型检测到广告关闭按钮: ({x}, {y})")
|
||||
d.click(x, y)
|
||||
if os.path.exists(screenshot_path):
|
||||
@@ -109,7 +57,7 @@ async def check_and_close_ad(d):
|
||||
except Exception as e:
|
||||
logger.error(f"视觉大模型广告检测异常: {e}")
|
||||
|
||||
logger.info(f"本地图形学和 VL 检测完成,未发现已知广告。")
|
||||
logger.info(f"VL 检测完成,未发现已知广告。")
|
||||
|
||||
# 清理本地截图
|
||||
if os.path.exists(screenshot_path):
|
||||
@@ -117,18 +65,6 @@ async def check_and_close_ad(d):
|
||||
|
||||
return False
|
||||
|
||||
def detect_ad_close_x_with_threshold(screenshot_path, template_dir, debug_dir, threshold):
|
||||
"""
|
||||
带自定义阈值的多模板匹配
|
||||
"""
|
||||
for filename in os.listdir(template_dir):
|
||||
if filename.startswith("ad_close") and filename.endswith(".jpg"):
|
||||
template_path = os.path.join(template_dir, filename)
|
||||
from Apps.XinDianTu.Kit import detect_ad_close_x
|
||||
pos = detect_ad_close_x(screenshot_path, template_path, debug_dir=debug_dir, threshold=threshold)
|
||||
if pos:
|
||||
return pos
|
||||
return None
|
||||
|
||||
|
||||
async def open_mini_program():
|
||||
|
||||
Reference in New Issue
Block a user