Reduce fudai detection overhead
This commit is contained in:
1 parent
58c1856fad
commit
b3d3f28ff3
4 files changed
+117
-161
No files matched your search
@@ -9,49 +9,70 @@ import time as time_module
|
||||
import logging
|
||||
|
||||
|
||||
def _screenshot_trace_enabled() -> bool:
|
||||
"""Return whether verbose per-screenshot stack tracing is enabled."""
|
||||
try:
|
||||
flag = str(os.environ.get("DOUYIN_SCREENSHOT_TRACE", "")).strip().lower()
|
||||
return flag in {"1", "true", "yes", "on"}
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _append_screenshot_trace(self, record: dict) -> None:
|
||||
try:
|
||||
if hasattr(self, "_screenshot_call_cache"):
|
||||
try:
|
||||
self._screenshot_call_cache.append(record)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
|
||||
_lf.write(json.dumps(record, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
"""截图(支持后台服务模式)"""
|
||||
start_ts = time_module.time()
|
||||
trace_enabled = _screenshot_trace_enabled()
|
||||
|
||||
try:
|
||||
import inspect
|
||||
if trace_enabled:
|
||||
try:
|
||||
import inspect
|
||||
|
||||
frames = []
|
||||
for fr in inspect.stack()[1:9]:
|
||||
frame_info = {
|
||||
"file": os.path.relpath(fr.filename, os.path.dirname(__file__)),
|
||||
"line": fr.lineno,
|
||||
"function": fr.function,
|
||||
frames = []
|
||||
for fr in inspect.stack()[1:9]:
|
||||
frame_info = {
|
||||
"file": os.path.relpath(fr.filename, os.path.dirname(__file__)),
|
||||
"line": fr.lineno,
|
||||
"function": fr.function,
|
||||
}
|
||||
frames.append(frame_info)
|
||||
|
||||
pre_record = {
|
||||
"ts": datetime.now().isoformat(),
|
||||
"device_id": device_id,
|
||||
"use_service": bool(use_service),
|
||||
"path": path,
|
||||
"phase": "start",
|
||||
"thread": threading.get_ident(),
|
||||
"stack": frames,
|
||||
}
|
||||
frames.append(frame_info)
|
||||
|
||||
pre_record = {
|
||||
"ts": datetime.now().isoformat(),
|
||||
"device_id": device_id,
|
||||
"use_service": bool(use_service),
|
||||
"path": path,
|
||||
"phase": "start",
|
||||
"thread": threading.get_ident(),
|
||||
"stack": frames,
|
||||
}
|
||||
try:
|
||||
if hasattr(self, "_screenshot_call_cache"):
|
||||
try:
|
||||
self._screenshot_call_cache.append(pre_record)
|
||||
except Exception:
|
||||
pass
|
||||
_append_screenshot_trace(self, pre_record)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
elif not getattr(self, "_screenshot_trace_disabled_logged", False):
|
||||
try:
|
||||
log_dir = os.path.join(os.path.dirname(__file__), "logs")
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
|
||||
_lf.write(json.dumps(pre_record, ensure_ascii=False) + "\n")
|
||||
logging.info(
|
||||
"[截图] screenshot_calls.jsonl 调用栈追踪默认关闭;如需诊断设置 DOUYIN_SCREENSHOT_TRACE=1"
|
||||
)
|
||||
self._screenshot_trace_disabled_logged = True
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
service_success = False
|
||||
if use_service:
|
||||
@@ -72,7 +93,7 @@ def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
if service.get_screenshot(device_id, force=False):
|
||||
dur = (time_module.time() - start_ts) * 1000.0
|
||||
logging.info("[截图] 使用后台服务耗时(ms)=%.1f device=%s", dur, device_id)
|
||||
try:
|
||||
if trace_enabled:
|
||||
end_record = {
|
||||
"ts": datetime.now().isoformat(),
|
||||
"device_id": device_id,
|
||||
@@ -83,18 +104,7 @@ def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
"duration_ms": dur,
|
||||
"thread": threading.get_ident(),
|
||||
}
|
||||
try:
|
||||
if hasattr(self, "_screenshot_call_cache"):
|
||||
self._screenshot_call_cache.append(end_record)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
|
||||
_lf.write(json.dumps(end_record, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
_append_screenshot_trace(self, end_record)
|
||||
service_success = True
|
||||
else:
|
||||
timetag = datetime.now().strftime("%H:%M:%S")
|
||||
@@ -123,7 +133,7 @@ def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
dur = (time_module.time() - start_ts) * 1000.0
|
||||
logging.info("[截图] 总耗时(ms)=%.1f use_service=%s result=%s", dur, bool(use_service), final_result)
|
||||
|
||||
try:
|
||||
if trace_enabled:
|
||||
end_record = {
|
||||
"ts": datetime.now().isoformat(),
|
||||
"device_id": device_id,
|
||||
@@ -134,17 +144,6 @@ def get_screenshot(self, device_id, path="pic", use_service: bool = False):
|
||||
"duration_ms": dur,
|
||||
"thread": threading.get_ident(),
|
||||
}
|
||||
try:
|
||||
if hasattr(self, "_screenshot_call_cache"):
|
||||
self._screenshot_call_cache.append(end_record)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
|
||||
_lf.write(json.dumps(end_record, ensure_ascii=False) + "\n")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
_append_screenshot_trace(self, end_record)
|
||||
|
||||
return bool(final_result)
|
||||
@@ -7,22 +7,8 @@ from __future__ import annotations
|
||||
|
||||
# pylint: disable=E1101
|
||||
|
||||
import os
|
||||
import time
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from douyin.core import path_config
|
||||
|
||||
# OCR 预处理接口
|
||||
from douyin.utils.ocr_preprocess import preprocess_for_ocr # type: ignore
|
||||
|
||||
# 调试图像管理器(用于保存调试截图)
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
except ImportError:
|
||||
douyin_screenshot_manager = None
|
||||
from typing import Any
|
||||
|
||||
|
||||
class FudaiClickExecutor:
|
||||
@@ -41,7 +27,7 @@ class FudaiClickExecutor:
|
||||
self.rx, self.ry = 1080, 2400
|
||||
|
||||
def click(self, fudai_x: int) -> bool:
|
||||
"""点击福袋图标(点击前会检查是否是红包,避免误点)
|
||||
"""点击福袋图标。
|
||||
|
||||
参数:
|
||||
fudai_x: 福袋图标的X坐标(1080基准)
|
||||
@@ -67,93 +53,14 @@ class FudaiClickExecutor:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 点击前检查:避免误点红包
|
||||
try:
|
||||
# 检查是否有红包弹窗
|
||||
from douyin.other import douyin_popups
|
||||
|
||||
has_redpacket = douyin_popups.check_shopping_redpacket_popup(self.analyser)
|
||||
if has_redpacket:
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 点击",
|
||||
"跳过(检测到红包弹窗)",
|
||||
fudai_x=fudai_x,
|
||||
note="检测到红包弹窗,避免误点红包",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
# 快速检查点击位置附近是否有红包特征(通过OCR)
|
||||
# 检查点击位置上方和下方的区域,看是否有红包关键词
|
||||
try:
|
||||
# 截图点击位置附近的区域(扩大范围以包含可能的红包文字)
|
||||
check_x1 = max(0, click_x - 150)
|
||||
check_x2 = min(self.rx, click_x + 150)
|
||||
check_y1 = max(0, click_y - 200)
|
||||
check_y2 = min(self.ry, click_y + 200)
|
||||
|
||||
# 转换为1080基准坐标
|
||||
check_x1_base = int(check_x1 * 1080 / self.rx)
|
||||
check_y1_base = int(check_y1 * 2400 / self.ry)
|
||||
check_x2_base = int(check_x2 * 1080 / self.rx)
|
||||
check_y2_base = int(check_y2 * 2400 / self.ry)
|
||||
|
||||
# 截图检查区域
|
||||
self.analyser.cut_pic(
|
||||
check_x1_base,
|
||||
check_y1_base,
|
||||
check_x2_base,
|
||||
check_y2_base,
|
||||
"",
|
||||
"檢查紅包特徵",
|
||||
)
|
||||
|
||||
# OCR识别
|
||||
name_chk = preprocess_for_ocr(self.analyser, "檢查紅包特徵")
|
||||
check_text = str(
|
||||
self.analyser.operation.analyse_pic_word(name_chk, 0) or ""
|
||||
).strip()
|
||||
|
||||
# 检查是否包含红包关键词
|
||||
redpacket_keywords = (
|
||||
"最高金额",
|
||||
"开抢",
|
||||
"邀你抢",
|
||||
"抢红包",
|
||||
"购物红包",
|
||||
"手气红包",
|
||||
)
|
||||
if check_text and any(kw in check_text for kw in redpacket_keywords):
|
||||
matched_keywords = [
|
||||
kw for kw in redpacket_keywords if kw in check_text
|
||||
]
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 点击",
|
||||
"跳过(检测到红包特征)",
|
||||
fudai_x=fudai_x,
|
||||
matched_keywords=matched_keywords,
|
||||
check_text_preview=check_text[:50],
|
||||
note="点击位置附近检测到红包特征文字,避免误点红包",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
except Exception as e:
|
||||
# OCR检查失败不影响点击,继续执行
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 点击",
|
||||
"红包特征检查异常,继续点击",
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
self.analyser._log_kv(
|
||||
"[福袋查找] 点击",
|
||||
"跳过点击前红包OCR拦截,交由点击后阶段诊断",
|
||||
fudai_x=fudai_x,
|
||||
note="避免切图+OCR拖慢;若点入优惠券/购物红包,后续关闭并按位置交换重试",
|
||||
)
|
||||
except Exception:
|
||||
# 红包检查失败不影响点击,继续执行
|
||||
pass
|
||||
|
||||
# 执行点击
|
||||
|
||||
@@ -685,11 +685,24 @@ class FudaiDetector:
|
||||
dark_bg = (r <= 90) & (g <= 90) & (b <= 105)
|
||||
red_body = (r >= 190) & (g <= 115) & (b <= 135)
|
||||
|
||||
warm_area, warm_box = self._largest_component_box(warm_left)
|
||||
red_area, _red_box = self._largest_component_box(red_body)
|
||||
warm_area = int(np.sum(warm_left))
|
||||
red_area = int(np.sum(red_body))
|
||||
dark_ratio = float(np.mean(dark_bg)) if dark_bg.size else 0.0
|
||||
warm_ratio_left = float(np.mean(warm_left)) if warm_left.size else 0.0
|
||||
|
||||
warm_box = None
|
||||
if warm_area > 0:
|
||||
ys, xs = np.nonzero(warm_left)
|
||||
if xs.size and ys.size:
|
||||
x_min = int(xs.min())
|
||||
y_min = int(ys.min())
|
||||
warm_box = (
|
||||
x_min,
|
||||
y_min,
|
||||
int(xs.max()) - x_min + 1,
|
||||
int(ys.max()) - y_min + 1,
|
||||
)
|
||||
|
||||
warm_box_large = False
|
||||
warm_box_left = False
|
||||
if warm_box is not None:
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
from __future__ import annotations
|
||||
|
||||
class _FakeOperation:
|
||||
def __init__(self, log_path):
|
||||
self._screenshot_log_path = str(log_path)
|
||||
self._screenshot_call_cache = []
|
||||
|
||||
def _get_screenshot_direct(self, _device_id, _path):
|
||||
return True
|
||||
|
||||
|
||||
def test_screenshot_trace_is_off_by_default(tmp_path, monkeypatch):
|
||||
from douyin.core.underlying_operations_pkg.screenshot_pipeline import get_screenshot
|
||||
|
||||
monkeypatch.delenv("DOUYIN_SCREENSHOT_TRACE", raising=False)
|
||||
op = _FakeOperation(tmp_path / "screenshot_calls.jsonl")
|
||||
|
||||
assert get_screenshot(op, "device1") is True
|
||||
|
||||
assert not (tmp_path / "screenshot_calls.jsonl").exists()
|
||||
assert op._screenshot_call_cache == []
|
||||
assert op._screenshot_trace_disabled_logged is True
|
||||
|
||||
|
||||
def test_screenshot_trace_can_be_enabled_for_diagnostics(tmp_path, monkeypatch):
|
||||
from douyin.core.underlying_operations_pkg.screenshot_pipeline import get_screenshot
|
||||
|
||||
monkeypatch.setenv("DOUYIN_SCREENSHOT_TRACE", "1")
|
||||
op = _FakeOperation(tmp_path / "screenshot_calls.jsonl")
|
||||
|
||||
assert get_screenshot(op, "device1", path="pic") is True
|
||||
|
||||
lines = (tmp_path / "screenshot_calls.jsonl").read_text(encoding="utf-8").splitlines()
|
||||
assert len(lines) == 2
|
||||
assert '"phase": "start"' in lines[0]
|
||||
assert '"phase": "end"' in lines[1]
|
||||
assert len(op._screenshot_call_cache) == 2
|
||||
Reference in new issue
Block a user