Browse Source

Add RuleImageMallResourceMixin to get rule ocr from rule image for mall resource. Add seperate mall resource json

Migs 10 months ago
parent
commit
3d1b4372b0

+ 91 - 0
module/atom/RuleImageMallResourceMixin.py

@@ -0,0 +1,91 @@
+# module/atom/image/image_mall_resource.py
+
+from module.logger import logger
+from module.atom.ocr import RuleOcr
+
+
+class RuleImageMallResourceMixin:
+    """
+    RuleImage 的 mall_resource 能力扩展
+    """
+
+    # mall_resource 专用偏移参数
+    MALL_RESOURCE_X_OFFSET = 40
+    MALL_RESOURCE_RIGHT_EXTEND = 70
+
+    def match_and_get_roi(self, image, threshold=None):
+        """
+        - match 成功 → 返回 (x, y, w, h)
+        - match 失败 / 异常 → 返回 None
+        """
+        try:
+            ok = self.match(image, threshold)
+        except Exception as e:
+            logger.error(f"match exception: {e}")
+            return None
+
+        if not ok:
+            return None
+
+        try:
+            mat = self.image
+            if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
+                return None
+
+            h, w = mat.shape[:2]
+            x = int(self.roi_front[0])
+            y = int(self.roi_front[1])
+
+            return (x, y, int(w), int(h))
+        except Exception:
+            return None
+
+    def get_mall_resource_text_roi(self, image, threshold=None):
+        """
+        mall_resource 专用:
+        - 定位 icon
+        - 推导文本 OCR ROI
+
+        return: roi or None
+        """
+        try:
+            icon_roi = self.match_and_get_roi(image, threshold)
+            if icon_roi is None:
+                # 回退 roi_front
+                ix, iy, iw, ih = self.roi_front
+            else:
+                ix, iy, iw, ih = icon_roi
+
+            x = int(ix) + self.MALL_RESOURCE_X_OFFSET
+            y = int(iy)
+            w = int(iw) + self.MALL_RESOURCE_RIGHT_EXTEND
+            h = int(ih)
+
+            return (x, y, w, h)
+        except Exception:
+            return None
+
+    def build_mall_resource_ocr(self, image, threshold=None, name="mall_resource"):
+        """
+        mall_resource 专用快捷方法:
+        - 定位 icon
+        - 推导 OCR ROI
+        - 构造 RuleOcr
+
+        return: RuleOcr | None
+        """
+        try:
+            roi = self.get_mall_resource_text_roi(image, threshold)
+            if roi is None:
+                return None
+
+            return RuleOcr(
+                roi=roi,
+                area=roi,
+                mode="Quantity",
+                method="Default",
+                keyword="",
+                name=name
+            )
+        except Exception:
+            return None

+ 2 - 2
module/atom/image.py

@@ -7,12 +7,13 @@ import numpy as np
 from numpy import float32, int32, uint8, fromfile
 from pathlib import Path
 
+from atom.RuleImageMallResourceMixin import RuleImageMallResourceMixin
 from module.base.decorator import cached_property
 from module.logger import logger
 from module.base.utils import is_approx_rectangle
 
 
-class RuleImage:
+class RuleImage(RuleImageMallResourceMixin):
     debug_mode: bool = False
 
     def __init__(self, roi_front: tuple, roi_back: tuple, method: str, threshold: float, file: str) -> None:
@@ -343,7 +344,6 @@ class RuleImage:
                 return False
         return True
 
-
 if __name__ == "__main__":
     from dev_tools.assets_test import detect_image
 

+ 27 - 16
module/atom/ocr.py

@@ -54,21 +54,32 @@ class RuleOcr(Digit, DigitCounter, Duration, Single, Full, Quantity):
         y = np.random.randint(y, y + h)
         return x, y
 
-
-
 if __name__ == "__main__":
-    O_MALL_RESOURCE_1 = RuleOcr(roi=(144, 7, 100, 43), area=(144, 7, 100, 43), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_1")
-    O_MALL_RESOURCE_2 = RuleOcr(roi=(326, 8, 124, 39), area=(326, 8, 124, 39), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_2")
-    O_MALL_RESOURCE_3 = RuleOcr(roi=(533, 9, 107, 38), area=(533, 9, 107, 38), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_3")
-    O_MALL_RESOURCE_4 = RuleOcr(roi=(739, 8, 100, 39), area=(739, 8, 100, 39), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_4")
-    O_MALL_RESOURCE_5 = RuleOcr(roi=(935, 11, 100, 37), area=(935, 11, 100, 37), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_5")
-    O_MALL_RESOURCE_6 = RuleOcr(roi=(1129, 6, 100, 41), area=(1129, 6, 100, 41), mode="Quantity", method="Default",
-                                keyword="", name="mall_resource_6")
-    image = cv2.imread(r"E:\2025-01-16225353.png")
-    print(O_MALL_RESOURCE_5.ocr_quantity(image))
+    # O_MALL_RESOURCE_1 = RuleOcr(roi=(144, 7, 100, 43), area=(144, 7, 100, 43), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_1")
+    # O_MALL_RESOURCE_2 = RuleOcr(roi=(326, 8, 124, 39), area=(326, 8, 124, 39), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_2")
+    # O_MALL_RESOURCE_3 = RuleOcr(roi=(533, 9, 107, 38), area=(533, 9, 107, 38), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_3")
+    # O_MALL_RESOURCE_4 = RuleOcr(roi=(739, 8, 100, 39), area=(739, 8, 100, 39), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_4")
+    # O_MALL_RESOURCE_5 = RuleOcr(roi=(935, 11, 100, 37), area=(935, 11, 100, 37), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_5")
+    # O_MALL_RESOURCE_6 = RuleOcr(roi=(1129, 6, 100, 41), area=(1129, 6, 100, 41), mode="Quantity", method="Default",
+    #                             keyword="", name="mall_resource_6")
+    # image = cv2.imread(r"E:\2025-01-16225353.png")
+    # print(O_MALL_RESOURCE_5.ocr_quantity(image))
+    from module.atom.image import RuleImage
+    image = cv2.imread(r"E:\Debug\MuMu-20260119-114931-173.png")
+
+    I_SIDE_CHECK_SPECIAL = RuleImage(roi_front=(155,8,42,42), roi_back=(155,7,1115,90), threshold=0.7, method="Template matching", file="E:\Debug\\navbar_mall_sccales_check.png")
+
+    roi = I_SIDE_CHECK_SPECIAL.get_mall_resource_text_roi(image, threshold=0.7)
+    if roi is None:
+        print("failed")
+    else:
+        print("roi =", roi)
+        rule = RuleOcr(roi=roi,area=roi,mode="Quantity",method="Default",keyword="",name="mall_resource_1")
+        print(rule.ocr_quantity(image))
+
 

+ 13 - 0
tasks/RichMan/assets.py

@@ -173,6 +173,19 @@ class RichManAssets:
 	I_MALL_SUNDRY_CHECK = RuleImage(roi_front=(1093,10,31,44), roi_back=(1093,10,31,44), threshold=0.7, method="Template matching", file="./tasks/RichMan/mall/navbar/navbar_mall_sundry_check.png")
 
 
+	# Image Rule Assets
+	# 体力 
+	I_MALL_RESOURCE_AP = RuleImage(roi_front=(155,7,45,43), roi_back=(155,7,1115,90), threshold=0.7, method="Default", file="./tasks/RichMan/mall/navbar/navbar_mall_resource_ap.png")
+	# 荣誉 
+	I_MALL_RESOURCE_HONOR = RuleImage(roi_front=(722,11,41,42), roi_back=(155,7,1115,90), threshold=0.7, method="Default", file="./tasks/RichMan/mall/navbar/navbar_mall_resource_honor.png")
+	# 友情点 
+	I_MALL_RESOURCE_FRIENDS = RuleImage(roi_front=(908,8,39,43), roi_back=(155,7,1115,90), threshold=0.7, method="Default", file="./tasks/RichMan/mall/navbar/navbar_mall_resource_friends.png")
+	# 勋章 
+	I_MALL_RESOURCE_MEDAL = RuleImage(roi_front=(535,9,40,44), roi_back=(155,7,1115,90), threshold=0.7, method="Default", file="./tasks/RichMan/mall/navbar/navbar_mall_resource_medal.png")
+	# 魅力值 
+	I_MALL_RESOURCE_CHARISMA = RuleImage(roi_front=(906,5,48,46), roi_back=(155,7,1115,90), threshold=0.7, method="Default", file="./tasks/RichMan/mall/navbar/navbar_mall_resource_charisma.png")
+
+
 	# Image Rule Assets
 	# 特殊 
 	I_SIDE_SURE_SPECIAL = RuleImage(roi_front=(1172,91,70,74), roi_back=(1172,91,70,74), threshold=0.7, method="Template matching", file="./tasks/RichMan/mall/navbar/navbar_side_sure_special.png")

+ 47 - 0
tasks/RichMan/mall/navbar/image_mall_resource.json

@@ -0,0 +1,47 @@
+[
+  {
+    "itemName": "mall_resource_ap",
+    "imageName": "navbar_mall_resource_ap.png",
+    "roiFront": "155,7,45,43",
+    "roiBack": "155,7,1115,90",
+    "method": "Default",
+    "threshold": 0.7,
+    "description": "体力"
+  },
+  {
+    "itemName": "mall_resource_honor",
+    "imageName": "navbar_mall_resource_honor.png",
+    "roiFront": "722,11,41,42",
+    "roiBack": "155,7,1115,90",
+    "method": "Default",
+    "threshold": 0.7,
+    "description": "荣誉"
+  },
+  {
+    "itemName": "mall_resource_friends",
+    "imageName": "navbar_mall_resource_friends.png",
+    "roiFront": "908,8,39,43",
+    "roiBack": "155,7,1115,90",
+    "method": "Default",
+    "threshold": 0.7,
+    "description": "友情点"
+  },
+  {
+    "itemName": "mall_resource_medal",
+    "imageName": "navbar_mall_resource_medal.png",
+    "roiFront": "535,9,40,44",
+    "roiBack": "155,7,1115,90",
+    "method": "Default",
+    "threshold": 0.7,
+    "description": "勋章"
+  },
+  {
+    "itemName": "mall_resource_charisma",
+    "imageName": "navbar_mall_resource_charisma.png",
+    "roiFront": "906,5,48,46",
+    "roiBack": "155,7,1115,90",
+    "method": "Default",
+    "threshold": 0.7,
+    "description": "魅力值"
+  }
+]

BIN
tasks/RichMan/mall/navbar/navbar_mall_resource_ap.png


BIN
tasks/RichMan/mall/navbar/navbar_mall_resource_charisma.png


BIN
tasks/RichMan/mall/navbar/navbar_mall_resource_friends.png


BIN
tasks/RichMan/mall/navbar/navbar_mall_resource_honor.png


BIN
tasks/RichMan/mall/navbar/navbar_mall_resource_medal.png