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

Revert "revert(GeneralInvite): restore partial nickname matching"

This reverts commit 558ef4f871c2bf17e62ea6f089cd26241ad1fbb9.
neptine 2 месяцев назад
Родитель
Сommit
b360b1977b

+ 2 - 3
module/atom/ocr.py

@@ -28,10 +28,10 @@ class RuleOcr(Digit, DigitCounter, Duration, Single, Full, Quantity):
             case OcrMode.QUANTITY: return Quantity.after_process(self, result)
             case _: return result
 
-    def ocr(self, image, keyword=None):
+    def ocr(self, image, keyword=None, exact: bool=False):
 
         match self.mode:
-            case OcrMode.FULL: return Full.ocr_full(self, image, keyword)
+            case OcrMode.FULL: return Full.ocr_full(self, image, keyword, exact=exact)
             case OcrMode.SINGLE: return Single.ocr_single(self, image)
             case OcrMode.DIGIT: return Digit.ocr_digit(self, image)
             case OcrMode.DIGITCOUNTER: return DigitCounter.ocr_digit_counter(self, image)
@@ -88,4 +88,3 @@ if __name__ == "__main__":
     )
 
     print(rule.ocr_quantity(image))
-

+ 6 - 2
module/ocr/base_ocr.py

@@ -215,11 +215,12 @@ class BaseCor:
         else:
             return self.keyword == result
 
-    def filter(self, boxed_results: list[BoxedResult], keyword: str=None) -> list or None:
+    def filter(self, boxed_results: list[BoxedResult], keyword: str=None, exact: bool=False) -> list or None:
         """
         使用ocr获取结果后和keyword进行匹配. 返回匹配的index list
         :param keyword: 如果不指定默认适用对象的keyword
         :param boxed_results:
+        :param exact: 是否只匹配完整的单个 OCR 文本
         :return:
         """
         # 首先先将所有的ocr的str顺序拼接起来, 然后再进行匹配
@@ -228,7 +229,10 @@ class BaseCor:
         concatenated_string = "".join(strings)
         if keyword is None:
             keyword = self.keyword
-        if keyword in concatenated_string:
+        if exact:
+            result = [index for index, word in enumerate(strings) if word == keyword]
+            return result or None
+        elif keyword in concatenated_string:
             result = [index for index, word in enumerate(strings) if keyword in word]
         else:
             result = None

+ 3 - 3
module/ocr/sub_ocr.py

@@ -23,11 +23,12 @@ class Full(BaseCor):
     def after_process(self, result):
         return result
 
-    def ocr_full(self, image, keyword: str=None) -> tuple:
+    def ocr_full(self, image, keyword: str=None, exact: bool=False) -> tuple:
         """
         检测整个图片的文本,并对结果进行过滤。返回的是匹配到的keyword的左边。如果没有匹配到返回(0, 0, 0, 0)
         :param image:
         :param keyword:
+        :param exact: 是否只匹配完整的单个 OCR 文本
         :return:
         """
         if keyword is None:
@@ -37,7 +38,7 @@ class Full(BaseCor):
         if not boxed_results:
             return 0, 0, 0, 0
 
-        index_list = self.filter(boxed_results, keyword)
+        index_list = self.filter(boxed_results, keyword, exact=exact)
         logger.info(f"OCR [{self.name}] detected in {index_list}")
         # 如果一个都没有匹配到
         if not index_list:
@@ -258,4 +259,3 @@ class Quantity(BaseCor):
 if __name__ == '__main__':
     import cv2
     image = cv2.imread(r'E:\Project\OnmyojiAutoScript-assets\jade.png')
-

+ 4 - 4
tasks/BondlingFairyland/general_invite.py

@@ -303,8 +303,8 @@ class GeneralInvite(BaseTask, BondlingFairylandAssets, GeneralInviteAssets):
         self.screenshot()
         self.O_FRIEND_NAME_1.keyword = name
         self.O_FRIEND_NAME_2.keyword = name
-        appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2)
-        appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2)
+        appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2, exact=True)
+        appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2, exact=True)
         if not appear_1 and not appear_2:
             logger.info('Current page no friend')
             return False
@@ -313,8 +313,8 @@ class GeneralInvite(BaseTask, BondlingFairylandAssets, GeneralInviteAssets):
             self.screenshot()
             if self.appear(self.I_SELECTED):
                 break
-            appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2)
-            appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2)
+            appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2, exact=True)
+            appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2, exact=True)
 
         return True
 

+ 4 - 5
tasks/Component/GeneralInvite/general_invite.py

@@ -316,8 +316,8 @@ class GeneralInvite(BaseTask, GeneralInviteAssets):
         self.screenshot()
         self.O_FRIEND_NAME_1.keyword = name
         self.O_FRIEND_NAME_2.keyword = name
-        appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2)
-        appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2)
+        appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2, exact=True)
+        appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2, exact=True)
         if not appear_1 and not appear_2:
             logger.info('Current page no friend')
             return False
@@ -326,8 +326,8 @@ class GeneralInvite(BaseTask, GeneralInviteAssets):
             self.screenshot()
             if self.appear(self.I_SELECTED):
                 break
-            appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2)
-            appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2)
+            appear_1 = self.ocr_appear_click(self.O_FRIEND_NAME_1, interval=2, exact=True)
+            appear_2 = self.ocr_appear_click(self.O_FRIEND_NAME_2, interval=2, exact=True)
 
         return True
 
@@ -652,4 +652,3 @@ if __name__ == '__main__':
     t.screenshot()
     print(t.appear(t.I_FIRE, threshold=0.8))
 
-

+ 8 - 5
tasks/base_task.py

@@ -513,11 +513,12 @@ class BaseTask(GlobalGameAssets, CostumeBase):
             return True
         return False
 
-    def ocr_appear(self, target: RuleOcr, interval: float = None) -> bool:
+    def ocr_appear(self, target: RuleOcr, interval: float = None, exact: bool = False) -> bool:
         """
         ocr识别目标
         :param interval:
         :param target:
+        :param exact: 是否只匹配完整的单个 OCR 文本
         :return: 如果target有keyword或者是keyword存在,返回是True,否则返回False
                  但是没有指定keyword,返回的是匹配到的值,具体取决于target的mode
         """
@@ -536,7 +537,7 @@ class BaseTask(GlobalGameAssets, CostumeBase):
             if not self.interval_timer[target.name].reached():
                 return None
 
-        result = target.ocr(self.device.image)
+        result = target.ocr(self.device.image, exact=exact)
         appear = False
 
         if not target.keyword or target.keyword == '':
@@ -562,16 +563,18 @@ class BaseTask(GlobalGameAssets, CostumeBase):
                          target: RuleOcr,
                          action: Union[RuleClick, RuleLongClick] = None,
                          interval: float = None,
-                         duration: float = None) -> bool:
+                         duration: float = None,
+                         exact: bool = False) -> bool:
         """
         ocr识别目标,如果目标存在,则触发动作
         :param target:
         :param action:
         :param interval:
         :param duration:
+        :param exact: 是否只匹配完整的单个 OCR 文本
         :return:
         """
-        appear = self.ocr_appear(target, interval)
+        appear = self.ocr_appear(target, interval, exact=exact)
 
         if not appear:
             return False
@@ -826,4 +829,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}')