Просмотр исходного кода

fix(CollectiveMissions): Fix select_mission loop guard and add brightness/saturation image matching #1620

Neptine 5 месяцев назад
Родитель
Сommit
b2b730f419

+ 53 - 2
module/atom/image.py

@@ -409,12 +409,63 @@ class RuleImage(RuleImageMallResourceMixin):
         """
         image = self.corp(image)
         average_color = cv2.mean(image)
-        # logger.info(f'{self.name} average_color: {average_color}')
+        if self.debug_mode:
+            logger.info(f'{self.name} average_color: {average_color}')
         for i in range(3):
             if abs(average_color[i] - color[i]) > bias:
                 return False
         return True
 
+    def match_brightness(
+            self, image: np.array,
+            threshold: float = 0.9,
+            roi: list = None,
+            gray: bool = False) -> bool:
+        source = self.corp(image, roi) if roi is not None else self.corp(image)
+        template = self.image
+
+        if len(source.shape) != 3:
+            raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
+        source_gray = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY)
+        source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
+
+        if len(template.shape) != 3:
+            raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
+        template_gray = cv2.cvtColor(template, cv2.COLOR_RGB2GRAY)
+        template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
+
+        source_value = float(source_gray.mean()) if gray else float(source_hsv[:, :, 2].mean())
+        template_value = float(template_gray.mean()) if gray else float(template_hsv[:, :, 2].mean())
+        score = 1.0 - abs(source_value - template_value) / 255.0
+        score = max(0.0, min(1.0, score))
+
+        if self.debug_mode:
+            logger.attr(self.name, f'brightness similarity {score:.5f}')
+            logger.info(f'Template value: {template_value}, Source value: {source_value}')
+        return score >= threshold
+
+    def match_saturation(self, image: np.array, threshold: float = 0.9, roi: list = None) -> bool:
+        source = self.corp(image, roi) if roi is not None else self.corp(image)
+        template = self.image
+
+        if len(source.shape) != 3:
+            raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
+        source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
+
+        if len(template.shape) != 3:
+            raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
+        template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
+
+        source_value = float(source_hsv[:, :, 1].mean())
+        template_value = float(template_hsv[:, :, 1].mean())
+        score = 1.0 - abs(source_value - template_value) / 255.0
+        score = max(0.0, min(1.0, score))
+
+        if self.debug_mode:
+            logger.attr(self.name, f'saturation similarity {score:.5f}')
+            logger.info(f'Template value: {template_value}, Source value: {source_value}')
+        return score >= threshold
+
 if __name__ == "__main__":
     from dev_tools.assets_test import detect_image
 
@@ -428,4 +479,4 @@ if __name__ == "__main__":
 
     detect_image(IMAGE_FILE, jade)
     detect_image(IMAGE_FILE, sign)
-    print(jade.roi_front)
+    print(jade.roi_front)

+ 1 - 1
tasks/CollectiveMissions/assets.py

@@ -41,7 +41,7 @@ class CollectiveMissionsAssets:
 	# 领取奖励 
 	I_CM_REWARDS = RuleImage(roi_front=(567,463,145,60), roi_back=(200,458,914,88), threshold=0.8, method="Template matching", file="./tasks/CollectiveMissions/cm/cm_cm_rewards.png")
 	# description 
-	I_CM_SWITCH = RuleImage(roi_front=(414,478,43,58), roi_back=(310,453,282,114), threshold=0.8, method="Template matching", file="./tasks/CollectiveMissions/cm/cm_cm_switch.png")
+	I_CM_SWITCH = RuleImage(roi_front=(414,478,43,58), roi_back=(401,470,72,77), threshold=0.8, method="Template matching", file="./tasks/CollectiveMissions/cm/cm_cm_switch.png")
 
 
 	# Ocr Rule Assets

+ 1 - 1
tasks/CollectiveMissions/cm/image.json

@@ -93,7 +93,7 @@
     "itemName": "cm_switch",
     "imageName": "cm_cm_switch.png",
     "roiFront": "414,478,43,58",
-    "roiBack": "310,453,282,114",
+    "roiBack": "401,470,72,77",
     "method": "Template matching",
     "threshold": 0.8,
     "description": "description"

+ 16 - 4
tasks/CollectiveMissions/script_task.py

@@ -90,7 +90,6 @@ class ScriptTask(GameUi, CollectiveMissionsAssets):
             if self.appear_then_click(self.I_UI_BACK_YELLOW, interval=1):
                 continue
 
-
         self.set_next_run(task='CollectiveMissions', success=True, finish=True)
         raise TaskEnd('CollectiveMissions')
 
@@ -286,19 +285,32 @@ class ScriptTask(GameUi, CollectiveMissionsAssets):
         :param friend_name: 要选择的好友名称
         :return: 成功选择返回True
         """
-
+        click_cnt = 0
         while 1:
             self.screenshot()
             # 识别当前任务
             missions = self.detect_one(self.O_CM_1, self.O_CM_2)
             logger.info(f"当前任务: {missions}")
             logger.info(f"目标任务: {missions_select}")
+
             if missions == missions_select:
                 logger.info(f"成功切换任务")
                 return True
-            else:
+            if click_cnt > 20:
+                logger.warning(f'click_cnt={click_cnt}')
+                return False
+            if not (self.I_CM_SWITCH.match_brightness(self.device.image) and
+                self.I_CM_SWITCH.match_brightness(self.device.image) and
+                self.I_CM_SWITCH.match_mean_color(self.device.image, color=(134, 107, 83))
+            ):
+                # 无法刷新
+                logger.warning(f'[SelectMission] cannot refresh, brightness/color mismatch at click_cnt={click_cnt}')
+                return False
+            if self.appear_then_click(self.I_CM_SWITCH, interval=2):
                 logger.info(f"尝试切换任务")
-                self.appear_then_click(self.I_CM_SWITCH, interval=1)
+                click_cnt += 1
+
+
 
     def _soul(self, index: int):
         """