Răsfoiți Sursa

feat(Hyakkiyakou): Release basic features

ryland 2 ani în urmă
părinte
comite
e52f70b27c

+ 9 - 3
tasks/Hyakkiyakou/agent/agent.py

@@ -13,6 +13,7 @@ from oashya.labels import CLASSINDEX as CI
 from oashya.labels import id2label, id2name
 from oashya.labels import id2label, id2name
 from module.logger import logger
 from module.logger import logger
 from tasks.Hyakkiyakou.agent.focus import Focus
 from tasks.Hyakkiyakou.agent.focus import Focus
+from tasks.Hyakkiyakou.debugger import Debugger
 
 
 
 
 def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
 def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
@@ -26,6 +27,8 @@ def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
 def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
 def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
     canvas_height, canvas_width = 720, 1280
     canvas_height, canvas_width = 720, 1280
     patch_height, patch_width = patch_size
     patch_height, patch_width = patch_size
+    patch_height = max(0, min(patch_height, canvas_height))
+    patch_width = max(0, min(patch_width, canvas_width))
 
 
     x1 = position[0] - patch_width // 2
     x1 = position[0] - patch_width // 2
     y1 = position[1] - patch_height // 2
     y1 = position[1] - patch_height // 2
@@ -93,7 +96,9 @@ class Agent:
                 case _ if CI.MIN_SR <= _class <= CI.MAX_SR: weight = weights[2]
                 case _ if CI.MIN_SR <= _class <= CI.MAX_SR: weight = weights[2]
                 case _ if CI.MIN_SSR <= _class <= CI.MAX_SSR: weight = 1.5 * weights[1]
                 case _ if CI.MIN_SSR <= _class <= CI.MAX_SSR: weight = 1.5 * weights[1]
                 case _ if CI.MIN_SP <= _class <= CI.MAX_SP: weight = 1.5 * weights[0]
                 case _ if CI.MIN_SP <= _class <= CI.MAX_SP: weight = 1.5 * weights[0]
-                case CI.BUFF_005: weight = -1.  # freeze
+                case CI.BUFF_005:  # freeze
+                    weight = -1.
+                    _cy += 100
                 case _: continue
                 case _: continue
             if weight == 0.:
             if weight == 0.:
                 continue
                 continue
@@ -113,7 +118,7 @@ class Agent:
                 max_variance = variance
                 max_variance = variance
         return Focus(inputs=tracks[max_index])
         return Focus(inputs=tracks[max_index])
 
 
-    def decision(self, tracks: list[tuple], state: list):
+    def decision(self, tracks: list[tuple], state: list) -> list:
         not_decision = [-1, -1, False, -1]
         not_decision = [-1, -1, False, -1]
         if not tracks:
         if not tracks:
             return not_decision
             return not_decision
@@ -147,5 +152,6 @@ class Agent:
         elif self.focus == focus:
         elif self.focus == focus:
             self.focus.update(focus)
             self.focus.update(focus)
             self.focus.set_omega(omega)
             self.focus.set_omega(omega)
-        # self.focus.show()
+        if Debugger.info_enable:
+            self.focus.show()
 
 

+ 11 - 5
tasks/Hyakkiyakou/agent/focus.py

@@ -70,13 +70,19 @@ velocity: {self._v}"""
     def decision(self, tracks, strategy, state) -> list:
     def decision(self, tracks, strategy, state) -> list:
 
 
         buff_omega, buff_cx, buff_cy, buff_v, buff_class = self.omega_buff(tracks, strategy['invite_friend'])
         buff_omega, buff_cx, buff_cy, buff_v, buff_class = self.omega_buff(tracks, strategy['invite_friend'])
-        self._omega_buff = buff_omega
-        buffed: bool = True if self._omega_buff > self._omega else False
-        omega = max(self._omega, self._omega_buff)
+        if self._omega > buff_omega:
+            buffed = False
+            target_x = self._cx + self._v * 100
+            target_y = self._cy - 40
+        else:
+            buffed = True
+            target_x = buff_cx + buff_v * 100
+            target_y = buff_cy - 40  # top left corner
+
         _r = self.r(vector=state, omega=self._omega, omega_buff=self._omega_buff)
         _r = self.r(vector=state, omega=self._omega, omega_buff=self._omega_buff)
         throw = True if _r > 0 else False
         throw = True if _r > 0 else False
         # x, y, throw, number
         # x, y, throw, number
-        return [0, 0, throw, 10]
+        return [target_x, target_y, throw, 10]
 
 
     def omega_buff(self, tracks, invite_friend: bool) -> float:
     def omega_buff(self, tracks, invite_friend: bool) -> float:
         max_omega = 0.
         max_omega = 0.
@@ -110,6 +116,6 @@ velocity: {self._v}"""
         upsilon = (vector[1] / 250 - vector[2] / 35)
         upsilon = (vector[1] / 250 - vector[2] / 35)
         upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
         upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
         result = _omega + tau + upsilon - 0.6
         result = _omega + tau + upsilon - 0.6
-        print(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f} | ")
+        logger.info(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f}")
         return result
         return result
 
 

+ 9 - 0
tasks/Hyakkiyakou/assets.py

@@ -44,6 +44,10 @@ class HyakkiyakouAssets:
 	I_HEND = RuleImage(roi_front=(81,164,86,299), roi_back=(81,164,86,299), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_hend.png")
 	I_HEND = RuleImage(roi_front=(81,164,86,299), roi_back=(81,164,86,299), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_hend.png")
 	# 冰冻 
 	# 冰冻 
 	I_HFREEZE = RuleImage(roi_front=(1092,665,187,54), roi_back=(1092,665,187,54), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_hfreeze.png")
 	I_HFREEZE = RuleImage(roi_front=(1092,665,187,54), roi_back=(1092,665,187,54), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_hfreeze.png")
+	# description 
+	I_HTITLE = RuleImage(roi_front=(578,21,134,48), roi_back=(578,21,134,48), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_htitle.png")
+	# description 
+	I_HCLOSE_RED = RuleImage(roi_front=(1056,177,54,54), roi_back=(1056,177,54,54), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_hclose_red.png")
 
 
 
 
 	# Image Rule Assets
 	# Image Rule Assets
@@ -140,6 +144,11 @@ class HyakkiyakouAssets:
 	I_FRIEND_REMOTE_2 = RuleImage(roi_front=(504,127,100,56), roi_back=(504,127,100,56), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_remote_2.png")
 	I_FRIEND_REMOTE_2 = RuleImage(roi_front=(504,127,100,56), roi_back=(504,127,100,56), threshold=0.8, method="Template matching", file="./tasks/Hyakkiyakou/hya/hya_friend_remote_2.png")
 
 
 
 
+	# Ocr Rule Assets
+	# 最坏的情况下来判断豆子的数量 
+	O_BEAN_NUMBER = RuleOcr(roi=(119,645,45,29), area=(119,645,45,29), mode="Digit", method="Default", keyword="", name="bean_number")
+
+
 	# Swipe Rule Assets
 	# Swipe Rule Assets
 	# description 
 	# description 
 	S_BEAN_05TO10 = RuleSwipe(roi_front=(390,648,21,22), roi_back=(565,646,21,23), mode="default", name="bean_05to10")
 	S_BEAN_05TO10 = RuleSwipe(roi_front=(390,648,21,22), roi_back=(565,646,21,23), mode="default", name="bean_05to10")

+ 3 - 1
tasks/Hyakkiyakou/config.py

@@ -48,12 +48,14 @@ class DebugConfig(ConfigBase):
     hya_info: bool = Field(default=False, description='hya_info_help')
     hya_info: bool = Field(default=False, description='hya_info_help')
     # 保存图片,拿去回喂给模型
     # 保存图片,拿去回喂给模型
     continuous_learning: bool = Field(default=False, description='continuous_learning_help')
     continuous_learning: bool = Field(default=False, description='continuous_learning_help')
+    # 单独的设定截屏间隔, 单位ms
+    screenshot_interval: float = Field(default=300, description='screenshot_interval_help')
 
 
 
 
 class Hyakkiyakou(ConfigBase):
 class Hyakkiyakou(ConfigBase):
     scheduler: Scheduler = Field(default_factory=Scheduler)
     scheduler: Scheduler = Field(default_factory=Scheduler)
     hyakkiyakou_config: HyakkiyakouConfig = Field(default_factory=HyakkiyakouConfig)
     hyakkiyakou_config: HyakkiyakouConfig = Field(default_factory=HyakkiyakouConfig)
-    hyakkiyakou__models: HyakkiyakouModels = Field(default_factory=HyakkiyakouModels)
+    hyakkiyakou_models: HyakkiyakouModels = Field(default_factory=HyakkiyakouModels)
     debug_config: DebugConfig = Field(default_factory=DebugConfig)
     debug_config: DebugConfig = Field(default_factory=DebugConfig)
 
 
 
 

+ 22 - 15
tasks/Hyakkiyakou/debugger.py

@@ -31,8 +31,8 @@ def test_track(show: bool = False):
         if file.suffix != '.png':
         if file.suffix != '.png':
             continue
             continue
         image = cv2.imdecode(fromfile(str(file), dtype=uint8), -1)
         image = cv2.imdecode(fromfile(str(file), dtype=uint8), -1)
-        # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
-        results = tracker(image=image)
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
+        results = tracker(image=image, response=[0, 0, False, 10])
         print(results)
         print(results)
         save_file = folder_save / f'{file.stem}.png'
         save_file = folder_save / f'{file.stem}.png'
         draw_image = draw_tracks(image, results)
         draw_image = draw_tracks(image, results)
@@ -48,47 +48,53 @@ def test_track(show: bool = False):
     print(f'平均时间: {(end_time - start_time) / len([folder_image.iterdir()])}')
     print(f'平均时间: {(end_time - start_time) / len([folder_image.iterdir()])}')
 
 
 
 
-glob_image = np.zeros((720, 1280, 3), dtype=np.uint8)
-
 def show_track(sync_image, sync_lock, stop_event):
 def show_track(sync_image, sync_lock, stop_event):
-    global glob_image
+    # global glob_image
     while True:
     while True:
         if stop_event.is_set():
         if stop_event.is_set():
             logger.info('OAS Track Debugger stopped')
             logger.info('OAS Track Debugger stopped')
             break
             break
         sync_lock.acquire()
         sync_lock.acquire()
-        cv2.imshow('OAS Tracker', glob_image)
+        if Debugger.sync_image is None:
+            logger.info('OAS Track Debugger not started')
+            time.sleep(1)
+            sync_lock.release()
+            continue
+        cv2.imshow('OAS Tracker', Debugger.sync_image)
         sync_lock.release()
         sync_lock.release()
         cv2.waitKey(100)
         cv2.waitKey(100)
 
 
 
 
 class Debugger:
 class Debugger:
-    sync_image = np.zeros((720, 1280, 3), dtype=np.uint8)
+    info_enable: bool = False
+    sync_image = None
     sync_lock = Lock()
     sync_lock = Lock()
     stop_event = Event()
     stop_event = Event()
     sync_thread = Thread(target=show_track, daemon=True, name='OAS Track Debugger',
     sync_thread = Thread(target=show_track, daemon=True, name='OAS Track Debugger',
                          args=(sync_image, sync_lock, stop_event))
                          args=(sync_image, sync_lock, stop_event))
     # sync_event = Event()  # reserved for future use
     # sync_event = Event()  # reserved for future use
 
 
+    def __init__(self, info_enable: bool = False):
+        Debugger.info_enable = info_enable
+
     def show(self, results):
     def show(self, results):
         image = draw_tracks(self.device.image, results)
         image = draw_tracks(self.device.image, results)
         cv2.imshow('OAS Tracker', image)
         cv2.imshow('OAS Tracker', image)
 
 
     def show_start(self):
     def show_start(self):
-        logger.info('OAS Track Debugger started')
+        logger.info('OAS Track Debugger show image start')
         self.sync_thread.start()
         self.sync_thread.start()
 
 
     def show_sync(self, image=None):
     def show_sync(self, image=None):
         self.sync_lock.acquire()
         self.sync_lock.acquire()
         if image is not None:
         if image is not None:
             # deepcopy
             # deepcopy
-            global glob_image
-            glob_image = copy.deepcopy(image)
-            self.sync_image = copy.deepcopy(image)
-            logger.info('OAS Track Debugger synced image')
+            Debugger.sync_image = copy.deepcopy(image)
+            # logger.info('OAS Track Debugger synced image')
         self.sync_lock.release()
         self.sync_lock.release()
 
 
     def show_stop(self):
     def show_stop(self):
+        logger.info('OAS Track Debugger show image stop')
         self.stop_event.set()
         self.stop_event.set()
         self.sync_thread.join()
         self.sync_thread.join()
 
 
@@ -102,11 +108,12 @@ def test_debugger():
         if file.suffix != '.png':
         if file.suffix != '.png':
             continue
             continue
         image = cv2.imdecode(fromfile(str(file), dtype=uint8), -1)
         image = cv2.imdecode(fromfile(str(file), dtype=uint8), -1)
-        # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
         debugger.show_sync(image)
         debugger.show_sync(image)
-        time.sleep(0.1)
+        time.sleep(0.2)
     debugger.show_stop()
     debugger.show_stop()
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    test_debugger()
+    # test_track()
+    pass

BIN
tasks/Hyakkiyakou/hya/hya_hclose_red.png


BIN
tasks/Hyakkiyakou/hya/hya_htitle.png


+ 18 - 0
tasks/Hyakkiyakou/hya/image1.json

@@ -52,5 +52,23 @@
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "冰冻"
     "description": "冰冻"
+  },
+  {
+    "itemName": "htitle",
+    "imageName": "hya_htitle.png",
+    "roiFront": "578,21,134,48",
+    "roiBack": "578,21,134,48",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
+  },
+  {
+    "itemName": "hclose_red",
+    "imageName": "hya_hclose_red.png",
+    "roiFront": "1056,177,54,54",
+    "roiBack": "1056,177,54,54",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
   }
   }
 ]
 ]

+ 11 - 0
tasks/Hyakkiyakou/hya/ocr.json

@@ -0,0 +1,11 @@
+[
+  {
+    "itemName": "bean_number",
+    "roiFront": "119,645,45,29",
+    "roiBack": "119,645,45,29",
+    "mode": "Digit",
+    "method": "Default",
+    "keyword": "",
+    "description": "最坏的情况下来判断豆子的数量"
+  }
+]

+ 158 - 17
tasks/Hyakkiyakou/script_task.py

@@ -1,19 +1,26 @@
 # This Python file uses the following encoding: utf-8
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # @author runhey
 # github https://github.com/runhey
 # github https://github.com/runhey
+import time
+
 import cv2
 import cv2
 import numpy as np
 import numpy as np
 
 
-from datetime import datetime
+from datetime import datetime, timedelta
 from numpy import uint8, fromfile
 from numpy import uint8, fromfile
+from random import choice
 from cached_property import cached_property
 from cached_property import cached_property
 # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
 # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
 from oashya.tracker import Tracker
 from oashya.tracker import Tracker
+from oashya.utils import draw_tracks
 
 
 from module.exception import TaskEnd
 from module.exception import TaskEnd
+from module.logger import logger
+from module.exception import RequestHumanTakeover
 from tasks.GameUi.game_ui import GameUi
 from tasks.GameUi.game_ui import GameUi
 from tasks.GameUi.page import page_hyakkiyakou
 from tasks.GameUi.page import page_hyakkiyakou
 from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
 from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
+from tasks.Hyakkiyakou.config import InferenceEngine, ModelPrecision
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
 from tasks.Hyakkiyakou.agent.agent import Agent
 from tasks.Hyakkiyakou.agent.agent import Agent
 from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
 from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
@@ -40,45 +47,179 @@ def plot_save(image, boxes):
 
 
 class ScriptTask(GameUi, HyaSlave):
 class ScriptTask(GameUi, HyaSlave):
 
 
+    @property
+    def _config(self):
+        return self.config.hyakkiyakou
+
     @cached_property
     @cached_property
     def tracker(self) -> Tracker:
     def tracker(self) -> Tracker:
-        args = {}
+        hyakkiyakou_models = self._config.hyakkiyakou_models
+        conf = hyakkiyakou_models.conf_threshold
+        nms = hyakkiyakou_models.iou_threshold
+        if conf < 0.2 or conf > 1:
+            raise RequestHumanTakeover('conf_threshold should be in [0.2, 1]')
+        if nms < 0.2 or nms > 1:
+            raise RequestHumanTakeover('iou_threshold should be in [0.2, 1]')
+        inf_en = 'onnxruntime' if hyakkiyakou_models.inference_engine == InferenceEngine.ONNXRUNTIME else 'tensorrt'
+        precision = 'fp32' if hyakkiyakou_models.model_precision == ModelPrecision.FP32 else 'int8'
+        # 这个坑后面在补
+        if inf_en == 'tensorrt' or precision == 'int8':
+            raise RequestHumanTakeover('Only support onnxruntime')
+        args = {
+            'conf_threshold': conf,
+            'iou_threshold': nms,
+            'precision': precision,
+            'inference_engine': inf_en,
+        }
         return Tracker(args=args)
         return Tracker(args=args)
 
 
     @cached_property
     @cached_property
     def agent(self) -> Agent:
     def agent(self) -> Agent:
-        return Agent()
+        hya_config = self._config.hyakkiyakou_config
+        sp = hya_config.hya_sp
+        ssr = hya_config.hya_ssr
+        sr = hya_config.hya_sr
+        r = hya_config.hya_r
+        n = hya_config.hya_n
+        g = hya_config.hya_g
+        weights: list = [sp, ssr, sr, r, n, g]
+        strategy: dict = {
+            'weights': weights,
+            'priorities': [],
+            'invite_friend': hya_config.hya_invite_friend,
+            'auto_bean': hya_config.hya_auto_bean
+        }
+        return Agent(strategy=strategy)
 
 
     @cached_property
     @cached_property
     def debugger(self) -> Debugger:
     def debugger(self) -> Debugger:
-        return Debugger()
+        debug_config = self._config.debug_config
+        fast_screenshot_interval = debug_config.screenshot_interval
+        if fast_screenshot_interval <= 100 or fast_screenshot_interval >= 1000:
+            raise RequestHumanTakeover('screenshot_interval must be between 1000 and 10000')
+        self.set_fast_screenshot_interval(fast_screenshot_interval)
+        return Debugger(info_enable=debug_config.hya_info)
 
 
     def run(self):
     def run(self):
+        hya_count: int = 0
+        self.limit_count: int = self._config.hyakkiyakou_config.hya_limit_count
+        limit_time = self._config.hyakkiyakou_config.hya_limit_time
+        self.limit_time: timedelta = timedelta(hours=limit_time.hour, minutes=limit_time.minute,
+                                               seconds=limit_time.second)
         self.ui_get_current_page()
         self.ui_get_current_page()
         self.ui_goto(page_hyakkiyakou)
         self.ui_goto(page_hyakkiyakou)
 
 
-        self.one()
+        if self._config.hyakkiyakou_config.hya_invite_friend:
+            self.invite_friend(True)
+
+        while 1:
+            if hya_count >= self.limit_count:
+                logger.info('Hyakkiyakou count limit out')
+                break
+            if datetime.now() - self.start_time >= self.limit_time:
+                logger.info('Hyakkiyakou time limit out')
+                break
+
+            self.one()
+            hya_count += 1
 
 
-        self.ui_click_until_disappear(self.I_UI_BACK_RED)
+        while 1:
+            self.screenshot()
+
+            if not self.appear(self.I_HACCESS):
+                continue
+            if self.appear(self.I_HCLOSE_RED):
+                break
+        self.ui_click_until_disappear(self.I_HCLOSE_RED)
         self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
         self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
         raise TaskEnd
         raise TaskEnd
 
 
     def one(self):
     def one(self):
-        pass
-
-
+        if not self.appear(self.I_HACCESS):
+            logger.warning('Page Error')
+        self.ui_click(self.I_HACCESS, self.I_HSTART, interval=2)
+        self.wait_until_appear(self.I_HTITLE)
+        # 随机选一个
+        click_button = choice([self.C_HSELECT_1, self.C_HSELECT_2, self.C_HSELECT_3])
+        while 1:
+            self.screenshot()
+            if not self.appear(self.I_HTITLE):
+                break
+            if self.appear_then_click(self.I_HSTART, interval=2):
+                continue
+            if not self.appear(self.I_HSELECTED):
+                self.click(click_button, interval=2)
+        self.device.stuck_record_add('BATTLE_STATUS_S')
+        # 正式开始
+        logger.hr('Start Hyakkiyakou')
+        init_bean_flag: bool = False
+        last_action = [0, 0, False, 10]
+        if self._config.debug_config.hya_show:
+            self.debugger.show_start()
+        while 1:
+            self.fast_screenshot()
+            if self.appear(self.I_HEND):
+                break
+            if not self.appear(self.I_CHECK_RUN):
+                continue
+            if not init_bean_flag:
+                init_bean_flag = True
+                self.bean_05to10()
+                time.sleep(0.5)
+            if not self.appear(self.I_HFREEZE):
+                # --------------------------------------------------------
+                slave_state = self.update_state()
+                tracks = self.tracker(image=self.device.image, response=last_action)
+                last_action = self.agent.decision(tracks=tracks, state=slave_state)
+                self.do_action(last_action, state=slave_state)
+            # debug
+            if self._config.debug_config.hya_show:
+                draw_image = draw_tracks(self.device.image, tracks)
+                self.debugger.show_sync(image=draw_image)
+
+        logger.info('Hyakkiyakou End')
+        if self._config.debug_config.hya_show:
+            self.debugger.show_stop()
+        self.ui_click(self.I_HEND, self.I_HACCESS)
+
+    def do_action(self, action: list, state):
+        x, y, throw, bean = action
+        if not throw:
+            return
+        if state[0] <= 0:
+            return
+        self.fast_click(x=x, y=y)
+
+
+# def test_opencv():
+#     import timeit
+#     from module.config.config import Config
+#     from module.device.device import Device
+#
+#     c = Config('oas1')
+#     d = Device(c)
+#
+#     t = ScriptTask(c, d)
+#     t.fast_screenshot()
+#     def sh():
+#         t.device.image = cv2.cvtColor(t.device.image, cv2.COLOR_BGR2RGB)
+#     execution_time = timeit.timeit(sh, number=100)
+#     print(f"执行总的时间: {execution_time * 1000} ms")
+#     cv2.imshow('test', t.device.image)
+#     cv2.waitKey(0)
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    # from module.config.config import Config
-    # from module.device.device import Device
+    from module.config.config import Config
+    from module.device.device import Device
+
+    c = Config('oas1')
+    d = Device(c)
 
 
-    # c = Config('oas1')
-    # d = Device(c)
+    t = ScriptTask(c, d)
+    t.run()
 
 
-    # t = ScriptTask(c, d)
-    # t.run()
-    from tasks.Hyakkiyakou.agent.tagent import test_observe, test_state
-    test_state()
+    # from tasks.Hyakkiyakou.agent.tagent import test_observe, test_state
+    # test_state()
     # from debugger import test_track
     # from debugger import test_track
     # test_track(show=False)
     # test_track(show=False)
     pass
     pass

+ 11 - 0
tasks/Hyakkiyakou/slave/hya_device.py

@@ -1,5 +1,6 @@
 import timeit
 import timeit
 
 
+from module.base.timer import Timer
 from module.logger import logger
 from module.logger import logger
 from module.base.utils import point2str
 from module.base.utils import point2str
 from tasks.base_task import BaseTask
 from tasks.base_task import BaseTask
@@ -13,7 +14,11 @@ class HyaDevice(BaseTask):
     3. 考虑JIT加速
     3. 考虑JIT加速
     我宣布世界上最好的 Linux 系统是 Windows
     我宣布世界上最好的 Linux 系统是 Windows
     """
     """
+    hya_screenshot_interval = Timer(0.2)  # 300ms
+
     def fast_screenshot(self):
     def fast_screenshot(self):
+        self.hya_screenshot_interval.wait()
+        self.hya_screenshot_interval.reset()
         self.device.image = self.device.screenshot_window_background()
         self.device.image = self.device.screenshot_window_background()
         return self.device.image
         return self.device.image
 
 
@@ -23,7 +28,13 @@ class HyaDevice(BaseTask):
         )
         )
         self.device.click_window_message(x=x, y=y, fast=True)
         self.device.click_window_message(x=x, y=y, fast=True)
 
 
+    def set_fast_screenshot_interval(self, interval: float):
+        """
 
 
+        @param interval: ms
+        @return:
+        """
+        self.hya_screenshot_interval = Timer(interval / 1000.)
 
 
 
 
 
 

+ 5 - 0
tasks/Hyakkiyakou/slave/hya_slave.py

@@ -172,6 +172,11 @@ class HyaSlave(HyaDevice, HyaColor, HyakkiyakouAssets):
                     unit_img.roi_back = self.UNIT0
                     unit_img.roi_back = self.UNIT0
                     if self.appear(unit_img):
                     if self.appear(unit_img):
                         return max(0, i)
                         return max(0, i)
+        # 最坏的情况下用ocr
+        num = self.O_BEAN_NUMBER.ocr(self.device.image)
+        if isinstance(num, int) and num >= 0:
+            return num
+
         logger.warning(f'Cannot predict bean, current: {current}')
         logger.warning(f'Cannot predict bean, current: {current}')
         return current
         return current