- Updated `config.json` to modify `yanghao_recommend_interval_seconds` for better timing control. - Refactored logging in `douyin_detection.py` to enhance readability and error handling. - Improved pixel loading and error management in image processing functions across multiple modules. - Enhanced GUI elements in `gui_app.py` to support new configuration options and improve user experience.
234 lines
8.3 KiB
Python
234 lines
8.3 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import time
|
|
from PIL import Image
|
|
|
|
import douyin_pixel
|
|
|
|
|
|
def check_coupon_popup(analyser) -> bool:
|
|
"""优惠券弹窗检测(仅模板匹配,使用 assets/coupon.png)。"""
|
|
check_start = time.time()
|
|
try:
|
|
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
|
pic = Image.open(pic_path)
|
|
pic.load() # Force load to catch truncated files early
|
|
pic = pic.convert("RGB")
|
|
matched = bool(douyin_pixel.detect_coupon_popup_background(pic))
|
|
except (OSError, IOError, SyntaxError) as e:
|
|
# Handle truncated image files
|
|
try:
|
|
analyser._log_kv("彈窗檢測步驟", "優惠券-圖片文件損壞或截斷", error=str(e))
|
|
except Exception:
|
|
pass
|
|
return False
|
|
except Exception as e:
|
|
try:
|
|
analyser._log_kv("彈窗檢測步驟", "優惠券-讀取截圖失敗", error=str(e))
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
total_elapsed = time.time() - check_start
|
|
try:
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"優惠券-模板匹配結果",
|
|
matched=int(bool(matched)),
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
)
|
|
except Exception:
|
|
pass
|
|
return matched
|
|
|
|
|
|
def check_shopping_redpacket_popup(analyser) -> bool:
|
|
"""從 `douyin_fudai.fudai_analyse._check_shopping_redpacket_popup` 抽離。
|
|
|
|
像素優先,OCR 僅作補充且不推翻像素結論。
|
|
"""
|
|
check_start = time.time()
|
|
|
|
# 步驟0:快速像素檢測中央紅色彈窗背景
|
|
pixel_check_start = time.time()
|
|
has_popup_pixel = False
|
|
try:
|
|
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
|
pic = Image.open(pic_path)
|
|
pic.load() # Force load to catch truncated files early
|
|
pic = pic.convert("RGB")
|
|
has_popup_pixel = bool(douyin_pixel.detect_redpacket_popup_background(pic))
|
|
_ = time.time() - pixel_check_start
|
|
if not has_popup_pixel:
|
|
return False
|
|
except (OSError, IOError, SyntaxError):
|
|
# Handle truncated image files
|
|
_ = time.time() - pixel_check_start
|
|
has_popup_pixel = False
|
|
except Exception:
|
|
_ = time.time() - pixel_check_start
|
|
has_popup_pixel = False
|
|
|
|
# 步驟1:截圖檢測區域
|
|
cut_start = time.time()
|
|
try:
|
|
analyser.cut_pic(230, 450, 860, 550, "", "診斷購物紅包")
|
|
_ = time.time() - cut_start
|
|
except Exception as e:
|
|
cut_elapsed = time.time() - cut_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-截圖區域失敗",
|
|
error=str(e),
|
|
耗時秒=f"{cut_elapsed:.3f}",
|
|
)
|
|
return False
|
|
|
|
# OCR 模式0
|
|
ocr_mode0_start = time.time()
|
|
try:
|
|
t = str(analyser.operation.analyse_pic_word("診斷購物紅包", 0) or "")
|
|
hay = t.strip() if t else ""
|
|
ocr_mode0_elapsed = time.time() - ocr_mode0_start
|
|
except Exception as e:
|
|
hay = ""
|
|
ocr_mode0_elapsed = time.time() - ocr_mode0_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-OCR(模式0)失敗",
|
|
error=str(e),
|
|
耗時秒=f"{ocr_mode0_elapsed:.3f}",
|
|
)
|
|
|
|
if not hay:
|
|
if has_popup_pixel:
|
|
total_elapsed = time.time() - check_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-OCR內容為空但像素判定有紅包彈窗,按像素結果返回",
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
像素判定="有紅包彈窗背景",
|
|
)
|
|
return True
|
|
|
|
# 嘗試按鈕區域快速 OCR(兜底)
|
|
try:
|
|
analyser.cut_pic(400, 1200, 680, 1400, "", "診斷購物紅包按鈕")
|
|
btn_text = str(
|
|
analyser.operation.analyse_pic_word("診斷購物紅包按鈕", 0) or ""
|
|
).strip()
|
|
if "搶" in btn_text:
|
|
total_elapsed = time.time() - check_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-檢測到彈窗(按鈕區域)",
|
|
識別文本=btn_text[:50],
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
檢測方式="按鈕區域快速檢測",
|
|
)
|
|
return True
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
keywords = ("邀你搶購物紅包", "搶購物紅包", "購物紅包", "邀你搶", "搶紅包", "搶")
|
|
if any(kw in hay for kw in keywords):
|
|
matched = [kw for kw in keywords if kw in hay]
|
|
total_elapsed = time.time() - check_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-檢測到彈窗",
|
|
匹配到的關鍵詞=matched,
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
OCR耗時秒=f"{ocr_mode0_elapsed:.3f}",
|
|
)
|
|
return True
|
|
|
|
texts = [hay]
|
|
ocr_mode1_elapsed = 0.0
|
|
ocr_mode2_elapsed = 0.0
|
|
|
|
# 模式1
|
|
ocr_mode1_start = time.time()
|
|
try:
|
|
t1 = str(analyser.operation.analyse_pic_word("診斷購物紅包", 1) or "")
|
|
hay1 = t1.strip() if t1 else ""
|
|
ocr_mode1_elapsed = time.time() - ocr_mode1_start
|
|
if hay1:
|
|
texts.append(hay1)
|
|
if any(kw in hay1 for kw in keywords):
|
|
matched = [kw for kw in keywords if kw in hay1]
|
|
total_elapsed = time.time() - check_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-檢測到彈窗(模式1)",
|
|
匹配到的關鍵詞=matched,
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
OCR耗時秒=f"{(ocr_mode0_elapsed + ocr_mode1_elapsed):.3f}",
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
ocr_mode1_elapsed = time.time() - ocr_mode1_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-OCR(模式1)失敗",
|
|
error=str(e),
|
|
耗時秒=f"{ocr_mode1_elapsed:.3f}",
|
|
)
|
|
|
|
# 模式2
|
|
ocr_mode2_start = time.time()
|
|
try:
|
|
t2 = str(analyser.operation.analyse_pic_word("診斷購物紅包", 2) or "")
|
|
hay2 = t2.strip() if t2 else ""
|
|
ocr_mode2_elapsed = time.time() - ocr_mode2_start
|
|
if hay2:
|
|
texts.append(hay2)
|
|
if any(kw in hay2 for kw in keywords):
|
|
matched = [kw for kw in keywords if kw in hay2]
|
|
total_elapsed = time.time() - check_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-檢測到彈窗(模式2)",
|
|
匹配到的關鍵詞=matched,
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
OCR耗時秒=f"{(ocr_mode0_elapsed + ocr_mode1_elapsed + ocr_mode2_elapsed):.3f}",
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
ocr_mode2_elapsed = time.time() - ocr_mode2_start
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-OCR(模式2)失敗",
|
|
error=str(e),
|
|
耗時秒=f"{ocr_mode2_elapsed:.3f}",
|
|
)
|
|
|
|
combined_text = " ".join(texts).strip()
|
|
final_keywords = ("購物紅包", "邀你搶", "搶紅包")
|
|
result = any(kw in combined_text for kw in final_keywords)
|
|
if not result and has_popup_pixel:
|
|
result = True
|
|
|
|
total_elapsed = time.time() - check_start
|
|
ocr_total = ocr_mode0_elapsed + ocr_mode1_elapsed + ocr_mode2_elapsed
|
|
if result:
|
|
matched = [kw for kw in final_keywords if kw in combined_text]
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-檢測到彈窗(合併檢查)",
|
|
匹配到的關鍵詞=matched,
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
OCR耗時秒=f"{ocr_total:.3f}",
|
|
)
|
|
else:
|
|
analyser._log_kv(
|
|
"彈窗檢測步驟",
|
|
"購物紅包-未檢測到彈窗",
|
|
合併文本=combined_text[:100] if combined_text else "空",
|
|
總耗時秒=f"{total_elapsed:.3f}",
|
|
OCR耗時秒=f"{ocr_total:.3f}",
|
|
)
|
|
return result
|