Browse Source

feat: Add test

huangrunheng 2 years ago
parent
commit
6553ad6d93

+ 8 - 1
tasks/Component/RightActivity/assets.py

@@ -8,5 +8,12 @@ from module.atom.list import RuleList
 # This file was automatically generated by ./dev_tools/assets_extract.py.
 # Don't modify it manually.
 class RightActivityAssets: 
-	pass
+
+
+	# Image Rule Assets
+	# description 
+	I_RA_OPEN = RuleImage(roi_front=(1257,78,22,58), roi_back=(1257,78,22,58), threshold=0.8, method="Template matching", file="./tasks/Component/RightActivity/ra/ra_ra_open.png")
+	# description 
+	I_RA_CLOSE = RuleImage(roi_front=(1256,123,23,44), roi_back=(1256,123,23,44), threshold=0.8, method="Template matching", file="./tasks/Component/RightActivity/ra/ra_ra_close.png")
+
 

+ 20 - 0
tasks/Component/RightActivity/ra/image.json

@@ -0,0 +1,20 @@
+[
+  {
+    "itemName": "ra_open",
+    "imageName": "ra_ra_open.png",
+    "roiFront": "1257,78,22,58",
+    "roiBack": "1257,78,22,58",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "ra_close",
+    "imageName": "ra_ra_close.png",
+    "roiFront": "1256,123,23,44",
+    "roiBack": "1256,123,23,44",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  }
+]

BIN
tasks/Component/RightActivity/ra/ra_ra_close.png


BIN
tasks/Component/RightActivity/ra/ra_ra_open.png


+ 6 - 1
tasks/Component/RightActivity/right_activity.py

@@ -7,14 +7,19 @@ from module.logger import logger
 from tasks.base_task import BaseTask
 from tasks.GameUi.game_ui import GameUi
 from tasks.GameUi.page import page_main
+from tasks.Component.RightActivity.assets import RightActivityAssets
 
 
-class RightActivity(GameUi):
+class RightActivity(GameUi, RightActivityAssets):
     def enter(self, target: RuleImage):
         self.ui_get_current_page()
         self.ui_goto(page_main)
         self.ui_click_until_disappear(target, interval=3.5)
 
+    def right_open(self):
+        self.ui_click(self.I_RA_CLOSE, self.I_RA_OPEN, interval=2)
 
+    def right_close(self):
+        self.ui_click(self.I_RA_OPEN, self.I_RA_CLOSE, interval=2)
 
 

+ 3 - 0
tasks/Hyakkiyakou/.gitignore

@@ -0,0 +1,3 @@
+
+/utils/exp_gen.py
+/temp

+ 151 - 0
tasks/Hyakkiyakou/utils/exp_test.py

@@ -0,0 +1,151 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import cv2
+
+from datetime import datetime
+from pathlib import Path
+from cached_property import cached_property
+
+from module.logger import logger
+from module.base.timer import Timer
+
+from tasks.Exploration.assets import ExplorationAssets
+from tasks.GameUi.game_ui import GameUi
+from tasks.GameUi.page import page_main, page_town
+from tasks.base_task import BaseTask
+from tasks.Script.config_device import ScreenshotMethod, ControlMethod
+from tasks.Component.RightActivity.right_activity import RightActivity
+from tasks.Restart.assets import RestartAssets
+
+from utils import usage_time
+from fast_device import FastDevice
+
+
+class Step:
+    def __init__(self,
+                 step_name: str,
+                 pos: tuple,
+                 time: float,
+                 check=None,
+                 save_img: bool = False,
+                 save_interval: float = 0.1,
+                 save_time: float = None,
+                 save_max: int = None,
+                 save_roi: tuple = (0, 0),
+                 info: dict = None,
+                 ) -> None:
+        self.step_name = step_name
+        self.pos = pos
+        self.time = time
+        self.check = check
+        self.save_img = save_img
+        self.save_interval = save_interval
+        self.save_time = save_time if save_time else time
+        self.save_max = save_max
+        self.save_roi = save_roi
+        self.info = info
+        x_start, y_start = self.save_roi
+        if x_start > 640 or y_start > 80:
+            raise ValueError(f'Invalid roi: {self.save_roi}')
+
+    def __str__(self):
+        return f'{self.step_name}'
+
+    def __repr__(self):
+        return f'{self.step_name}'
+
+
+# -----------------------------------------------------------------------
+
+
+
+# -----------------------------------------------------------------------
+
+class ExpTest(RightActivity, FastDevice, RestartAssets, ExplorationAssets):
+
+    @cached_property
+    def save_path(self) -> Path:
+        save_path = Path(f'./tasks/Hyakkiyakou/temp/{self.name}')
+        save_path.mkdir(parents=True, exist_ok=True)
+        return save_path
+
+    def init(self, name: str):
+        self.name = name
+        self.config.model.script.device.screenshot_method = ScreenshotMethod.WINDOW_BACKGROUND
+        self.config.model.script.device.control_method = ControlMethod.WINDOW_MESSAGE
+
+    def _exit(self):
+        self.ui_get_current_page()
+        self.ui_goto(page_town)
+        self.right_close()
+        self.ui_click(self.I_LOGIN_SCROOLL_OPEN, self.I_LOGIN_SCROOLL_CLOSE, interval=1)
+
+    def _go(self):
+        pass
+
+    def run_step(self, step: Step):
+        logger.hr(f'Running step: {step}', 1)
+        cache_image: dict = {}
+        timer_step = Timer(step.time)
+        timer_save_img = Timer(step.save_time)
+        timer_save_interval = Timer(step.save_interval)
+        save_image_count = 0
+        start_flag = False
+        while 1:
+            with usage_time('screenshot'):
+                self.fast_screenshot()
+
+            if not start_flag:
+                with usage_time('click'):
+                    self.device.click(x=step.pos[0], y=step.pos[1])
+                timer_step.start()
+                timer_save_img.start()
+                timer_save_interval.start()
+                start_flag = True
+            if not start_flag:
+                continue
+            if timer_step.reached():
+                break
+            if not step.save_img:
+                continue
+            if timer_save_img.reached():
+                continue
+            if step.save_max and save_image_count >= step.save_max:
+                continue
+            if timer_save_interval.reached():
+                timer_save_interval.reset()
+                save_image_count += 1
+                save_name = f't@{self.name}@{int(datetime.now().timestamp() * 1000)}_{save_image_count:03d}.png'
+                x_start, y_start = step.save_roi
+                img = self.device.image[y_start:y_start + 640, x_start:x_start + 640]
+                cache_image[save_name] = img
+                logger.info(f'Save image: {save_name}')
+                # cv2.imwrite(str(self.save_path / save_name), img)
+        if cache_image:
+            logger.info(f'Save image: {cache_image.keys()}')
+            for key, value in cache_image.items():
+                cv2.imwrite(str(self.save_path / key), value)
+
+    def run_town(self):
+        step_a = (641, 474)
+        step_b = (1165, 481)
+        step_c = (641, 556)
+        step_d = (1231, 567)
+        step_save_roi = (640, 80)
+        step_1 = Step('step_1', pos=step_b, time=2)
+        step_2 = Step('step_2', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
+        step_3 = Step('step_3', pos=step_b, time=2)
+        step_4 = Step('step_4', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
+        step_5 = Step('step_5', pos=step_d, time=2)
+        step_6 = Step('step_6', pos=step_a, time=2, save_img=True, save_roi=step_save_roi)
+        step_7 = Step('step_7', pos=step_d, time=2)
+        step_8 = Step('step_8', pos=step_c, time=2, save_img=True, save_roi=step_save_roi)
+        self.run_step(step_1)
+        self.run_step(step_2)
+        self.run_step(step_3)
+        self.run_step(step_4)
+        self.run_step(step_5)
+        self.run_step(step_6)
+        self.run_step(step_7)
+        self.run_step(step_8)

+ 15 - 0
tasks/Hyakkiyakou/utils/fast_device.py

@@ -0,0 +1,15 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+
+from tasks.base_task import BaseTask
+from tasks.Script.config_device import ScreenshotMethod, ControlMethod
+
+class FastDevice(BaseTask):
+
+    def fast_screenshot(self):
+        if self.config.model.script.device.screenshot_method != ScreenshotMethod.WINDOW_BACKGROUND:
+            raise
+        self.device.image = self.device.screenshot_window_background()
+        return self.device.image

+ 0 - 0
tasks/Hyakkiyakou/generate.py → tasks/Hyakkiyakou/utils/generate.py


+ 28 - 0
tasks/Hyakkiyakou/utils/utils.py

@@ -0,0 +1,28 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+
+
+from datetime import datetime
+
+from module.logger import logger
+
+
+class usage_time:
+    def __init__(self, name: str = None):
+        self.name = 'Timer' if name is None else name
+
+    def __enter__(self):
+        self.start_time = datetime.now()
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.end_time = datetime.now()
+        execution_time = self.end_time - self.start_time
+        logger.info(f'{str(self.name)}: {execution_time.total_seconds() * 1000 }ms')
+
+
+if __name__ == '__main__':
+    with usage_time('test'):
+        print('test1')
+    print('end')

+ 23 - 11
tasks/Hyakkiyakou/utils/video2images.py

@@ -43,13 +43,20 @@ class TransformVideo(HyakkiyakouAssets):
         else:
             self.parse_one(video, save_path)
 
-    def parse_one(self, video: Path, save_path: Path):
+    def parse_one(self, video: Path, save_path: Path = None):
         """
         单次解析一个视频
         @param video:
         @param save_path:
         @return:
         """
+        if isinstance(video, str):
+            video = Path(video)
+        if not save_path:
+            save_path = video.parent / f'images_{video.stem}'
+        if isinstance(save_path, str):
+            save_path = Path(save_path)
+        save_path.mkdir(parents=True, exist_ok=True)
         # 获取时间戳的str形式,用来命名图片
         datetime_now = datetime.now().strftime('%Y%m%dT%H%M%S')
         logger.info(f'Start transform video {video}')
@@ -67,7 +74,11 @@ class TransformVideo(HyakkiyakouAssets):
             if not ret:
                 break
             w, h, _ = frame.shape
-            if w != 1280 and h != 720:
+            if h == 720 and w == 1280:
+                pass
+            elif h == 720 and w != 1280:
+                frame = frame[:, 0:1280]
+            elif w != 1280 and h != 720:
                 frame = cv2.resize(frame, (1280, 720))
             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
             # 抽帧
@@ -75,22 +86,23 @@ class TransformVideo(HyakkiyakouAssets):
             if count_frame % frame_skip != 0:
                 continue
 
-            if not self.I_CHECK_RUN.match(frame, threshold=0.6):
-                continue
+            # 检查是否是百鬼夜行中
+            # if not self.I_CHECK_RUN.match(frame, threshold=0.5):
+            #     continue
             if not self.change_channel:
                 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
             index_frame += 1
             cv2.imwrite(str(save_path / f'{datetime_now}_{index_frame:05d}.png'), frame)
-            cv2.imshow('frame', frame)
-            cv2.waitKey(100)
-            cv2.destroyAllWindows()
+            # cv2.imshow('frame', frame)
+            # cv2.waitKey(100)
+            # cv2.destroyAllWindows()
 
         cap.release()
         logger.info(f'{video} done')
 
 
 if __name__ == '__main__':
-    VIDEO = 'D:/Project/Hyakkiyakou/OnmyojiAutoScript-hyakkiyakou/temp/sources_video/001-砸百鬼靠技术?在绝对的运气面前,所有技术.mp4'
-    SAVE_PATH = 'D:/Project/Hyakkiyakou/OnmyojiAutoScript-hyakkiyakou/temp/sources_images'
-    t = TransformVideo()
-    t.run(VIDEO, SAVE_PATH)
+    VIDEO = 'F:/videos/444592487.mp4'
+    # SAVE_PATH = 'D:/Project/Hyakkiyakou/OnmyojiAutoScript-hyakkiyakou/temp/sources_images'
+    t = TransformVideo(interval=0.1)
+    t.parse_one(VIDEO)

+ 4 - 0
tasks/base_task.py

@@ -40,6 +40,10 @@ class BaseTask(GlobalGameAssets, CostumeBase):
     current_count: int = None  # 当前运行的次数
 
     def __init__(self, config: Config, device: Device) -> None:
+        """
+
+        :rtype: object
+        """
         self.config = config
         self.device = device