Parcourir la source

feat(module): Add new rule for detecting gif appear

huangrunheng il y a 2 ans
Parent
commit
7a184b7d37

+ 47 - 0
dev_tools/get_images.py

@@ -0,0 +1,47 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import cv2
+import time
+from datetime import timedelta, datetime
+from cached_property import cached_property
+from random import choice
+from pathlib import Path
+
+
+from module.exception import TaskEnd
+from module.logger import logger
+from module.base.timer import Timer
+
+from tasks.Script.config_device import ScreenshotMethod, ControlMethod
+from tasks.base_task import BaseTask
+from tasks.Exploration.version import highlight
+
+class GetAnimation(BaseTask):
+
+    @cached_property
+    def save_folder(self) -> Path:
+        save_time = datetime.now().strftime('%Y%m%dT%H%M%S')
+        save_folder = Path(f'./log/temp/{save_time}')
+        save_folder.mkdir(parents=True, exist_ok=True)
+        return save_folder
+
+    def run_screenshot(self):
+        self.config.model.script.device.screenshot_method = ScreenshotMethod.WINDOW_BACKGROUND
+        run_timer = Timer(3)
+        sho_timer = Timer(0.1)
+        run_timer.start()
+        sho_timer.start()
+        save_images = {}
+        while 1:
+            if run_timer.reached():
+                break
+            if sho_timer.reached():
+                sho_timer.reset()
+                image = self.device.screenshot_window_background()
+                # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
+                image = highlight(image)
+                time_now1 = int(time.time() * 1000)
+                save_images[time_now1] = image
+        for time_now, image in save_images.items():
+            cv2.imwrite(str(self.save_folder / f'all{time_now}.png'), image)

+ 57 - 0
module/atom/gif.py

@@ -0,0 +1,57 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import numpy as np
+
+from module.atom.image import RuleImage
+
+
+
+class RuleGif:
+    # 大部分实现同RuleImage 的接口
+
+    @property
+    def name(self) -> str:
+        return self.appear_target.name
+
+    def __init__(self, targets: list[RuleImage]):
+        self.targets = targets
+        self.roi_front: list = [0, 0, 0, 0]
+        self.appear_target = targets[0]
+
+    def pre_process(self, image):
+        return image
+
+
+    def search(self, image, roi: list = None, threshold: float = None) -> tuple:
+        """
+
+        :param image:
+        :param roi:
+        :param threshold:
+        :return: bool
+        第一项是否为出现, 第二项为匹配的RuleImage
+        """
+        image = self.pre_process(image)
+        #
+        threshold = self.targets[0].threshold if threshold is None else threshold
+        roi = self.targets[0].roi_back if roi is None else roi
+        for target in self.targets:
+            target.roi_back = roi
+            if target.match(image, threshold):
+                self.roi_front = target.roi_front
+                self.appear_target = target
+                return True, target
+        return False, None
+
+    def match(self, image, threshold: float = None) -> bool:
+        return self.search(image, threshold=threshold)[0]
+
+
+    def coord(self) -> tuple:
+        x, y, w, h = self.roi_front
+        return x + np.random.randint(0, w), y + np.random.randint(0, h)
+
+    def front_center(self) -> tuple:
+        x, y, w, h = self.roi_front
+        return int(x + w//2), int(y + h//2)

+ 33 - 0
tasks/Exploration/assets.py

@@ -10,6 +10,39 @@ from module.atom.list import RuleList
 class ExplorationAssets: 
 
 
+	# Image Rule Assets
+	# description 
+	I_LIGHTTEST = RuleImage(roi_front=(550,288,85,82), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_lighttest.png")
+	# description 
+	I_LIGHT1 = RuleImage(roi_front=(498,297,86,87), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light1.png")
+	# description 
+	I_LIGHT2 = RuleImage(roi_front=(497,300,86,82), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light2.png")
+	# description 
+	I_LIGHT3 = RuleImage(roi_front=(495,301,85,77), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light3.png")
+	# description 
+	I_LIGHT4 = RuleImage(roi_front=(496,296,85,85), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light4.png")
+	# description 
+	I_LIGHT5 = RuleImage(roi_front=(494,296,82,86), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light5.png")
+	# description 
+	I_LIGHT6 = RuleImage(roi_front=(490,295,83,85), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light6.png")
+	# description 
+	I_LIGHT7 = RuleImage(roi_front=(486,295,84,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light7.png")
+	# description 
+	I_LIGHT8 = RuleImage(roi_front=(485,298,84,81), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light8.png")
+	# description 
+	I_LIGHT9 = RuleImage(roi_front=(484,295,85,84), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light9.png")
+	# description 
+	I_LIGHT10 = RuleImage(roi_front=(479,297,84,78), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light10.png")
+	# description 
+	I_LIGHT11 = RuleImage(roi_front=(479,296,83,82), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light11.png")
+	# description 
+	I_LIGHT12 = RuleImage(roi_front=(477,290,85,90), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light12.png")
+	# description 
+	I_LIGHT13 = RuleImage(roi_front=(478,293,82,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light13.png")
+	# description 
+	I_LIGHT14 = RuleImage(roi_front=(474,290,82,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light14.png")
+
+
 	# Click Rule Assets
 	# 点击设置按钮 
 	C_CLICK_SETTINGS = RuleClick(roi_front=(55,662,21,21), roi_back=(55,662,21,21), name="click_settings")

BIN
tasks/Exploration/highlight/highlight_light1.png


BIN
tasks/Exploration/highlight/highlight_light10.png


BIN
tasks/Exploration/highlight/highlight_light11.png


BIN
tasks/Exploration/highlight/highlight_light12.png


BIN
tasks/Exploration/highlight/highlight_light13.png


BIN
tasks/Exploration/highlight/highlight_light14.png


BIN
tasks/Exploration/highlight/highlight_light2.png


BIN
tasks/Exploration/highlight/highlight_light3.png


BIN
tasks/Exploration/highlight/highlight_light4.png


BIN
tasks/Exploration/highlight/highlight_light5.png


BIN
tasks/Exploration/highlight/highlight_light6.png


BIN
tasks/Exploration/highlight/highlight_light7.png


BIN
tasks/Exploration/highlight/highlight_light8.png


BIN
tasks/Exploration/highlight/highlight_light9.png


BIN
tasks/Exploration/highlight/highlight_lighttest.png


+ 137 - 0
tasks/Exploration/highlight/image.json

@@ -0,0 +1,137 @@
+[
+  {
+    "itemName": "lighttest",
+    "imageName": "highlight_lighttest.png",
+    "roiFront": "550,288,85,82",
+    "roiBack": "97,182,1130,409",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light1",
+    "imageName": "highlight_light1.png",
+    "roiFront": "498,297,86,87",
+    "roiBack": "97,182,1130,409",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light2",
+    "imageName": "highlight_light2.png",
+    "roiFront": "497,300,86,82",
+    "roiBack": "97,182,1130,409",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light3",
+    "imageName": "highlight_light3.png",
+    "roiFront": "495,301,85,77",
+    "roiBack": "97,182,1130,409",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light4",
+    "imageName": "highlight_light4.png",
+    "roiFront": "496,296,85,85",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light5",
+    "imageName": "highlight_light5.png",
+    "roiFront": "494,296,82,86",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light6",
+    "imageName": "highlight_light6.png",
+    "roiFront": "490,295,83,85",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light7",
+    "imageName": "highlight_light7.png",
+    "roiFront": "486,295,84,87",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light8",
+    "imageName": "highlight_light8.png",
+    "roiFront": "485,298,84,81",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light9",
+    "imageName": "highlight_light9.png",
+    "roiFront": "484,295,85,84",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light10",
+    "imageName": "highlight_light10.png",
+    "roiFront": "479,297,84,78",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light11",
+    "imageName": "highlight_light11.png",
+    "roiFront": "479,296,83,82",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light12",
+    "imageName": "highlight_light12.png",
+    "roiFront": "477,290,85,90",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light13",
+    "imageName": "highlight_light13.png",
+    "roiFront": "478,293,82,87",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  },
+  {
+    "itemName": "light14",
+    "imageName": "highlight_light14.png",
+    "roiFront": "474,290,82,87",
+    "roiBack": "0,0,100,100",
+    "method": "Template matching",
+    "threshold": 0.6,
+    "description": "description"
+  }
+]

+ 93 - 0
tasks/Exploration/version.py

@@ -0,0 +1,93 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import cv2
+import numpy as np
+
+from pathlib import Path
+from cached_property import cached_property
+
+from module.base.utils import color_similarity_2d, load_image, save_image
+from module.atom.gif import RuleGif
+from module.atom.image import RuleImage
+
+from tasks.base_task import BaseTask
+from tasks.Exploration.assets import ExplorationAssets
+
+
+
+class Version(BaseTask):
+    pass
+
+
+def apply_mask(image, mask):
+    image16 = image.astype(np.uint16)
+    mask16 = mask.astype(np.uint16)
+    mask16 = cv2.merge([mask16, mask16, mask16])
+    image16 = cv2.multiply(image16, mask16)
+    # cv2.multiply(image16, mask16, dst=image16)
+    image16 = cv2.convertScaleAbs(image16, alpha=1 / 255)
+    # cv2.convertScaleAbs(image16, alpha=1 / 255, dst=image16)
+    # Image.fromarray(image16.astype(np.uint8)).show()
+    return image16.astype(np.uint8)
+
+def highlight(image):
+    yuv = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
+    _, u, _ = cv2.split(yuv)
+    cv2.subtract(128, u, dst=u)
+    cv2.multiply(u, 8, dst=u)
+
+    color = color_similarity_2d(image, color=(255,255,255))
+    hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
+    _, _, v = cv2.split(hsv)
+    image = apply_mask(image, u)
+    image = apply_mask(image, color)
+    image = apply_mask(image, v)
+
+    cv2.convertScaleAbs(image, alpha=3, dst=image)
+    cv2.subtract((255, 255, 255, 0), image, dst=image)
+
+    # from PIL import Image
+    # Image.fromarray(image.astype(np.uint8)).show()
+    return image
+
+
+class HighlightGif(RuleGif):
+    def pre_process(self, image):
+        return highlight(image)
+
+class HighLight(BaseTask, ExplorationAssets):
+
+    @cached_property
+    def TEMPLATE_GIF(self) -> RuleGif:
+        return HighlightGif(
+            targets=[
+                self.I_LIGHT1, self.I_LIGHT2, self.I_LIGHT3, self.I_LIGHT4, self.I_LIGHT5,
+                self.I_LIGHT6, self.I_LIGHT7, self.I_LIGHT8, self.I_LIGHT9, self.I_LIGHT10,
+                self.I_LIGHT11, self.I_LIGHT12, self.I_LIGHT13, self.I_LIGHT14,
+            ],
+        )
+
+
+if __name__ == '__main__':
+    # image = load_image(r'C:\Users\萌萌哒\Desktop\屏幕截图 2024-08-17 175713.png')
+    # image = highlight(image)
+    # save_image(image, r'C:\Users\萌萌哒\Desktop\1345.png')
+    #
+    # IMAGE_FILE = r"C:\Users\萌萌哒\Desktop\屏幕截图 2024-08-17 175713.png"
+    # from tasks.Exploration.assets import ExplorationAssets
+    # targe = ExplorationAssets.I_LIGHTTEST
+    # print(targe.test_match(image))
+
+    from dev_tools.get_images import GetAnimation
+    from module.config.config import Config
+    from module.device.device import Device
+    c = Config('oas1')
+    d = Device(c)
+    t = HighLight(c, d)
+    t.screenshot()
+
+    t.screenshot()
+    t.appear_then_click(t.TEMPLATE_GIF)
+
+

+ 8 - 4
tasks/base_task.py

@@ -16,6 +16,7 @@ from module.atom.long_click import RuleLongClick
 from module.atom.swipe import RuleSwipe
 from module.atom.ocr import RuleOcr
 from module.atom.list import RuleList
+from module.atom.gif import RuleGif
 from module.ocr.base_ocr import OcrMode, OcrMethod
 from module.atom.animate import RuleAnimate
 from module.logger import logger
@@ -136,7 +137,10 @@ class BaseTask(GlobalGameAssets, CostumeBase):
 
         return self.device.image
 
-    def appear(self, target: RuleImage, interval: float = None, threshold: float = None):
+    def appear(self,
+               target: RuleImage | RuleGif,
+               interval: float = None,
+               threshold: float = None):
         """
 
         :param target: 匹配的目标可以是RuleImage, 也可以是RuleOcr
@@ -144,7 +148,7 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         :param threshold:
         :return:
         """
-        if not isinstance(target, RuleImage):
+        if not isinstance(target, RuleImage) and not isinstance(target, RuleGif):
             return False
 
         if interval:
@@ -164,7 +168,7 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         return appear
 
     def appear_then_click(self,
-                          target: RuleImage,
+                          target: RuleImage | RuleGif,
                           action: Union[RuleClick, RuleLongClick] = None,
                           interval: float = None,
                           threshold: float = None,
@@ -178,7 +182,7 @@ class BaseTask(GlobalGameAssets, CostumeBase):
         :param threshold:
         :return: True or False
         """
-        if not isinstance(target, RuleImage):
+        if not isinstance(target, RuleImage) and not isinstance(target, RuleGif):
             return False
 
         appear = self.appear(target, interval=interval, threshold=threshold)