Jelajahi Sumber

feat(KekkaiUtilize): 添加多尺度模板匹配支持皮肤按钮缩放识别

结界皮肤改变时,"育成"、"结界卡"、"防守"等按钮的显示大小会变化
原始模板匹配无法识别不同缩放比例的按钮图片
修改
1. 在 RuleImage 添加 match_multi_scale() 方法
   自动尝试 0.5~1.2 倍的缩放比例进行模板匹配
   根据最佳缩放比例动态调整 roi_front 尺寸,确保点击坐标正确
2. 在 base_task 添加多尺度识别方法
   appear_multi_scale() - 多尺度图片识别
   appear_then_click_multi_scale() - 多尺度识别并点击
3. 修改以下图片使用多尺度匹配:
   I_SHI_GROWN
   I_SHI_CARD
   I_SHI_DEFENSE
感觉结界皮肤可以删了(
测试了10多款皮肤都没问题(也可能我皮肤太少)
后边只用对鲤鱼旗适配就行
yuyuyuyuyu 9 bulan lalu
induk
melakukan
6009a6e164

+ 70 - 4
module/atom/image.py

@@ -154,8 +154,8 @@ class RuleImage(RuleImageMallResourceMixin):
         mat = self.image
 
         if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
-            logger.error(f"Template image is invalid: {mat.shape}") #检测模板尺寸,不合法则不进行匹配,避免两次截图画面完全相同造成模板不合法
-            return True  # 如果模板图像无效,直接返回 True
+            logger.error(f"Template image is invalid: {mat.shape}")
+            return False  # 模板无效,匹配失败
 
         res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
         min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)  # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
@@ -169,6 +169,73 @@ class RuleImage(RuleImageMallResourceMixin):
         else:
             return False
 
+    def match_multi_scale(self, image: np.array, threshold: float = None,
+                          scales: list = None) -> bool:
+        """
+        多尺度模板匹配,自动尝试多个缩放比例以适应图片大小的变化
+        :param image: 原始截图
+        :param threshold: 匹配阈值
+        :param scales: 缩放比例列表,默认 [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
+        :return: 匹配是否成功
+        """
+        if threshold is None:
+            threshold = self.threshold
+
+        if scales is None:
+            scales = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
+
+        source = self.corp(image)
+        mat = self.image
+
+        if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
+            logger.error(f"Template image is invalid: {mat.shape}")
+            return False  # 模板无效,匹配失败
+
+        best_score = 0
+        best_loc = None
+        best_scale = 1.0
+
+        for scale in scales:
+            try:
+                # 按比例缩放模板
+                scaled_w = int(mat.shape[1] * scale)
+                scaled_h = int(mat.shape[0] * scale)
+
+                # 跳过太大或太小的缩放
+                if scaled_w < 10 or scaled_h < 10:
+                    continue
+                if scaled_w > source.shape[1] * 1.5 or scaled_h > source.shape[0] * 1.5:
+                    continue
+
+                scaled_mat = cv2.resize(mat, (scaled_w, scaled_h))
+
+                res = cv2.matchTemplate(source, scaled_mat, cv2.TM_CCOEFF_NORMED)
+                _, max_val, _, max_loc = cv2.minMaxLoc(res)
+
+                if max_val > best_score:
+                    best_score = max_val
+                    best_loc = max_loc
+                    best_scale = scale
+            except Exception as e:
+                continue
+
+        if self.debug_mode:
+            logger.attr(self.name, f'best scale: {best_scale:.2f}, best score: {best_score:.5f}')
+
+        if best_score > threshold and best_loc is not None:
+            # 计算缩放后的模板尺寸
+            scaled_w = int(mat.shape[1] * best_scale)
+            scaled_h = int(mat.shape[0] * best_scale)
+
+            # 设置 roi_front 为匹配位置 + 缩放后的尺寸
+            self.roi_front[0] = best_loc[0] + self.roi_back[0]
+            self.roi_front[1] = best_loc[1] + self.roi_back[1]
+            self.roi_front[2] = scaled_w
+            self.roi_front[3] = scaled_h
+            return True
+        else:
+            return False
+
     def match_all(self, image: np.array, threshold: float = None, roi: list = None) -> list[tuple]:
         """
         区别于match,这个是返回所有的匹配结果
@@ -357,5 +424,4 @@ if __name__ == "__main__":
 
     detect_image(IMAGE_FILE, jade)
     detect_image(IMAGE_FILE, sign)
-    print(jade.roi_front)
-
+    print(jade.roi_front)

+ 3 - 3
tasks/KekkaiActivation/script_task.py

@@ -162,7 +162,7 @@ class ScriptTask(KU, KekkaiActivationAssets):
                 break
             if self.appear(self.I_A_AUTO_INVITE):
                 break
-            if self.appear_then_click(self.I_SHI_CARD, interval=1):
+            if self.appear_then_click_multi_scale(self.I_SHI_CARD, interval=1):
                 continue
         logger.info('Enter card page')
 
@@ -385,7 +385,7 @@ class ScriptTask(KU, KekkaiActivationAssets):
         while 1:
             self.screenshot()
 
-            if self.appear(self.I_REALM_SHIN) and self.appear(self.I_SHI_GROWN):
+            if self.appear(self.I_REALM_SHIN) and self.appear_multi_scale(self.I_SHI_GROWN):
                 self.screenshot()
                 if not self.appear(self.I_REALM_SHIN):
                     continue
@@ -418,4 +418,4 @@ if __name__ == "__main__":
 
     t = ScriptTask(c, d)
     t.run()
-    # t.run_activation(t.config.kekkai_activation.activation_config)
+    # t.run_activation(t.config.kekkai_activation.activation_config)

+ 9 - 9
tasks/KekkaiUtilize/assets.py

@@ -38,27 +38,27 @@ class KekkaiUtilizeAssets:
 
 	# Image Rule Assets
 	# 育成 
-	I_SHI_GROWN = RuleImage(roi_front=(595,291,35,38), roi_back=(530,254,181,198), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_grown.png")
+	I_SHI_GROWN = RuleImage(roi_front=(595,291,35,38), roi_back=(530,220,202,267), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_grown.png")
 	# 结界卡 
-	I_SHI_CARD = RuleImage(roi_front=(886,290,38,56), roi_back=(870,276,75,86), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_card.png")
+	I_SHI_CARD = RuleImage(roi_front=(891,300,31,55), roi_back=(823,210,252,297), threshold=0.6, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_card.png")
 	# description 
-	I_SHI_DEFENSE = RuleImage(roi_front=(294,298,49,56), roi_back=(215,207,208,221), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_defense.png")
+	I_SHI_DEFENSE = RuleImage(roi_front=(303,308,34,52), roi_back=(178,209,261,274), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_shi_defense.png")
 	# 收取经验(没有满的图) 
-	I_CARD_EXP = RuleImage(roi_front=(889,151,57,54), roi_back=(829,107,199,185), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_card_exp.png")
+	I_CARD_EXP = RuleImage(roi_front=(889,151,57,54), roi_back=(829,33,231,257), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_card_exp.png")
 	# 收取体力(没有满) 
-	I_BOX_AP = RuleImage(roi_front=(815,435,57,51), roi_back=(744,336,202,204), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_ap.png")
+	I_BOX_AP = RuleImage(roi_front=(815,435,57,51), roi_back=(744,336,395,216), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_ap.png")
 	# 收取盒子的经验(没有满) 
-	I_BOX_EXP = RuleImage(roi_front=(893,440,42,45), roi_back=(862,396,147,127), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp.png")
+	I_BOX_EXP = RuleImage(roi_front=(893,440,42,45), roi_back=(792,334,359,232), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp.png")
 	# 结界皮肤 
-	I_REALM_SHIN = RuleImage(roi_front=(175,460,54,58), roi_back=(22,380,393,308), threshold=0.9, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_realm_shin.png")
+	I_REALM_SHIN = RuleImage(roi_front=(175,460,54,58), roi_back=(22,359,393,328), threshold=0.9, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_realm_shin.png")
 	# 寄养别人的经验 
-	I_UTILIZE_EXP = RuleImage(roi_front=(583,144,58,43), roi_back=(531,112,156,123), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_utilize_exp.png")
+	I_UTILIZE_EXP = RuleImage(roi_front=(583,144,58,43), roi_back=(513,43,280,309), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_utilize_exp.png")
 	# 取出 
 	I_AP_EXTRACT = RuleImage(roi_front=(592,501,100,75), roi_back=(592,501,100,75), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_ap_extract.png")
 	# 提取 
 	I_EXP_EXTRACT = RuleImage(roi_front=(592,448,100,73), roi_back=(592,448,100,73), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_exp_extract.png")
 	# 盒子经验满 
-	I_BOX_EXP_MAX = RuleImage(roi_front=(889,430,61,64), roi_back=(829,372,228,186), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp_max.png")
+	I_BOX_EXP_MAX = RuleImage(roi_front=(889,430,61,64), roi_back=(796,326,405,330), threshold=0.7, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_box_exp_max.png")
 	# 种树活动关闭标识 
 	I_PLANT_TREE_CLOSE = RuleImage(roi_front=(777,91,36,34), roi_back=(711,52,169,125), threshold=0.8, method="Template matching", file="./tasks/KekkaiUtilize/realm/realm_plant_tree_close.png")
 

+ 12 - 12
tasks/KekkaiUtilize/realm/image.json

@@ -3,7 +3,7 @@
     "itemName": "shi_grown",
     "imageName": "realm_shi_grown.png",
     "roiFront": "595,291,35,38",
-    "roiBack": "530,254,181,198",
+    "roiBack": "530,220,202,267",
     "method": "Template matching",
     "threshold": 0.6,
     "description": "育成"
@@ -11,8 +11,8 @@
   {
     "itemName": "shi_card",
     "imageName": "realm_shi_card.png",
-    "roiFront": "886,290,38,56",
-    "roiBack": "870,276,75,86",
+    "roiFront": "891,300,31,55",
+    "roiBack": "823,210,252,297",
     "method": "Template matching",
     "threshold": 0.6,
     "description": "结界卡"
@@ -20,17 +20,17 @@
   {
     "itemName": "shi_defense",
     "imageName": "realm_shi_defense.png",
-    "roiFront": "294,298,49,56",
-    "roiBack": "215,207,208,221",
+    "roiFront": "303,308,34,52",
+    "roiBack": "178,209,261,274",
     "method": "Template matching",
-    "threshold": 0.8,
+    "threshold": 0.7,
     "description": "description"
   },
   {
     "itemName": "card_exp",
     "imageName": "realm_card_exp.png",
     "roiFront": "889,151,57,54",
-    "roiBack": "829,107,199,185",
+    "roiBack": "829,33,231,257",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "收取经验(没有满的图)"
@@ -39,7 +39,7 @@
     "itemName": "box_ap",
     "imageName": "realm_box_ap.png",
     "roiFront": "815,435,57,51",
-    "roiBack": "744,336,202,204",
+    "roiBack": "744,336,395,216",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "收取体力(没有满)"
@@ -48,7 +48,7 @@
     "itemName": "box_exp",
     "imageName": "realm_box_exp.png",
     "roiFront": "893,440,42,45",
-    "roiBack": "862,396,147,127",
+    "roiBack": "792,334,359,232",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "收取盒子的经验(没有满)"
@@ -57,7 +57,7 @@
     "itemName": "realm_shin",
     "imageName": "realm_realm_shin.png",
     "roiFront": "175,460,54,58",
-    "roiBack": "22,380,393,308",
+    "roiBack": "22,359,393,328",
     "method": "Template matching",
     "threshold": 0.9,
     "description": "结界皮肤"
@@ -66,7 +66,7 @@
     "itemName": "utilize_exp",
     "imageName": "realm_utilize_exp.png",
     "roiFront": "583,144,58,43",
-    "roiBack": "531,112,156,123",
+    "roiBack": "513,43,280,309",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "寄养别人的经验"
@@ -93,7 +93,7 @@
     "itemName": "box_exp_max",
     "imageName": "realm_box_exp_max.png",
     "roiFront": "889,430,61,64",
-    "roiBack": "829,372,228,186",
+    "roiBack": "796,326,405,330",
     "method": "Template matching",
     "threshold": 0.7,
     "description": "盒子经验满"

TEMPAT SAMPAH
tasks/KekkaiUtilize/realm/realm_shi_card.png


TEMPAT SAMPAH
tasks/KekkaiUtilize/realm/realm_shi_defense.png


+ 67 - 0
tasks/KekkaiUtilize/realm_test.py

@@ -0,0 +1,67 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+
+from module.logger import logger
+from module.base.timer import Timer
+
+from tasks.KekkaiActivation.script_task import ScriptTask as KekkaiActivationScriptTask
+
+class ScriptTask(KekkaiActivationScriptTask):
+
+    def run(self):
+        """
+        测试多尺度匹配在默认鲤鱼旗皮肤下的识别效果
+        验证同一张图片能否识别不同缩放大小的按钮
+        """
+        logger.hr('Multi-Scale Matching Test - Default Skin')
+        logger.info('Testing if one image can recognize scaled versions of 育成 button')
+
+        images_count = [[self.I_SHI_GROWN,     0, 0],  # 式神育成
+                        [self.I_SHI_CARD,      0, 0],  # 结界卡
+                        [self.I_SHI_DEFENSE,   0, 0],  # 结界防守
+                        [self.I_REALM_SHIN,    0, 0],  # 结界皮肤
+                        [self.I_CARD_EXP,      0, 0],  # 收取经验
+                        [self.I_BOX_AP,        0, 0],  # 收取体力
+                        [self.I_BOX_EXP,       0, 0],  # 收取盒子经验
+                        [self.I_BOX_EXP_MAX,   0, 0],  # 收取盒子经验(满)
+                        [self.I_UTILIZE_EXP,   0, 0],  # 收取寄养经验
+                        ]
+
+        timer = Timer(10)
+        timer.start()
+        logger.info('Testing for 10 seconds...')
+        while 1:
+            self.screenshot()
+
+            for i in images_count:
+                # 普通匹配
+                if self.appear(i[0]):
+                    i[1] += 1
+                # 多尺度匹配
+                if self.appear_multi_scale(i[0]):
+                    i[2] += 1
+
+            if timer.reached():
+                break
+
+        print('\n' + '=' * 70)
+        print('%-20s   %8s   %8s' % ('Image', 'Normal', 'Multi-Scale'))
+        print('=' * 70)
+        for i in images_count:
+            if i[1] > 0 or i[2] > 0:  # 只显示有匹配结果的
+                print('%-20s %8d   %8d' % (str(i[0]), i[1], i[2]))
+        print('=' * 70)
+
+        logger.info('Test completed')
+
+
+if __name__ == '__main__':
+    from module.config.config import Config
+    from module.device.device import Device
+
+    c = Config('oas1')
+    d = Device(c)
+    t = ScriptTask(c, d)
+    t.run()

+ 5 - 5
tasks/KekkaiUtilize/script_task.py

@@ -129,7 +129,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
         while 1:
             self.screenshot()
 
-            if self.appear(self.I_REALM_SHIN) and self.appear(self.I_SHI_GROWN):
+            if self.appear(self.I_REALM_SHIN) and self.appear_multi_scale(self.I_SHI_GROWN):
                 self.screenshot()
                 if not self.appear(self.I_REALM_SHIN):
                     continue
@@ -196,7 +196,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             self.screenshot()
             if self.appear(self.I_REALM_SHIN):
                 break
-            if self.appear(self.I_SHI_DEFENSE):
+            if self.appear_multi_scale(self.I_SHI_DEFENSE):
                 break
             if self.appear_then_click(self.I_PLANT_TREE_CLOSE):
                 continue
@@ -334,7 +334,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             if self.in_shikigami_growth():
                 break
 
-            if self.appear_then_click(self.I_SHI_GROWN, interval=1):
+            if self.appear_then_click_multi_scale(self.I_SHI_GROWN, interval=1):
                 continue
         logger.info('Enter shikigami grown')
 
@@ -735,7 +735,7 @@ class ScriptTask(GameUi, ReplaceShikigami, KekkaiUtilizeAssets):
             self.screenshot()
             if self.appear(self.I_REALM_SHIN):
                 break
-            if self.appear(self.I_SHI_DEFENSE):
+            if self.appear_multi_scale(self.I_SHI_DEFENSE):
                 break
             if self.appear_then_click(self.I_UI_BACK_RED, interval=1):
                 continue
@@ -757,4 +757,4 @@ if __name__ == "__main__":
     # t.check_card_num('勾玉', 67)
     # t.screenshot()
     # print(t.appear(t.I_BOX_EXP, threshold=0.6))
-    # print(t.appear(t.I_BOX_EXP_MAX, threshold=0.6))
+    # print(t.appear(t.I_BOX_EXP_MAX, threshold=0.6))

+ 64 - 1
tasks/base_task.py

@@ -215,6 +215,69 @@ class BaseTask(GlobalGameAssets, CostumeBase):
 
         return appear
 
+    def appear_multi_scale(self,
+                           target: RuleImage,
+                           interval: float = None,
+                           threshold: float = None,
+                           scales: list = None):
+        """
+        多尺度图片识别,自动尝试多个缩放比例以适应图片大小的变化
+        :param target: RuleImage对象
+        :param interval: 匹配间隔时间
+        :param threshold: 匹配阈值
+        :param scales: 缩放比例列表,默认 [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
+        :return: interval时间到达且匹配成功则返回True, 否则False
+        """
+        if interval:
+            if target.name in self.interval_timer:
+                if self.interval_timer[target.name].limit != interval:
+                    self.interval_timer[target.name] = Timer(interval)
+            else:
+                self.interval_timer[target.name] = Timer(interval)
+            if not self.interval_timer[target.name].reached():
+                return False
+
+        appear = target.match_multi_scale(self.device.image, threshold=threshold, scales=scales)
+
+        if appear and interval:
+            self.interval_timer[target.name].reset()
+
+        return appear
+
+    def appear_then_click_multi_scale(self,
+                                      target: RuleImage,
+                                      action: Union[RuleClick, RuleLongClick] = None,
+                                      interval: float = None,
+                                      threshold: float = None,
+                                      scales: list = None,
+                                      duration: float = None):
+        """
+        多尺度图片识别并点击,自动尝试多个缩放比例以适应图片大小的变化
+        :param target: RuleImage对象
+        :param action: 点击位置,可以是RuleClick或RuleLongClick
+        :param interval: 匹配间隔时间
+        :param threshold: 匹配阈值
+        :param scales: 缩放比例列表
+        :param duration: 长按时间(毫秒)
+        :return: True or False
+        """
+        appear = self.appear_multi_scale(target, interval=interval, threshold=threshold, scales=scales)
+
+        if appear and not action:
+            x, y = target.coord()
+            self.device.click(x, y, control_name=target.name)
+        elif appear and action:
+            x, y = action.coord()
+            if isinstance(action, RuleLongClick):
+                if duration is None:
+                    self.device.long_click(x, y, duration=action.duration / 1000, control_name=target.name)
+                else:
+                    self.device.long_click(x, y, duration=duration / 1000, control_name=target.name)
+            elif isinstance(action, RuleClick):
+                self.device.click(x, y, control_name=target.name)
+
+        return appear
+
     def wait_until_appear(self,
                           target: RuleImage | RuleOcr,
                           skip_first_screenshot=False,
@@ -729,4 +792,4 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         logger.info(f'Push notify: {content}')
 
     def save_image(self, task_name=None, content=None, wait_time=2, image_type=False, push_flag=False, level=3):
-        logger.info(f'Save image: {task_name}')
+        logger.info(f'Save image: {task_name}')