From c79c50ffe4c20984139b3bcc675925f83818b8dd Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 18 Jan 2026 14:38:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B4=AD=E7=89=A9=E7=BA=A2=E5=8C=85=E8=83=8C=E6=99=AF=E7=BA=A2?= =?UTF-8?q?=E8=89=B2=E5=8D=A0=E6=AF=94=E9=98=88=E5=80=BC=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E7=BA=A2=E5=8C=85=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.json | 3 ++- douyin/detection/douyin_pixel.py | 36 ++++++++++++++++++++++++++++---- douyin/other/douyin_popups.py | 15 ++++++++++++- douyin/utils/douyin_config.py | 10 +++++++++ gui/gui_app.py | 25 ++++++++++++++++++++++ 5 files changed, 83 insertions(+), 6 deletions(-) diff --git a/config/config.json b/config/config.json index 1efa726..579bbad 100644 --- a/config/config.json +++ b/config/config.json @@ -222,5 +222,6 @@ "high_value_room_revisit_min_prize_value": 1000.0, "high_value_room_revisit_max_age_days": 1, "swipe_up_start_y": 1200, - "swipe_up_end_y": 600 + "swipe_up_end_y": 600, + "shopping_redpacket_red_ratio_threshold": 0.5 } diff --git a/douyin/detection/douyin_pixel.py b/douyin/detection/douyin_pixel.py index 2e3da41..7055585 100644 --- a/douyin/detection/douyin_pixel.py +++ b/douyin/detection/douyin_pixel.py @@ -173,7 +173,10 @@ def detect_coupon_popup_background( def detect_redpacket_popup_background( - pic_rgb: Image.Image, return_info: bool = False + pic_rgb: Image.Image, + return_info: bool = False, + red_ratio_threshold: float | None = None, + analyser: Any = None, ) -> bool | tuple[bool, dict]: """中央购物红包弹窗检测:检测中央大红包区域的红色占比。 @@ -189,6 +192,8 @@ def detect_redpacket_popup_background( 参数: pic_rgb: RGB格式的整屏截图 return_info: 是否返回详细信息(包含红色比例) + red_ratio_threshold: 红色占比阈值(默认0.15,若提供则使用) + analyser: analyser对象,用于读取配置阈值 返回: bool: 是否检测到购物红包背景(return_info=False时) @@ -251,7 +256,19 @@ def detect_redpacket_popup_background( red_ratio = red_pixels / total_pixels if total_pixels > 0 else 0 # 阈值:至少15%的像素满足红色特征 - result = bool(red_ratio > 0.15) + if red_ratio_threshold is not None: + threshold = float(red_ratio_threshold) + elif analyser is not None: + try: + threshold = float( + getattr(analyser, "shopping_redpacket_red_ratio_threshold", 0.15) + or 0.15 + ) + except Exception as e: # type: ignore[general-exception] + threshold = 0.15 + else: + threshold = 0.15 + result = bool(red_ratio > threshold) if return_info: reason_value: Optional[str] = ( @@ -260,7 +277,7 @@ def detect_redpacket_popup_background( return result, { "method": "pixel_detection", "red_ratio": float(red_ratio), - "effective_threshold": 0.15, + "effective_threshold": threshold, "reason": reason_value, } return result @@ -1030,6 +1047,7 @@ def read_pixel_stage_state_from_screenshot( coupon_threshold = None attend_button_threshold = None fansclub_threshold = None + redpacket_threshold = None live_room_pixel_threshold = None if analyser is not None: try: @@ -1050,6 +1068,13 @@ def read_pixel_stage_state_from_screenshot( ) except Exception as e: # type: ignore[general-exception] pass + try: + redpacket_threshold = float( + getattr(analyser, "shopping_redpacket_red_ratio_threshold", None) + or 0.15 + ) + except Exception as e: # type: ignore[general-exception] + pass try: live_room_pixel_threshold = float( getattr(analyser, "live_room_match_threshold", None) or 0.8 @@ -1082,7 +1107,10 @@ def read_pixel_stage_state_from_screenshot( analyser=analyser, ), "has_redpacket_bg": lambda pic: detect_redpacket_popup_background( - pic, return_info=True + pic, + return_info=True, + red_ratio_threshold=redpacket_threshold, + analyser=analyser, ), "has_lucky_draw_result": lambda pic: detect_lucky_draw_result_popup( pic, lucky_draw_red_ratio_threshold, return_info=True diff --git a/douyin/other/douyin_popups.py b/douyin/other/douyin_popups.py index f79efea..3b20be6 100644 --- a/douyin/other/douyin_popups.py +++ b/douyin/other/douyin_popups.py @@ -209,7 +209,20 @@ def check_shopping_redpacket_popup(analyser) -> bool: 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)) + try: + redpacket_threshold = float( + getattr(analyser, "shopping_redpacket_red_ratio_threshold", None) + or 0.15 + ) + except Exception: + redpacket_threshold = 0.15 + has_popup_pixel = bool( + douyin_pixel.detect_redpacket_popup_background( + pic, + red_ratio_threshold=redpacket_threshold, + analyser=analyser, + ) + ) pixel_elapsed = time.time() - pixel_check_start try: diff --git a/douyin/utils/douyin_config.py b/douyin/utils/douyin_config.py index 67344cd..c989828 100644 --- a/douyin/utils/douyin_config.py +++ b/douyin/utils/douyin_config.py @@ -160,6 +160,16 @@ class ConfigManager: threshold = float(config["fansclub_join_red_ratio_threshold"] or 0.25) analyser.fansclub_join_red_ratio_threshold = max(0.0, min(1.0, threshold)) + if "shopping_redpacket_red_ratio_threshold" in config: + threshold = float(config["shopping_redpacket_red_ratio_threshold"] or 0.15) + analyser.shopping_redpacket_red_ratio_threshold = max( + 0.0, min(1.0, threshold) + ) + else: + analyser.shopping_redpacket_red_ratio_threshold = getattr( + analyser, "shopping_redpacket_red_ratio_threshold", 0.15 + ) + # 粉丝团等待/点击延迟配置 if "fansclub_click_delay" in config: analyser.fansclub_click_delay = float(config["fansclub_click_delay"] or 0.6) diff --git a/gui/gui_app.py b/gui/gui_app.py index be98426..7d8ccee 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -666,6 +666,10 @@ def main(): fansclub_join_red_ratio_threshold_var = tk.DoubleVar( value=float(cfg.get("fansclub_join_red_ratio_threshold", 0.25) or 0.25) ) + # 购物红包背景红色占比阈值(0.0~1.0,默认0.15) + shopping_redpacket_red_ratio_threshold_var = tk.DoubleVar( + value=float(cfg.get("shopping_redpacket_red_ratio_threshold", 0.15) or 0.15) + ) # 调试图像开关 & 清理相关变量 debug_captures_enabled_var = tk.BooleanVar( @@ -1689,6 +1693,21 @@ def main(): side=LEFT, padx=(6, 0) ) + pixel_r3 = ttk.Frame(pixel_detect_box) + pixel_r3.pack(fill=X, padx=10, pady=(0, 10)) + ttk.Label(pixel_r3, text="购物红包背景红色占比阈值:").pack(side=LEFT) + ttk.Spinbox( + pixel_r3, + from_=0.0, + to=1.0, + increment=0.01, + textvariable=shopping_redpacket_red_ratio_threshold_var, + width=8, + ).pack(side=LEFT) + ttk.Label(pixel_r3, text="(默认0.15,用于检测购物红包弹窗背景)").pack( + side=LEFT, padx=(6, 0) + ) + # 标签页5:日志配置 logging_tab = ttk.Frame(left_notebook) left_notebook.add(logging_tab, text="日志配置") @@ -2063,6 +2082,9 @@ def main(): fansclub_join_red_ratio_threshold_var.set( float(cfg.get("fansclub_join_red_ratio_threshold", 0.25) or 0.25) ) + shopping_redpacket_red_ratio_threshold_var.set( + float(cfg.get("shopping_redpacket_red_ratio_threshold", 0.15) or 0.15) + ) # 粉丝团等待/点击延迟配置 fansclub_click_delay_var.set(float(cfg.get("fansclub_click_delay", 0.6) or 0.6)) fansclub_auto_wait_var.set( @@ -2350,6 +2372,9 @@ def main(): cfg2["fansclub_join_red_ratio_threshold"] = float( fansclub_join_red_ratio_threshold_var.get() or 0.25 ) + cfg2["shopping_redpacket_red_ratio_threshold"] = float( + shopping_redpacket_red_ratio_threshold_var.get() or 0.15 + ) # 粉丝团等待/点击延迟配置 cfg2["fansclub_click_delay"] = float(fansclub_click_delay_var.get() or 0.6) cfg2["fansclub_auto_join_wait_seconds"] = float(